Topic Resolution: Resolved
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #10906
    Heidi Maskelyne
    Participant

    Thank you for add filter for first purchase on the dev build
    wjecf_is_first_purchase

    it works brilliant !!!!
    code below for other users if they want to use checks billing postcode and shipping postcode to check if user has purchased before on another account:

    I have one question how can I override the coupon error message programmatically since at present I use “you need an account or you have purchased before” which i set up in the Miscellaneous >> Custom error message, This is too vague so i want to return a true error message.

    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')
            ) );
            
            return sizeof($orders); 
        
        }
    
    }      
    
    add_filter('wjecf_is_first_purchase','check_is_first_purchase',10,1);
    function check_is_first_purchase( $is_first_purchase ) {
    	global $woocommerce;
    	
    	$coupon_id = 'CHANGEME' ;	
    	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){
    			$is_first_purchase = false;
    			$woocommerce->cart->remove_coupon(sanitize_text_field($coupon_id));
    		}
    	}
    	
    		
        return $is_first_purchase;	
    }
    #10923
    Soft79
    Keymaster

    You can remove the $woocommerce->cart->remove_coupon(sanitize_text_field($coupon_id)); from your snippet.

    The default message is “Sorry, coupon “%s” is only valid on your first purchase”. If you want to make this message dynamic, you can use the woocommerce_coupon_message filter.

    #10932
    Heidi Maskelyne
    Participant

    Did you mean ?

    woocommerce_coupon_error filter

    #10933
    Soft79
    Keymaster

    Oh yeah, I think you’re right

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

    #10961
    Soft79
    Keymaster

    Is for both.

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