Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › how to select 2 free products on products with 2 quantity
- This topic has 10 replies, 2 voices, and was last updated 4 years, 10 months ago by numep.
-
AuthorPosts
-
December 7, 2018 at 3:40 am #4385numepParticipant
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.
December 7, 2018 at 12:33 pm #4386Soft79KeymasterYou 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.
December 7, 2018 at 12:34 pm #4387numepParticipantok 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 );
December 7, 2018 at 12:41 pm #4388Soft79KeymasterHi,
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/ )
December 7, 2018 at 12:43 pm #4389Soft79KeymasterThe 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.
December 7, 2018 at 12:48 pm #4390numepParticipantyeah 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 ) {
.December 7, 2018 at 12:56 pm #4391numepParticipantOk 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 🙁
December 7, 2018 at 1:01 pm #4392Soft79KeymasterYou 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.
December 7, 2018 at 1:16 pm #4393numepParticipantok, 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.
December 7, 2018 at 1:22 pm #4394Soft79KeymasterNo sorry, but if you want us to do custom work please contact us at admin at soft79.nl to request a quote.
December 7, 2018 at 1:54 pm #4395numepParticipantthanks 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.
-
AuthorPosts
- You must be logged in to reply to this topic.