Repository Forums Support WooCommerce Extended Coupon Features PRO Buy 5 and get 2 (cheapest) for FREE

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #19345
    Veselina Boyanova
    Participant

    Hello,
    Your plugin is really wonderful!
    I need your kind assistance for the Black Promo:
    Buy 7 (products of a specific category) and get 2 (the cheapest of them) for FREE!
    Could you please tell me the right settings? Because I’ve already tried several combinations, but I couldn’t achiev my goal. 🙁
    Thank you in advance!
    Veselina

    #19346
    Soft79
    Keymaster

    If you paste the following snippet to your child theme, you get an option ‘Limit discount to: Two cheapest of every n items’ that you can use.

    1. Paste the snippet in your child theme’s functions.php
    2. Create a new 100% discount coupon
    3. On the ‘Usage restrictions’-tab set ‘Minimum quantity of matching products’ to: 7
    4. On the ‘Usage limits’-tab set ‘Limit discount to’ to ‘Two cheapest of every n items’

    That should do the trick!

    
    add_filter( 'wjecf_get_limit_to_options',
        function ( $options ) {
            $options['two_cheapest_for_every_n_items'] = [
                'callback' => 'limit_to_two_cheapest_for_every_n_items',
                'title' => __( 'Two cheapest of every n items. n = min qty of matching products (or 2 if not supplied)', 'soft79-emedia-custom')
            ];
            return $options;
        }, 10, 1
    );
    
    function limit_to_two_cheapest_for_every_n_items( $coupon, $discountable_cart_items ) {
        //nth is the amount entered in 'minimum qty of matching products' or 2
        $nth = intval( WJECF_Wrap( $coupon )->get_meta( '_wjecf_min_matching_product_qty' ) );
        if ( empty( $nth ) ) {
            $nth = 2;
        }
    
        //Discountable items; cheap to expensive
        uasort(
            $discountable_cart_items,
            function ( $cart_item_a, $cart_item_b ) {
                $price_a = $cart_item_a['data']->get_price();
                $price_b = $cart_item_b['data']->get_price();
                return $price_a <=> $price_b;
            }
        );
    
        $limits = [];
    
        //First find the total number of items
        $total_items = 0;
        foreach( $discountable_cart_items as $cart_item_key => $cart_item ) {
            $total_items += $cart_item['quantity'];
        }
    
        //This amount of items should be discounted
        $discountable_items_left = 2 * floor( $total_items / $nth );
    
        foreach( $discountable_cart_items as $cart_item_key => $cart_item ) {
            $limits[$cart_item_key] = min( $discountable_items_left, $cart_item['quantity'] );
            $discountable_items_left -= $limits[$cart_item_key];
        }
    
        return $limits;
    }
    
    #19349
    Veselina Boyanova
    Participant

    Thank you extremely much for your fast cooperation!!! It works like a charm!!! 🙂
    Have a wonderful week!!! 🙂

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.