Repository › Forums › Support › WooCommerce Extended Coupon Features PRO › Stock is not reduced when free product is not selected
- This topic has 3 replies, 2 voices, and was last updated 4 years, 3 months ago by Soft79.
-
AuthorPosts
-
June 13, 2019 at 7:08 pm #6077Yulia KolesnikParticipant
Hi, despite that plugin functions correctly and does amazing things I am having an issue:
Free products that are to be chosen by customer are not deducted from the stock when gift option is not chosen.
For example: I give an option to choose 1 of 4 shirts when cart is over 50$, if customer forgets to choose his gift, coupon still applies to his order, but without a free product gift, therefore that gift-product quantity is no being reduced in stock. Is there a way to display cart error message with “Please choose your gift” reminder before checkout?June 14, 2019 at 7:40 am #6097Soft79KeymasterThis can be done by pasting a small snippet in your child theme’s functions.php:
add_action( 'woocommerce_before_checkout_process', function() { if ( ! is_callable( 'WJECF_API' ) ) return; //Don't checkout if no free gift selected foreach( WC()->cart->get_applied_coupons() as $coupon_code ) { $coupon = new WC_Coupon( $coupon_code ); //Must the user select a free product? if ( ! WJECF_API()->must_select_free_product( $coupon ) ) { continue; } $session_selected_products = WJECF_API()->get_session_selected_products( $coupon_code ); $selected_free_products = WJECF_API()->count_selected_products( $coupon, $session_selected_products ); if ( empty( $selected_free_products ) ) { wc_add_notice( __('Please select your free gift.', 'woocommerce-jos-autocoupon'), 'error' ); return; } } }, 20 );
I had to make some untested changes to this snippet. Please let me know if it has issues.
June 14, 2019 at 11:04 am #6101Yulia KolesnikParticipantIt does show free products on the checkout page, but:
1) no error message when gift is not selected
2) and most importantly – doesn’t proceed to PayPal even if gift is selectedJune 14, 2019 at 11:31 am #6103Soft79KeymasterI see, the method unfortunately needs access to a private method. This is a quick and dirty fix:
add_action( 'woocommerce_before_checkout_process', function() { if ( ! is_callable( 'WJECF' ) ) return; //Don't checkout if no free gift selected foreach( WC()->cart->get_applied_coupons() as $coupon_code ) { $coupon = new WC_Coupon( $coupon_code ); //Must the user select a free product? if ( ! WJECF()->get_plugin('pro-free-products')->must_select_free_product( $coupon ) ) { continue; } $fp = WJECF()->get_plugin('pro-free-products'); $session_selected_products = $fp->get_session_selected_products( $coupon_code ); $reflector = new ReflectionObject( $fp ); $method = $reflector->getMethod('count_selected_products'); $method->setAccessible(true); $selected_free_products = $method->invoke($fp, $coupon, $session_selected_products ); if ( empty( $selected_free_products ) ) { wc_add_notice( __('Please select your free gift.', 'woocommerce-jos-autocoupon'), 'error' ); return; } } }, 20 );
I’m thinking about adding this functionality into the core plugin. But for now you can use this (dirty) solution.
-
AuthorPosts
- You must be logged in to reply to this topic.