• Resolved mparsons501979

    (@mparsons501979)


    Hi, is it possible to have a code or note appear on the packing slip document that signifies whether the customer is new or whether they are a returning customer?? My client wants to be able to easily identify this from the slip so they know to add an additional leaflet to their delivery.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Contributor kluver

    (@kluver)

    Hi @mparsons501979,

    You can add custom text to the packing slip by using one of our available action hooks. To check if the text has to be displayed you can use the helper function from this Stack Overflow answer that checks if the user already has made a purchase before: https://stackoverflow.com/questions/38874189/checking-if-customer-has-already-bought-something-in-woocommerce/46216073#46216073

    Thread Starter mparsons501979

    (@mparsons501979)

    @kluver many thanks for your reply. I’ve not learnt php yet so this is sadly out of my skillset. Will see if I can find some coding assistance from the community.

    Plugin Contributor Yordan Soares

    (@yordansoares)

    Hi @mparsons501979,

    Try adding this code snippet to your site:

    /**
     * Show if it's a new customer on the invoice
     */
    add_action( 'wpo_wcpdf_after_order_data', 'wpo_wcpdf_profit_amount', 10, 2 );
    function wpo_wcpdf_profit_amount($template_type, $order) {
    	if ($template_type == 'packing-slip') {
    		if( !empty( $user_id = $order->get_user_id() ) ){
    			$value = $user_id;
    		} else {
    			$value = $order->get_billing_email();
    		}
    		if( has_bought( $value ) ) {
    		?>
    		<span style="font-weight:bold;text-transform:uppercase">Returning Customer</span>
    		<?php
    		} else {
    		?>
    		<span style="font-weight:bold;text-transform:uppercase">New Customer</span>
    		<?php
    		}
    	}
    }
    
    function has_bought( $value = 0 ) {
        if ( ! is_user_logged_in() && $value === 0 ) {
            return false;
        }
    
        global $wpdb;
        
        // Based on user ID (registered users)
        if ( is_numeric( $value) ) { 
            $meta_key   = '_customer_user';
            $meta_value = $value == 0 ? (int) get_current_user_id() : (int) $value;
        } 
        // Based on billing email (Guest users)
        else { 
            $meta_key   = '_billing_email';
            $meta_value = sanitize_email( $value );
        }
        
        $paid_order_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
    
        $count = $wpdb->get_var( $wpdb->prepare("
            SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
            INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
            WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $paid_order_statuses ) . "' )
            AND p.post_type LIKE 'shop_order'
            AND pm.meta_key = '%s'
            AND pm.meta_value = %s
            LIMIT 1
        ", $meta_key, $meta_value ) );
    
        // Return a boolean value based on orders count
        return $count > 1 ? true : false;
    }

    If you haven’t worked with code snippets (actions/filters) or functions.php before, read this guide: How to use filters

    Let me know if it worked!

    Thread Starter mparsons501979

    (@mparsons501979)

    @yordansoares you legend! That worked for a returning customer. Just need to test for a new customer but no reason why it shouldn’t work. Amazing, thank you so much for this!

    Plugin Contributor Yordan Soares

    (@yordansoares)

    I’m glad to know that it worked!

    If you don’t mind and have the time, do you think you could leave us a review?

    Thanks in advance and all the best with your store!

    Thread Starter mparsons501979

    (@mparsons501979)

    @yordansoares review has been left. I really appreciate your help on this!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Custom Field on Packing Slip’ is closed to new replies.