You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce:从商品列表页添加自定义价格至购物车和心愿单问题

WooCommerce自定义价格同步购物车与心愿单解决方案

问题背景

为特定分类的商品在列表页计算并展示自定义价格(商品常规价格设为0),使用默认加入购物车按钮,但自定义价格无法同步到购物车和心愿单,已尝试保存购物车项数据但无效。

解决方案

1. 修正自定义价格展示代码(确保隐藏字段在加购表单内)

原代码的隐藏输入字段可能不在加购表单范围内,导致POST请求无法获取值,调整钩子位置并修正输出:

// Display custom price on product listing page for specific categories
add_action('woocommerce_before_add_to_cart_button', 'display_custom_price_on_product_listing', 25);

function display_custom_price_on_product_listing() {
    global $product, $wpdb;

    if (has_term(array('ring', 'rings', 'pendants', 'mangalsutra', 'mangalsutras', 'bracelet', 'bracelets'), 'product_cat')) {
        extract(get_all_product_data($product, true));

        // Load data from custom tables for calculations
        $metal_purity_percent_new = get_metal_purity($metal_purity);
        $diamond_total = $dqone + $dqtwo + $dqthree + $dqfour + $dqfive + $dqsix + $dqseven + $dqeight + $dqnine + $dqten;

        // Color stone price calculation
        $colorstone_price = get_colorstone($colorstone);
        $colorstone_weightnew = (float) $product->get_attribute('colorstone-weight');
        $colorstone_total = $colorstone_price * $colorstone_weightnew;

        $alltax = get_extra_charges();
        $gst = get_gst();

        // Gold value & making charge calculations
        $gold_value_new = ($gold_rate * $metal_purity_percent_new) / 100;
        $mc = makingcharge($net_weight, $metal_type);
        $rt = makingchargerate_type($net_weight, $metal_type);
        
        if ($rt == 'Flat') {
            $flatrate = $net_weight < 2 ? $mc : $mc * $net_weight;
        } else {
            $gold_valuerate = ($gold_rate * $metal_purity_percent_new) / 100;
            $percentagerate = ($gold_valuerate * $mc) / 100;
            $flatrate = $percentagerate * $net_weight;
        }

        $totalvalue = round(($gold_value_new * $net_weight) + $regular_price + $colorstone_total + $alltax + $diamond_total + $flatrate);
        $custom_price = $totalvalue + (($totalvalue * $gst) / 100);

        // Display custom price
        echo '<p class="product-custom-price" style="font-size: 20px;">' . wc_price($custom_price) . '</p>';

        // Hidden input inside add-to-cart form
        echo '<input type="hidden" name="listing_price" value="' . esc_attr($custom_price) . '">';
    }
}

2. 完善购物车项数据的保存与恢复

仅保存数据还不够,需确保会话中恢复自定义数据,并避免同商品不同价格被合并:

// Save custom cart item data
add_filter('woocommerce_add_cart_item_data', 'save_custom_cart_item_data', 10, 3);
function save_custom_cart_item_data($cart_item_data, $product_id, $variation_id) {
    if (isset($_POST['listing_price']) && !empty($_POST['listing_price'])) {
        $custom_price = (float) sanitize_text_field($_POST['listing_price']);
        $cart_item_data['listing_price'] = $custom_price;

        // Make cart item unique to avoid merging with same product
        $cart_item_data['unique_key'] = md5(microtime().rand());
    }
    return $cart_item_data;
}

// Restore custom data from session
add_filter('woocommerce_get_cart_item_from_session', 'restore_custom_cart_item_data', 10, 2);
function restore_custom_cart_item_data($cart_item, $values) {
    if (isset($values['listing_price'])) {
        $cart_item['listing_price'] = $values['listing_price'];
    }
    return $cart_item;
}

3. 修改购物车的价格显示与计算逻辑

覆盖默认价格显示,并强制系统使用自定义价格计算总价:

// Display custom price in cart
add_filter('woocommerce_cart_item_price', 'display_custom_cart_item_price', 10, 3);
add_filter('woocommerce_cart_item_subtotal', 'display_custom_cart_item_subtotal', 10, 3);

function display_custom_cart_item_price($price, $cart_item, $cart_item_key) {
    if (isset($cart_item['listing_price'])) {
        $price = wc_price($cart_item['listing_price']);
    }
    return $price;
}

function display_custom_cart_item_subtotal($subtotal, $cart_item, $cart_item_key) {
    if (isset($cart_item['listing_price'])) {
        $subtotal = wc_price($cart_item['listing_price'] * $cart_item['quantity']);
    }
    return $subtotal;
}

// Update cart totals with custom price
add_action('woocommerce_before_calculate_totals', 'update_cart_totals_with_custom_price', 10, 1);
function update_cart_totals_with_custom_price($cart) {
    if (is_admin() && !defined('DOING_AJAX')) return;

    foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
        if (isset($cart_item['listing_price'])) {
            $cart_item['data']->set_price($cart_item['listing_price']);
        }
    }
}

4. 同步自定义价格到心愿单(以YITH WooCommerce Wishlist为例)

如果使用YITH心愿单,添加以下钩子同步自定义价格:

// Save custom price to YITH Wishlist
add_filter('yith_wcwl_added_product_custom_item_data', 'save_custom_price_to_wishlist', 10, 3);
function save_custom_price_to_wishlist($custom_data, $product_id, $variation_id) {
    if (isset($_POST['listing_price']) && !empty($_POST['listing_price'])) {
        $custom_data['listing_price'] = (float) sanitize_text_field($_POST['listing_price']);
    }
    return $custom_data;
}

// Display custom price in YITH Wishlist
add_filter('yith_wcwl_product_price', 'display_custom_price_in_wishlist', 10, 2);
function display_custom_price_in_wishlist($price, $product) {
    // Get wishlist item data
    $wishlist_items = YITH_WCWL()->get_wishlist_items();
    foreach ($wishlist_items as $item) {
        if ($item['product_id'] == $product->get_id() && isset($item['custom_data']['listing_price'])) {
            $price = wc_price($item['custom_data']['listing_price']);
            break;
        }
    }
    return $price;
}

关键说明

  • 确保隐藏字段在加购表单内:使用woocommerce_before_add_to_cart_button钩子,保证输入字段被包含在默认加购表单中,POST请求才能获取到值。
  • 强制购物车项唯一:添加unique_key避免同一款商品的不同自定义价格被合并。
  • 修改价格计算:通过woocommerce_before_calculate_totals直接设置商品价格,确保系统计算总价时使用自定义价格。

内容的提问来源于stack exchange,提问作者Nilima Bose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 03:38:13