Repository Forums Support WooCommerce Extended Coupon Features PRO how to select 2 free products on products with 2 quantity

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #4385
    numep
    Participant

    Hi, I’m trying to create a coupon where user gets to select 2 free products when they add minimum 2 quantity of same product…I could select the option to multiple Free products and put 1 in minimum products but if user select’s 1 quantity of product coupon will apply…so I need to limit user to be able to select 2 Free Products only if user adds 2 quantity of same product. Please help.

    #4386
    Soft79
    Keymaster

    You need to add a snippet to your CHILD THEME’s functions.php for that:

    
    add_filter( 'wjecf_free_product_amount_for_coupon', function ( $amount, $coupon ) {
        if ( 0 === strcasecmp( $coupon->get_code(), 'THE_COUPON_CODE' ) ) $amount = 2;
        return $amount;
    }, 10, 2 );
    

    Replace THE_COUPON_CODE by the actual coupon code. This snippet fixes the allowed amount of free products for the coupon to two. You need to set Min quantity of matching items of the coupon to 2 as well.

    #4387
    numep
    Participant

    ok so I read other posts and created a hack and added a INPUT type number so ADMIN can choose quantity of Free products using this code:

    private function get_free_product_amount_for_coupon( $coupon ) {
    		$coupon_qty = $this->get_quantity_products_value( $coupon );
    		$coupon_multiplier_qty = $this->get_product_multiplier_value( $coupon );
    		return apply_filters( 'wjecf_free_product_amount_for_coupon', ($coupon_qty > 0 ? $coupon_qty:$coupon_multiplier_qty ), $coupon ); // 2.3.3
    	}

    so now I would like to know a last question, can u help me out with a filter or code to add an ADDITIONAL free gift on a coupon? it should be 1 free product something like this $this->add_to_cart( 854, 1,'', '', $cart_item_data );

    #4388
    Soft79
    Keymaster

    Hi,

    That field is planned to be implemented in a future version of the plugin, but it takes some planning because it will include mathematics. e.g.: quantity = floor( items_in_cart / 5 )

    A suggestion: Your hack can be implemented without editing core files by using the wjecf_free_product_amount_for_coupon filter and using our development best practices. See the example code at https://www.soft79.nl/documentation/wjecf/development/ )

    #4389
    Soft79
    Keymaster

    The additional single free gift is a bit more complicated; you can hack into the template files (copy them to your child theme!) and make the input of that particular item hidden and force the value to 1.

    #4390
    numep
    Participant

    yeah thanks for suggestion, I do know is best not to edit the core files but since I need to add a input field in admin so they can choose the QUANTITY than I had to edit core files, anyways this is no issues since if u update plugin I can compare files and update necessary files, at the moment hack is working as I want it to work.

    Just want to add other hack that lets add 1 manual additional FREE PRODUCT if coupon applied can u help me with this can’t seem to find where to insert code however I suspect is in this function private function set_free_product_amount_in_cart( $product_id, $quantity ) {.

    #4391
    numep
    Participant

    Ok think I found the solution for this:

    // Add free products or adjust the quantity
    		$cart_item_key = $this->set_free_product_amount_in_cart( 854, 1 );
    		foreach ( $free_product_ids as $product_id => $qty ) {
    			$cart_item_key = $this->set_free_product_amount_in_cart( $product_id, $qty );
    		}
    	}

    UPDATE: Only problem it wont remove when coupon is not valid 🙁

    #4392
    Soft79
    Keymaster

    You don’t need to hack the core files, the example I sent you also adds an input field to admin, see init_admin_hook(), admin_coupon_meta_fields() and action_woocommerce_coupon_options_usage_restriction().

    The extra free item can probably be solved in template override. Selected item(s) are store in WC session data: _wjecf_session_data['selected_free_products']. Maybe you can apply a filter to override this also.

    Really, try not to edit core files of any plugin or theme. 99% of the time it’s possible to achieve something without hacking core files.

    #4393
    numep
    Participant

    ok, thanks for your help will try to apply hack using those hooks.

    About the extra free item can u help me out with some example code since I’m kind of lost. I appreciate your help and time.

    #4394
    Soft79
    Keymaster

    No sorry, but if you want us to do custom work please contact us at admin at soft79.nl to request a quote.

    #4395
    numep
    Participant

    thanks for ur time, I found the SOLUTION, but will definitely be hiring soft79 if I need further customization’s, also I recommended your Plugin to a few friends of mine, so be assured you have a good client here. Thanks for ur time.

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