每第三种产品(WooCommerce)增加50%的折扣

问题描述 投票:1回答:1

我在购物车上为每3个产品添加了折扣规则:3,6,9,12,15 ..它应该只对最便宜的产品折扣50%。

因此,如果您有9个,只有最便宜的3个可以享受50%的折扣。

此代码对所有产品都适用折扣,因此应仅每3个产品

add_action('woocommerce_cart_calculate_fees', 'ts_add_custom_discount', 10, 1 );
function ts_add_custom_discount( $wc_cart ){
    $discount = 0;
    $product_ids = array();
    $item_prices = array();
    $in_cart = false;

    foreach ( $wc_cart->get_cart() as $cart_item_key => $cart_item ) {
        $cart_product = $cart_item['data'];
        if ( has_term( 'get2', 'product_cat', $cart_product->get_id() ) ) { // get2 selected category
            $in_cart = true;
        }else {
            $in_cart = true;
            $product_ids[] = $cart_product->get_id();
            $item_prices[$cart_product->get_id()] = $cart_product->get_price();
        }
    }

    if( $in_cart ) {

        $count_ids = count($product_ids);

        asort( $item_prices ); //Sort the prices from lowest to highest

        $cartQuantity = WC()->cart->cart_contents_count;

        $count = 0;

        if( $count_ids > 3 || $cartQuantity >= 3 ) { 
           foreach( $item_prices as $id => $price ) {
                if( $count >= 1 ) {
                    break;
                }
                //$product = wc_get_product( $id );
                //$price = $product->get_price();
                $discount -= ($price * 50) /100;
                $count++;
           }
       }

    } 

    if( $discount != 0 ){
        $wc_cart->add_fee( 'Discount', $discount, true  );
    }
}

我附上了screenshot,您会看到每第3个最便宜的产品都有红色轮廓。

php wordpress woocommerce hook-woocommerce discount
1个回答
0
投票

这将在最便宜的产品上为每第三个产品提供50%的折扣

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

    // Products in cart
    $products_in_cart = count( $cart->get_cart() );

    // Every so many products
    $every = 3;

    // When products in cart greater than or equal to every so many products
    if ( $products_in_cart >= $every ) {
        // Set array
        $product_prices = array();

        // Loop though cart items
        foreach ( $cart->get_cart() as $cart_item ) {
            // Product
            $product = $cart_item['data'];

            // Get price
            $product_price = $product->get_price();

            // Push
            $product_prices[] = $product_price;
        }

        // Sort: low to high
        asort( $product_prices );

        // Number of products receive a discount
        $products_receive_discount = floor( $products_in_cart / $every );

        // Set variable
        $total = 0;

        // Counter
        $i = 0;

        // Loop trough
        foreach ( $product_prices as $product_price ) {
            if( $i == $products_receive_discount ) break;

            // Calculate
            $total += $product_price;

            // Counter
            $i++;
        }

        // Calculate discount
        $discount = ( $total * 50) / 100;

        // Add fee
        $cart->add_fee( 'Discount', $discount, true );
    }
}
add_action( 'woocommerce_cart_calculate_fees', 'custom_fee', 10, 1 );
© www.soinside.com 2019 - 2024. All rights reserved.