通过 WooCommerce 中的 URL 获取并设置添加到购物车的自定义产品价格 [英] GET and set a custom product price on add to cart via URL in WooCommerce
问题描述
我正在尝试使用 URL 将参数传递给 Woocommerce 网站 - 例如:
I am trying to pass a parameter to a Woocommerce site using a URL - for instance:
site/?custom_p=77
将是 URL.
这是我的代码
add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
function custom_price( $cprice, $product ) {
// Delete product cached price (if needed)
wc_delete_product_transients($product->get_id());
// Get the data from the GET request
$custom_p=$_GET['custom_p'];
return ($custom_p) ;
}
添加到购物车的网址 site/?add-to-cart=455&custom_p=77
当我直接声明数字 77
时的问题 $custom_p=77
但 $_GET['custom_p']
声明给出为购物车中的结果为零.
The problem when I declare the number 77
directly it works $custom_p=77
but $_GET['custom_p']
declaration gives as a result zero in cart.
推荐答案
一旦 URL 更改,您的 GET 变量就会丢失,因此所有产品价格都会变空,因为您也没有针对已添加到购物车的产品.
Your GET variables get lost once URL changes, so all product prices get empty because also you are not targeting a the product that has been added to cart.
在这种情况下,当检测到 custom_p
GET 变量时,您需要启用并使用 WooCommerce 会话变量来存储产品 ID 和自定义价格.然后您可以使用该 WooCommerce 会话变量来更改产品价格.
In this case you need to enable and use a WooCommerce Session variable to store the product ID and the custom price, when a custom_p
GET variable is detected. Then you can use that WooCommerce Session variable to change the product price.
首先,我们检测必要的数据并将其存储在 WC_Session
变量中:
First we detect and store the necessary data in a WC_Session
variable:
// get and set the custom product price in WC_Session
add_action( 'init', 'get_custom_product_price_set_to_session' );
function get_custom_product_price_set_to_session() {
// Check that there is a 'custom_p' GET variable
if( isset($_GET['add-to-cart']) && isset($_GET['custom_p'])
&& $_GET['custom_p'] > 0 && $_GET['add-to-cart'] > 0 ) {
// Enable customer WC_Session (needed on first add to cart)
if ( ! WC()->session->has_session() ) {
WC()->session->set_customer_session_cookie( true );
}
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('custom_p', [
'id' => (int) wc_clean($_GET['add-to-cart']),
'price' => (float) wc_clean($_GET['custom_p']),
]);
}
}
然后有两种方法可以更改添加到购物车的产品的价格(仅选择一种)
选项 1 - 直接更改产品价格:
// Change product price from WC_Session data
add_filter('woocommerce_product_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_product_price', 900, 2 );
function custom_product_price( $price, $product ) {
if ( ( $data = WC()->session->get('custom_p') ) && $product->get_id() == $data['id'] ) {
$price = $data['price'];
}
return $price;
}
<小时>
选项 2 - 更改购物车商品价格:
// Change cart item price from WC_Session data
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1 );
function custom_cart_item_price( $cart ){
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Must be required since Woocommerce version 3.2 for cart items properties changes
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Looo through our specific cart item keys
foreach ( $cart->get_cart() as $cart_item ) {
// Get custom product price for the current item
if ( ( $data = WC()->session->get('custom_p') ) && $cart_item['data']->get_id() == $data['id'] ) {
// Set the new product price
$cart_item['data']->set_price( $data['price'] );
}
}
}
代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
USAGE URL 变量示例:www.example.com/?add-to-cart=37&custom_p=75
USAGE URL variables example:
www.example.com/?add-to-cart=37&custom_p=75
这篇关于通过 WooCommerce 中的 URL 获取并设置添加到购物车的自定义产品价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!