首页 > 解决方案 > 计算和更新购物车中的产品价格 - WooCommerce

问题描述

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

1.获取(通过Ajax)并在WC_Session中设置自定义产品价格

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 价格。如果有人能够提供一些指导,我将不胜感激。感谢磨坊!

标签: phpwordpresssessionwoocommerce

解决方案


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

您基本上是用最新添加到购物车的“foam_price”覆盖了之前的“foam_price”。

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

尝试(未经测试):

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']);
    }
}}

推荐阅读