admin管理员组文章数量:1424923
On the WP homepage I have a Form like this:
<form action="'.esc_url( admin_url('admin-post.php')).'" method="post">
//etc.
//etc.
and in the file functions.php I have this snipped of code.
function redirect_form() {
if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {
if ($_POST["var1"] == 'product1'){
wp_redirect(home_url('/product/product1/')); exit;
// etc.
// etc.
Everything is working as expected.
But the question is how can I get those variables on the WooCommerce single product page? I want echo those variables on the page of the product.
UPDATED:
I am trying the same thing with global variables. What I am missing?
function redirect_form() {
if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {
global $abc;
$abc = $_POST["var2"];
if ($_POST["var1"] == 'product1'){
wp_redirect(home_url('/products/product1/')); exit;
// etc.
// etc.
then
add_action( 'woocommerce_single_product_summary', 'dev_designs_show_sku', 5 );
function dev_designs_show_sku(){
global $product, $abc;
echo $abc;
}
It is not working. What I am missing?
UPDATED 2
then I want adjust price of the product based on those variables. Something like this.
function return_custom_price($price, $product) {
global $post, $blog_id, $abc;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0] + $abc);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
On the WP homepage I have a Form like this:
<form action="'.esc_url( admin_url('admin-post.php')).'" method="post">
//etc.
//etc.
and in the file functions.php I have this snipped of code.
function redirect_form() {
if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {
if ($_POST["var1"] == 'product1'){
wp_redirect(home_url('/product/product1/')); exit;
// etc.
// etc.
Everything is working as expected.
But the question is how can I get those variables on the WooCommerce single product page? I want echo those variables on the page of the product.
UPDATED:
I am trying the same thing with global variables. What I am missing?
function redirect_form() {
if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {
global $abc;
$abc = $_POST["var2"];
if ($_POST["var1"] == 'product1'){
wp_redirect(home_url('/products/product1/')); exit;
// etc.
// etc.
then
add_action( 'woocommerce_single_product_summary', 'dev_designs_show_sku', 5 );
function dev_designs_show_sku(){
global $product, $abc;
echo $abc;
}
It is not working. What I am missing?
UPDATED 2
then I want adjust price of the product based on those variables. Something like this.
function return_custom_price($price, $product) {
global $post, $blog_id, $abc;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0] + $abc);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
Share
Improve this question
edited Jun 11, 2019 at 19:10
Jozef P.
asked Jun 8, 2019 at 20:39
Jozef P.Jozef P.
151 silver badge5 bronze badges
2
- Are these variables you need for the single product page a one-time use based on user's input? Or are these variables going to stick around if a user navigates away from the product page and then comes back? – ChristopherJones Commented Jun 11, 2019 at 18:58
- @ChristopherJones . Good question. I don't know how to answer. I want adjust price of the product based on those variables. – Jozef P. Commented Jun 11, 2019 at 19:08
2 Answers
Reset to default 0I think you can maybe use $_SESSIONS or a $_GET var.
Option 1: (I think this will work best for you situation)
function redirect_form() {
if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {
$_SESSION['var2'] = htmlentities(stripslashes(trim($_POST["var2"])), ENT_QUOTES);
if ($_POST["var1"] == 'product1'){
wp_redirect(home_url('/products/product1/')); exit;
///// And In your price adjustment call
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0] + $_SESSION['var2']);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
Note: And at the top of your functions.php file, you'll need to start $_SESSIONS in order to use them.
function register_my_session(){
if( !session_id()){
session_start();
}
}
add_action('init', 'register_my_session');
Option 2: (If you run into $_SESSION issues)
function redirect_form() {
if (isset($_POST["var1"]) && !empty($_POST["var1"]) && isset($_POST["var2"]) && isset($_POST["var3"]) && !empty($_POST["var3"])) {
$url_parm = htmlentities(stripslashes(trim($_POST["var2"])), ENT_QUOTES);
if ($_POST["var1"] == 'product1'){
wp_redirect(home_url('/products/product1/?var2='.$url_parm)); exit;
///// And In your price adjustment call
function return_custom_price($price, $product) {
global $post, $blog_id;
$price = get_post_meta($post->ID, '_regular_price');
$post_id = $post->ID;
$price = ($price[0] + $_GET['var2']);
return $price;
}
add_filter('woocommerce_get_price', 'return_custom_price', 10, 2);
If you take Option 2, I would maybe think about protecting doing some checks and balances in your return_custom_price()
call to make sure you have clean data;
Hope that helps and let me know if you running into any issues!!
If you're looking to customize what's displayed on product pages, there are a couple of ways you can do this:
1) Overriding the product page templates:
The components that make up a product page can be found in wp-content/plugins/woocommerce/templates
, if you want to add variables to one or more parts of a product page, you can override that part of the page within your currently set theme.
For example if you wanted to add variables to the short description, you could copy the short description template:
wp-content/plugins/woocommerce/templates/single-product/short-description.php
to your currently set theme:
yourtheme/woocommerce/single-product/short-description.php`
and add variables/change the structure on your copy. You can do the same with the tabs in the product pages (e.g. additional information & description), check out the files in plugins/woocommerce/templates/single-product/tabs/
.
This is useful if you want to make major changes to a section, a downside is that you may need to re-copy the file for compatability if WooCommerce developers make changes to that template.
2) Add variables via filters: Some of the elements have filters, you could manipulate the data that would be used in each section using these.
Using the short description as an example again, you could add a hook to manipulate the data like so:
// define the woocommerce_short_description callback
function filter_woocommerce_short_description( $post_post_excerpt ) {
// make filter magic happen here...
return $post_post_excerpt;
};
// add the filter
add_filter( 'woocommerce_short_description', 'filter_woocommerce_short_description', 10, 1 );
Taken from hookr.io
You could do this in your functions.php
file or inside a custom plugin. The benefit of this is that it's a bit more robust against WooCommerce developer changes to the template, but you're more limited in terms of the changes you can make.
Update based on clarification
I think I misunderstood your question, if you're asking about variable scope, this is more of a PHP question than a WordPress question. You could declare your variable as a global, which you can then access anywhere.
function redirect_form() {#
/* This is now globally scoped, you can access it anywhere */
global $my_var;
$my_var = $_POST["var1"];
}
You'll then need to re-define you're global wherever you want to use it, so wherever your manipulating your product page code you can access it like this:
global $my_var;
echo $my_var;
Just be careful what you name your variable as you don't want to overwrite something important. I'd always try to avoid using globals if possible, depending on what you're trying to do, there may be a better/safer way of sharing this variable, but I wouldn't be able to suggest a better approach without fully knowing what it is you're trying to achieve.
本文标签: How to get POST variable on WooCommerce product page (form on homepage)
版权声明:本文标题:How to get _POST variable on WooCommerce product page (form on homepage) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745409622a2657406.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论