计算和更新购物车- WooCommerce中的产品价格



我正在创建一个WooCommerce (Version 5.6.0)网站,用户可以在这里购买泡沫产品,并在存档页面上计算价格。基本上,用户输入尺寸并根据公式计算价格。我目前正在努力更新购物车中的价格,我没有定制产品和价格在这个级别的经验,所以任何帮助将非常感激。到目前为止我所做的步骤:

1。在WC_Session

中获取(通过Ajax)并设置自定义产品价格
function get_custom_product_price_set_to_session(){
// Check that there is a 'foamPrice' and foamProductI _POST variable
if (isset($_POST['foamProductId']) && isset($_POST['foamPrice'])) {
// 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('foam_price', [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
}}
add_action('wp_ajax_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
add_action('wp_ajax_nopriv_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');

2。从WC_Session数据中更改购物车商品价格:

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;
// Loop 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('foam_price')) && $cart_item['data']->get_id() == $data['id']) {
// Set the new product price
$cart_item['data']->set_price($data['price']);
}
}}

我遇到的问题是,它只适用于添加到购物车的最新产品,以前添加到购物车的产品每次添加新产品到购物车时都被重新设置为0价格。如果有人能提供一些指导,我将非常感激。非常感谢!

With

WC()->session->set('foam_price', [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);

你基本上重写了之前的&;foam_price&;与最新添加到购物车。

在我看来,您需要为不同的产品id存储不同的值。

Try with (untesting):

function get_custom_product_price_set_to_session(){
// Check that there is a 'foamPrice' and foamProductI _POST variable
if (isset($_POST['foamProductId']) && isset($_POST['foamPrice'])) {
// 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('foam_price' . wc_clean($_POST['foamProductId']), [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
}}
add_action('wp_ajax_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
add_action('wp_ajax_nopriv_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');

…然后:

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;
// Loop 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('foam_price' . $cart_item['data']->get_id())) && $cart_item['data']->get_id() == $data['id']) {
// Set the new product price
$cart_item['data']->set_price($data['price']);
}
}}

最新更新