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

标签 php wordpress woocommerce cart hook-woocommerce

我正在尝试从函数 $GetTotalPrice 传递购物车的总量( cart_prices_GetPrice() )发挥作用cart_prices_ApplyPrice() , 使用 woocommerce_after_calculate_totalswoocommerce_before_calculate_totals钩子(Hook),但我得到一个空值。

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

       } 
    } 
 }
同时,每个功能都可以完美运行。
我究竟做错了什么?是否有可能以某种方式链接两个钩子(Hook)数据,以便第一个不返回空值?

更新:
我将无法拒绝钩子(Hook)woocommerce_before_calculate_totals ,因为我需要为购物车中的每个产品单独 Markdown 。

最佳答案

您不能在 woocommerce_before_calculate_totals 中使用购物车总数当您出于多种原因想要更改购物车商品价格时……
相反,您将在 woocommerce_before_calculate_totals 内获得购物车项目小计钩。在下面的代码中,我使用了包含税费的折扣购物车商品小计:

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
        }
    }
}
代码位于事件子主题(或事件主题)的 functions.php 文件中。测试和工作。
请参阅 Change cart item prices in Woocommerce 3回答代码,查看如何处理 minicart 显示的自定义购物车项目价格。

关于php - 如何使用购物车总数来更改 WooCommerce 中的购物车商品价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66899108/

相关文章:

wordpress - "Add New"插件按钮未出现在 WordPress 管理面板上

javascript - Https(SSL 证书)阻止了我在 wordpress 网站上的媒体小部件

wordpress - Woocommerce - 手动删除部分 URL 导致 404 错误

php - 带有水印提示和提交值的输入文本框

php - 如何在android中制作JSON响应的toast消息?

javascript - 在 wordpress 插件中使用 wp_remote_get 时 Ajax 调用失败

css - 在智能手机上停止滚动时删除 "Image Zoom"效果

PHP 通过要重定向的 URL 传递变量

PHP/MySQL - 插入 1 条记录后无法提交值

mysql - SQL条件if不存在