0

Hi I had created a custom billing field shipping tracking code to add shipping tracking number to the order in the backend and send this info by woocommerce order email to customer automatically returning this value as short code in html. But it doesnt work. What is wrong? Please help.

    /**
     * Add Shortcode 
     */
    function tracking_shortcode( ) {
       $order = new WC_Order( $order_id );
       $shipping_tracking_code = get_post_meta( $order->id, '_shipping_tracking_code', true );
       ob_start();
          return $shipping_tracking_code;
       return ob_get_clean();
    }
    add_shortcode( 'tracking_code', 'tracking_shortcode' );
  • Where is $order_id coming from? It's never set. Also, return ob_get_clean(); is never executed because you already return $shipping_tracking_code on the line above it. – swissspidy Jan 28 '17 at 11:25
1

Point-1: it's not clear where you are getting $order_id from. I'm assuming it's in the Shortcode.

Point-2: You don't need to. use ob_* functions for returning Shortcode replacement content, simply returning is enough.

Assuming other parts of your CODE is correct, below is the possible CODE that should work:

  1. If the shortcode is implemented in theme's functions.php file:

    /**
     * Add Shortcode 
     */
    function tracking_shortcode( $attrs ) {
        $attrs = shortcode_atts( array(
            'order_id' => ''
        ), $attrs );
        if( empty( $attrs['order_id'] ) ) return '';
    
        $order = new WC_Order( $attrs['order_id'] );
        // I've followed your code, but may be even the above line is not needed.
        // may be you can just use $attrs['order_id'] directly to get tracking code
        return get_post_meta( $order->id, '_shipping_tracking_code', true );
    }
    add_shortcode( 'tracking_code', 'tracking_shortcode' );
    
  2. If the shortcode is implemented using a plugin:

    /**
     * Add Shortcode 
     */
    function tracking_shortcode( $attrs ) {
        $attrs = shortcode_atts( array(
            'order_id' => ''
        ), $attrs );
        if( empty( $attrs['order_id'] ) ) return '';
    
        $order = new WC_Order( $attrs['order_id'] );
        return get_post_meta( $order->id, '_shipping_tracking_code', true );
    }
    
    /**
     * Initiate Shortcode 
     */
    function tracking_shortcode_init()
    {
        add_shortcode( 'tracking_code', 'tracking_shortcode' );
    }
    add_action('init', 'tracking_shortcode_init');
    

Learn more about shortcode from WordPress doc.

  • Tried first option entered the first code in the functions.php file, but still the shortcode is not working. Getting no returns in email messages. – anagam2017 Jan 29 '17 at 18:30
  • Tried the second option by entering the second code in the plugin file, still no result. – anagam2017 Jan 29 '17 at 18:30
  • Cant understand why it doesnt display at all – anagam2017 Jan 29 '17 at 18:31
  • Provide your full shortcode. – Fayaz Jan 29 '17 at 21:34
  • I had implemented the shortcode in my custom plugin using the second snippet. but it doesnt work, it returns only [tracking_code]. Where as I am able to get the value of $shipping_tracking_code in another snippet using the following code – anagam2017 Jan 31 '17 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.