Woocommerce 根据数量对每件商品进行百分比折扣

根据WooCommerce 购物车数量基本折扣答案代码,我对购物车上的 xx 产品应用折扣,具体如下:

  add_action( 'woocommerce_cart_calculate_fees','wc_cart_quantity_discount', 10, 1 );

function wc_cart_quantity_discount( $cart_object ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )

        return;


    ## -------------- DEFINIG VARIABLES ------------- ##

    $discount = 0;

    $cart_item_count = $cart_object->get_cart_contents_count();

    $cart_total_excl_tax = $cart_object->subtotal_ex_tax;


    ## ----------- CONDITIONAL PERCENTAGE ----------- ##

    if( $cart_item_count <= 17 )

        $percent = 0;

    elseif( $cart_item_count >= 18 && $cart_item_count <= 120 )

        $percent = 10;




    ## ------------------ CALCULATION ---------------- ##

    $discount -= ($cart_total_excl_tax / 100) * $percent;


    ## ----  APPLYING CALCULATED DISCOUNT TAXABLE ---- ##

    if( $percent > 0 )

        $cart_object->add_fee( __( 'Remise sur la quantité 10%', 'woocommerce' ), $discount, true);

}

但我想管理每种产品的数量折扣。


你对此有什么建议吗?


森林海
浏览 115回答 1
1回答

慕村225694

以下将根据商品数量进行百分比折扣:add_action( 'woocommerce_cart_calculate_fees','cart_quantity_discount' );function cart_quantity_discount( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; $discount = 0; // Initializing&nbsp; &nbsp; $percent&nbsp; = 10;&nbsp; // Percentage&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; // Loop through cat items&nbsp; &nbsp; foreach ( $cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; $subtotal = $cart_item['line_total'];&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Calculation by Item based on quantity&nbsp; &nbsp; &nbsp; &nbsp; if( $cart_item['quantity'] > 17 ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $discount += $cart_item['line_total'] * $percent / 100;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( $discount > 0 ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart->add_fee( __( "Item quantity discount", "woocommerce" ), -$discount, true );&nbsp; &nbsp; }&nbsp; &nbsp;}代码位于活动子主题(或活动主题)的functions.php 文件中。它应该有效。
打开App,查看更多内容
随时随地看视频慕课网APP