Forum Replies Created
-
AuthorPosts
-
Heidi MaskelyneParticipant
I had this issue too and added the functionality to
first check POSTCODE
if exists
check first line address & phone
if doesn’t exist then must be another house on same rd so allow coupon.Been using this past year no issues:
Code below 🙂
function has_bought_postcode($search_term,$meta_key){ $var = trim($search_term); if(isset($var) === true && $var === '') { return 0; } else { $orders = get_posts( array( 'numberposts' => 2, // Just two is enough 'meta_key' => $meta_key, 'meta_value' => $search_term, 'post_type' => 'shop_order', 'post_status' => array('wc-processing', 'wc-completed') ) ); // If the inputed billing postcode is not set on a customer order we set the value to TRUE return sizeof($orders); } } /** DONT ALLOW POSTCODE EXCLUDING FIRST ADDRESS LINE :) OR PHONE !!!!!!!!!!!!!!!!!!!!!!!!!! */ add_filter('wjecf_is_first_purchase','check_is_first_purchase',10,1); function check_is_first_purchase( $is_first_purchase ) { global $woocommerce; if ( $is_first_purchase !== false ) { //Can be null. // ...implement your postcode code and set $is_first_purchase to false if an order has been placed on the postcode... if(has_bought_postcode($woocommerce->customer->get_billing_postcode(),'_billing_postcode')>0 || has_bought_postcode($woocommerce->customer->get_shipping_postcode(),'_shipping_postcode')>0){ if(has_bought_postcode($woocommerce->customer->get_billing_address_1(),'_billing_address_1')>0 || has_bought_postcode($woocommerce->customer->get_shipping_address_1(),'_shipping_address_1')>0 || has_bought_postcode($woocommerce->customer->get_billing_phone(),'_billing_phone')>0){ $is_first_purchase = false; } } } return $is_first_purchase; }
Heidi MaskelyneParticipantthank you for your help
for anyone that needs this functionality you can adjust the code below to suit your needs
// discount subscription renewals function discount_subscription_renewals($billing_email){ $query_args = array( 'posts_per_page' => -1, 'post_type' => 'shop_coupon', 'post_status' => 'publish', 'orderby' => array( 'title' => 'ASC' ), 'meta_query' => array( array( 'key' => 'discount_type', 'value' => 'fixed_cart', ), ), ); $coupons = get_posts( $query_args ); $coupon_code = false; $discount = 0; foreach ( $coupons as $coupon ) { $customer_email = array($coupon->customer_email[0]); $amount = $coupon->coupon_amount; $usage_count = $coupon->usage_count; $usage_limit = $coupon->usage_limit; if (in_array($billing_email,$customer_email) && ($usage_count < $usage_limit)){ $coupon_code = $coupon->post_title; break; //$discount += $amount; } } return $coupon_code; } function apply_discount_on_renewal_order_created( $renewal_order, $subscription ) { $order = new WC_Order($renewal_order->get_id() ); $billing_email = $order->get_billing_email(); $coupon_code = discount_subscription_renewals($billing_email); if ($coupon_code){ $order->apply_coupon($coupon_code); //$order->set_total( $order->get_total() - $coupon_amount ); } return $order; } add_filter( 'wcs_renewal_order_created', 'apply_discount_on_renewal_order_created', 10, 2 );
Heidi MaskelyneParticipantHi again ,
I know we can hook into renewals below code which is good but how can I check through all coupons to see if the subscriber has a valid coupon to use ?
function apply_discount_on_renewal_order_created( $renewal_order, $subscription ) { $order = new WC_Order( $renewal_order->get_id() ); $coupon_amount = 25; $order->set_total( $order->get_total() - $coupon_amount ); return $order; } add_filter( 'wcs_renewal_order_created', 'apply_discount_on_renewal_order_created', 10, 2 );
Heidi MaskelyneParticipanti have already tried gettext filter and does not work at all
also the plugin does not work either
i managed to fix by editing error.php in woocommerce templates adding below
if (strpos($message, '" is not yours.') !== false) { $message = $message.'<br><br><b> PLEASE LOGIN INTO YOUR ACCOUNT TO USE THIS COUPON</b>'; //updated error string }?>
Thanks Anyway for your help
Heidi MaskelyneParticipantI have sorted and fixed
$coupon_id= wc_get_coupon_id_by_code('example'); $add_coupon_id = array_merge(explode(',', get_post_meta($coupon_id, '_wjecf_customer_ids', true)), array($user_id)); update_post_meta($coupon_id, '_wjecf_customer_ids',implode(',', $add_coupon_id));
Thanks
Heidi MaskelyneParticipantmy code isnt working as expected can i get some help please?
IT DOES NOT APPEND THE LIST IT REMOVES ALL FROM LIST AND JUST ADDS THE NEW CUSTOMER
Heidi MaskelyneParticipantHi i have figured out myself which works great added to thank you page
for other users you can use below:
$coupons = $order->get_used_coupons(); if(preg_match("/woolcoolreturn/", json_encode($coupons))) { $coupon_id = wc_get_coupon_id_by_code('woolcoolreturn'); $allowed_customer_ids = explode(',', str_replace(get_current_user_id(),'',get_post_meta($coupon_id, '_wjecf_customer_ids', true))); update_post_meta($coupon_id, '_wjecf_customer_ids', implode(',', $allowed_customer_ids)); }
if admin can check that this is correct way of doing then great.
Heidi MaskelyneParticipantyes these are the options i have but i only want percentage discount on certain products
if i have multiple different items in cart the whole cart is discount not just the products its allowed on
if i do percentage discount and exclude the only item its not allowed to discount then if the user adds that to the cart the coupon errors and does not apply unless user removes item from cart.
does this mean i have to add “allowed Categories” ? instead of exclude categories ?
Heidi MaskelyneParticipantOne last question
First purchase is this only for customer or both (guest) ?
Since I add “customer role” in coupon settings but unsure if I need to ?
Heidi MaskelyneParticipantDid you mean ?
woocommerce_coupon_error
filterHeidi MaskelyneParticipanttry this below add it to your functions.php
change the $coupon_id to coupon you created
add_action('woocommerce_applied_coupon', 'remove_coupon_from_cart'); function remove_coupon_from_cart( ) { global $woocommerce; $coupon_id = 'MYCOUPON'; $min_order= 75; $max_weight = 10; $cart_weight =$woocommerce->cart->cart_contents_weight; $cart_total = $woocommerce->cart->get_cart_total(); if ($cart_weight > $max_weight || $cart_total < $min_order){ $woocommerce->cart->remove_coupon( $coupon_id ); }
Heidi MaskelyneParticipantDont worry i have it working and didnt realise you select the item you want and click update basket
Thanks
-
AuthorPosts