简体   繁体   English

如何使用购物车总数更改 WooCommerce 中的购物车商品价格

[英]How to use cart total to change cart items prices in WooCommerce

I am trying to pass the total amount of the cart ( $GetTotalPrice ) from function cart_prices_GetPrice() to function cart_prices_ApplyPrice() , using a woocommerce_after_calculate_totals and woocommerce_before_calculate_totals hooks, but I get an empty value. I am trying to pass the total amount of the cart ( $GetTotalPrice ) from function cart_prices_GetPrice() to function cart_prices_ApplyPrice() , using a woocommerce_after_calculate_totals and woocommerce_before_calculate_totals hooks, but I get an empty value.

//Trying to get cart amount
add_action('woocommerce_after_calculate_totals', 'cart_prices_GetPrice'); 
function cart_prices_GetPrice()  {

    //Getting the cart amount
    $GetTotalPrice = WC()->cart->get_cart_total();
    return $GetTotalPrice;

}

//Applying custom price
add_action('woocommerce_before_calculate_totals', 'cart_prices_ApplyPrice');
function cart_prices_ApplyPrice( $cart_object ) {

    //Getting the cart amount from first function
    
    $totalprice = cart_prices_GetPrice(); // doesn't work and returns 0 :(


    //price change to cost2
    if( $totalprice != 0 && $totalprice >= 2000 ) {
       foreach ( $cart_object->get_cart() as $cart_id => $cart_item ) {
            
               // get products id
               $product_id = $cart_item['product_id'];
               if( $cart_item['product_id'] == $product_id ) {
                   
               // price change to cost2
               $new_price1 = 0.20;
               $cart_item['data']->set_price( $new_price1 );
               
              }

       } 
    } 
 }

At the same time, each of the functions separately works perfectly.同时,每个功能都可以完美运行。
What am I doing wrong?我究竟做错了什么? Is it possible to somehow link two hook data so that the first one doesn't return an empty value?是否有可能以某种方式链接两个钩子数据,以便第一个不返回空值?


Update:更新:
I will not be able to refuse the hook woocommerce_before_calculate_totals , because I need to apply a separate price reduction for each product in the cart.我将无法拒绝钩子woocommerce_before_calculate_totals ,因为我需要为购物车中的每个产品单独降价。

You can simply not use cart total in woocommerce_before_calculate_totals when you want to alter cart item price for many reasons…当您出于多种原因想要更改购物车商品价格时,您根本不能在woocommerce_before_calculate_totals中使用购物车总数……

Instead you will get cart item subtotal inside woocommerce_before_calculate_totals hook.相反,您将在woocommerce_before_calculate_totals挂钩中获得购物车项目小计。 On the code below I use the discounted cart item subtotal including taxes:在下面的代码中,我使用了包含税费的折扣购物车商品小计:

add_action('woocommerce_before_calculate_totals', 'customize_cart_item_prices');
function customize_cart_item_prices( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Avoiding the hook repetition for price calculations
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $threshold_amount = 1000; // Min subtotal
    $discount_rate    = 0.2; // price discount rate (20%)

    $cart_items       = $cart->get_cart();

    // Getting non discounted cart items subtotal
    $subtotal_excl_tax = array_sum( wp_list_pluck( $cart_items, 'line_subtotal' ) );
    $subtotal_tax = array_sum( wp_list_pluck( $cart_items, 'line_subtotal_tax' ) );

    // Getting discounted cart items subtotal
    $total_excl_tax = array_sum( wp_list_pluck( $cart_items, 'line_total' ) );
    $total_tax = array_sum( wp_list_pluck( $cart_items, 'line_tax' ) );

    if( ( $total_excl_tax + $total_tax ) >= $threshold_amount ) {
        // Loop through cart items
        foreach ( $cart_items as $item ) {
            $price = $item['data']->get_price(); // Get price

            $item['data']->set_price( $price * ( 1 - $discount_rate ) ); // Set new price
        }
    }
}

Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.测试和工作。

See on Change cart item prices in Woocommerce 3 answer code, to see how to handle minicart displayed custom cart item price.请参阅Woocommerce 3 答案代码中的更改购物车项目价格,以了解如何处理迷你车显示的自定义购物车项目价格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM