【问题标题】:Cart total price Override in WooCommerceWooCommerce 中的购物车总价覆盖
【发布时间】:2018-08-21 22:17:00
【问题描述】:

我遇到了 Woocommerce 购物车总计仅显示 添加到购物车中的最后一件商品的价格 的问题。个别商品的价格与总价的加起来不正确,它只显示最后一件商品的价格。

这是我用来覆盖添加到购物车的产品价格的代码:

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

感谢任何帮助。

【问题讨论】:

  • 拜托,您应该尝试更好地解释您正在尝试做什么以及您的代码应该做什么(请注意您的代码没有注释),为我们提供更多详细信息和上下文。跨度>

标签: php wordpress woocommerce hook-woocommerce


【解决方案1】:

您没有在 woocommerce_calculate_totals 函数中累积小计。您每次都在写$cart_sub_total = $final_price;,所以最后您只会得到最后一件商品的价格。相反,它应该是$cart_sub_total = $cart_sub_total + $final_price;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 2017-04-05
    • 2016-10-29
    • 2017-09-10
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-09-01
    相关资源
    最近更新 更多