基于 Woocommerce 中特定类别购物车项目计数的有条件累进百分比折扣

时间:2021-07-24 15:27:22

标签: wordpress woocommerce

我从@LoicTheAztec 找到了这段精彩的代码,但我希望他为特定类别执行此功能,有人可以帮助我吗?

我尝试加入一些代码,但无法加入

我需要做这个累进折扣,特定类别的购物车中每件产品5%,最高可达15%,即可以是同一产品的3件商品或同一类别的3件商品< /p>

add_action( 'woocommerce_cart_calculate_fees', 'cart_progressive_discount', 50, 1 );
function cart_progressive_discount( $cart ) {
   /* if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;*/
    if ( is_product_category () && has_term ('nossos-produtos', 'product_cat') ) return;
    

    ## ------ Settings below ------- ##

    $percent = 5; // Percent rate: Progressive discount by steps of 5%
    $max_percentage = 15; // 50% (so for 10 items as 5 x 10 = 50)
    $discount_text = __( 'Desconto Produto Brincante', 'woocommerce' ); // Discount Text

    ## ----- ----- ----- ----- ----- ##

    $cart_items_count = $cart->get_cart_contents_count();
    $cart_lines_total = $cart->get_subtotal() - $cart->get_discount_total();

    // Dynamic percentage calculation
    $percentage = $percent * ($cart_items_count - 0);

    // Progressive discount from 5% to 45% (Between 2 and 10 items)
    if( $percentage < $max_percentage ) {
        $discount_text .=  ' (' . $percentage . '%)';
        $discount = $cart_lines_total * $percentage / 100;
        $cart->add_fee( $discount_text, -$discount );
    }
    // Fixed discount at 50% (11 items and more)
    else {
        $discount_text .=  ' (' . $max_percentage . '%)';
        $discount = $cart_lines_total * $max_percentage / 100;
        $cart->add_fee( $discount_text, -$discount );
    }
}

0 个答案:

没有答案