【问题标题】:Modify cart total amount with filter or hook使用过滤器或挂钩修改购物车总金额
【发布时间】:2015-03-14 22:13:34
【问题描述】:

好的,所以我正在尝试编写一个过滤器或挂钩,它将根据购物车中商品的总数量来修改购物车的总金额。例如,如果购物车中有三件商品,我想从总订单中扣除 20 美元。以下是我到目前为止的代码,非常感谢任何帮助!

add_filter('woocommerce_cart_contents_total', 'bundle_deals');

function bundle_deals( $cart_contents_total, $cart_contents_count) {
   global $woocommerce;

   if ($woocommerce->cart->get_cart()->cart_contents_count <= 3) {
     $cart_contents_total = $woocommerce->cart->get_cart()->cart_contents_total - 20.00;
   }

   return $cart_contents_total;
}

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:
    function woocommerce_cart_subtotal(  $cart_subtotal, $compound, $obj  ){
    
    $t = 0;
    foreach ( $obj->cart_contents as $key => $product ) : 
    
        $product_price = $product['line_total'];    
    
        foreach ( WC()->cart->get_coupons( 'order' ) as $code => $coupon ) : 
    
                $product_price  = 19;//wpq_9522_discount( $product['line_total'], $coupon->discount_type, $coupon->amount ); 
    
        endforeach; 
    
        $t += $product_price;
    
    endforeach; 
    
    return $cart_subtotal;//( $t > 0 ) ? sprintf( '<s>%s</s> %s',      
    $cart_subtotal, wc_price( $t ) ) : $cart_subtotal ;
    }
    
    add_filter( 'woocommerce_cart_subtotal', 'woocommerce_cart_subtotal',      99, 3 );
    

    【讨论】:

      【解决方案2】:

      总金额可以通过以下钩子更改。

      add_action('woocommerce_calculate_totals', array($this, 'calculate_totals'), 10, 1);
      
      function calculate_totals($totals){
      //your code
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-11-22
        • 1970-01-01
        • 2013-06-29
        • 1970-01-01
        • 2017-04-28
        • 1970-01-01
        • 1970-01-01
        • 2021-12-11
        相关资源
        最近更新 更多