douhui3305 2018-08-21 22:17
浏览 45
已采纳

购物车总价格在WooCommerce中覆盖

I am running into issues with the Woocommerce cart total only displaying the price of the last item added to the cart. Individual items prices are not adding up to the total price correctly, it only show the price of the last item.

Here is the code I'm using for overriding the price of the products added to the cart:

function action_woocommerce_before_cart_table() {
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    foreach ($items as $item => $values) {
        $price = 0;
        if (array_key_exists('addons', $values) && count($values['addons']) > 0) {
            foreach ($values['addons'] as $value) {
                $price = $price + $value['price'];
            }
        } else {
            $regular_price = get_post_meta($values['product_id'], '_regular_price', true);
            $sale_price = get_post_meta($values['product_id'], '_sale_price', true);
            $price = $regular_price;
        }
        $values['data']->set_price($price);
    }
}

add_action('woocommerce_before_cart_table', 'action_woocommerce_before_cart_table', 10, 0);

function action_woocommerce_checkout_before_order_review() {
    foreach (WC()->cart->get_cart() as $item => $values) {
        $price = 0;
        $quantity = $values['quantity'];
        if (array_key_exists('addons', $values) && count($values['addons']) > 0) {
            foreach ($values['addons'] as $value) {
                $price = $price + $value['price'];
            }
        } else {
            $regular_price = get_post_meta($values['product_id'], '_regular_price', true);
            $sale_price = get_post_meta($values['product_id'], '_sale_price', true);
            $price = $regular_price;
        }

//        $final_price = $quantity * $price;
        $values['data']->set_price($price);
    }
}

add_action('woocommerce_review_order_before_cart_contents', 'action_woocommerce_checkout_before_order_review', 10, 0);

function woocommerce_calculate_totals($cart) {
    $cart_sub_total = 0;
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    foreach ($items as $item => $values) {
        $price = 0;
        $quantity = $values['quantity'];
        if (array_key_exists('addons', $values) && count($values['addons']) > 1) {
            foreach ($values['addons'] as $value) {
                $price = $price + $value['price'];
            }
        } else {
            $regular_price = get_post_meta($values['product_id'], '_regular_price', true);
            $sale_price = get_post_meta($values['product_id'], '_sale_price', true);
            $price = $regular_price;
        }
        $final_price = $quantity * $price;
        $cart_sub_total = $final_price;
        $values['data']->set_price($cart_sub_total);
    }
    WC()->cart->subtotal = $cart_sub_total;

    $cart->sub_total = $cart_sub_total;
    $coupons = WC()->cart->get_coupons();
    $cupon_price = 0;
    if (!empty($coupons)) {
        foreach ($coupons as $code => $coupon) {
            $cupon_price = $cupon_price + $coupon->get_amount();
        }
    }
    $new_price = $cart_sub_total - $cupon_price;
    WC()->cart->total = $new_price;
    $cart->total = $new_price;
}

add_action('woocommerce_before_cart_totals', 'woocommerce_calculate_totals', 30);
add_action('woocommerce_after_calculate_totals', 'woocommerce_calculate_totals', 30);

Appreciate any help.

  • 写回答

1条回答 默认 最新

  • donglin1692 2018-08-21 23:07
    关注

    You're not accumulating the subtotal in your woocommerce_calculate_totals function. You're writing $cart_sub_total = $final_price; every time so you'll only have the price of the last item at the end. Instead it should be $cart_sub_total = $cart_sub_total + $final_price;.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像