Forum Replies Created

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • in reply to: Coupon on first purchase #17226
    Heidi Maskelyne
    Participant

    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;	
    }
    in reply to: Apply Coupon To Exisitng Subscriber #15618
    Heidi Maskelyne
    Participant

    thank 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 );
    in reply to: Apply Coupon To Exisitng Subscriber #15615
    Heidi Maskelyne
    Participant

    Hi 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 );
    in reply to: Overriding Error Message #15263
    Heidi Maskelyne
    Participant

    i 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

    in reply to: Add Customer to Coupon On The Fly #15174
    Heidi Maskelyne
    Participant

    I 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

    in reply to: Add Customer to Coupon On The Fly #15173
    Heidi Maskelyne
    Participant

    my 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

    in reply to: Remove Customer Programically From Allowed List #13017
    Heidi Maskelyne
    Participant

    Hi 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.

    in reply to: How To do Percentage Discount on Certain Products #11869
    Heidi Maskelyne
    Participant

    yes these are the options i have but i only want percentage discount on certain products

    Options I Have:

    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 ?

    in reply to: First Purchase #10935
    Heidi Maskelyne
    Participant

    One 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 ?

    in reply to: First Purchase #10932
    Heidi Maskelyne
    Participant

    Did you mean ?

    woocommerce_coupon_error filter

    in reply to: How to use weight as a condition #10904
    Heidi Maskelyne
    Participant

    try 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 );
        }
    in reply to: Free Product Shows twice When using multi checkout #10332
    Heidi Maskelyne
    Participant

    Dont worry i have it working and didnt realise you select the item you want and click update basket

    Thanks

Viewing 12 posts - 1 through 12 (of 12 total)