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

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?