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;.

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

报告相同问题?

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装