根据WooCommerce购物车中的数量为特定产品类别提供折扣

时间:2020-10-28 11:46:12

标签: php wordpress woocommerce categories cart

我目前所说的折扣

  • 如果购物车中有6个特定类别的产品---折扣总价$ 10

这段代码可以正常工作。我的问题是,这不起作用

  • 如果有7个产品,则第7个产品来自不同的类别。

我的目标是,无论购物车中有多少种产品,只要“ A类”中有6种产品,就可以享受折扣。

只要“折扣类别”中有6种产品,或者有1种产品的数量为6等,以下代码就可以使用。当我添加其他类别的产品时,它会崩溃。随时撕开它。

add_action( 'woocommerce_before_calculate_totals', 'this_item_free' );

function this_item_free() {

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $valid_product_category_id = array('soup-mix');
    $has_category = false;

    $count = WC()->cart->get_cart_contents_count();

    foreach ( WC()->cart->get_cart() as $product ) {
        $quantity = $product['quantity'];
        $price = $product['data']->get_price();
    }

    if ( has_term( $valid_product_category_id, 'product_cat', $product['product_id'],
            $product['quantity'] ) ) {
        $has_category = true;
        $cart_total = floatval( preg_replace( '#[^\d.]#', '', WC()->cart->get_cart_total() ) );

        if($count == 6 && $has_category = true){
            add_action( 'woocommerce_cart_calculate_fees', 'discount_based_on_total', 25, 1 );
            function discount_based_on_total( $cart ) {

                $total = $cart->cart_contents_total;
                $discount = 9.95;

                $cart->add_fee( __('discount', 'woocommerce'), -$discount );
                wc_add_notice( apply_filters( 'discount_applied', 'You just got a free soup!') );

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的代码包含一些不必要的步骤,您只需使用woocommerce_cart_calculate_fees动作钩子即可获得想要实现的目标

我的答案包含:

无论购物车中有多少种产品,只要“类别A”中有6种产品或数量为6的1种产品,就可以享受折扣。

function action_woocommerce_cart_calculate_fees( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    /* SETTINGS */

    // Specific categories
    $specific_categories = array( 'Categorie-A' );

    // Discount
    $discount = 10;
    
    // Min quantity
    $minimun_quantity = 6;

    /* END SETTINGS */
    
    // Counter
    $current_quantity = 0;

    // Loop though each cart item
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get product id
        $product_id = $cart_item['product_id'];

        // Has certain category     
        if ( has_term( $specific_categories, 'product_cat', $product_id ) ) {
            // Quantity
            $product_quantity = $cart_item['quantity'];

            // Add to total
            $current_quantity += $product_quantity;
        }
    }

    // Greater than or equal to
    if ( $current_quantity >= $minimun_quantity ) {          
        // Add fee
        $cart->add_fee( __( 'Discount_applied', 'woocommerce' ), -$discount, false );      
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );

要应用每6种产品的折扣(6 = $ 10、12 = $ 20、18 = $ 30等)

替换

// Greater than or equal to
if ( $current_quantity >= $minimun_quantity ) {          
    // Add fee
    $cart->add_fee( __( 'Discount_applied', 'woocommerce' ), -$discount, false );      
}

使用

// Greater than or equal to
if ( $current_quantity >= $minimun_quantity ) {
    // Modulo
    $mod = $current_quantity % $minimun_quantity;
    
    // Times it fit
    $times = ( $current_quantity - $mod ) / $minimun_quantity;

    // Discount * times
    $discount = $discount * $times;
    
    // Add fee
    $cart->add_fee( __( 'Discount_applied', 'woocommerce' ), -$discount, false );      
}