• 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' );
Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    you have two returns there. that’s not right. I think you mean to use “echo” for the first one.

    Thread Starter anagam2016

    (@anagam2016)

    Hi,
    Thanks for your response. I tried it, it doesn’t work anyway
    I am trying to get this displayed with these three options. In all cases no result

    1:<?php _e( do_shortcode( ‘[tracking_code]’ ) ); ?>
    2:[tracking_code]
    3:<?php echo $shipping_tracking_code; ?>

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    I’d write it as

    function tracking_shortcode( ) {
           $order = new WC_Order( $order_id );
           $shipping_tracking_code = get_post_meta( $order->id, '_shipping_tracking_code', true );
           return "Your shipping code is: " . $shipping_tracking_code;
        }
        add_shortcode( 'tracking_code', 'tracking_shortcode' );

    Does whatever you’re using to send the email handle shortcodes?

    Thread Starter anagam2016

    (@anagam2016)

    I tried this.
    Now it returns only
    ‘Your shipping code is:,
    The value of $shipping_tracking_code is not returned.
    Btw i m trying to get this in woocommerce email

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    The value of $shipping_tracking_code is not returned.

    So that indicates that there’s something wrong with your call to get_post_meta. That’s where you need to focus your attention or on the previous call, which may not be returning anything useful in $order.

    Moderator bcworkz

    (@bcworkz)

    Email content does not get echoed, it is compiled into a variable and included in wp_mail() parameters. Any solution that uses a variant of echo will fail. What you need to do is add your content to that variable that contains the other mail content. I’m pretty sure Woocommerce provides a filter for this, but I’m not certain about what it’s called.

    It can be found by digging through WC code, which can be a frustrating, though informative process. If you ask at the dedicated WC support forum someone should be able to tell you.

    BTW, I know you were simply fishing for a solution, but your use of the _e() translation function is so wrong in so many ways it makes me cringe ?? I mention this not to be critical, but for the sake of anyone else reading who doesn’t know better.

    Thread Starter anagam2016

    (@anagam2016)

    <?php
    /**
     * Add Promo code text to the Order emails
     */
    
    add_action( 'woocommerce_email_before_order_table', 'add_tracking_code', 20);
    
    function add_tracking_code( $order, $is_admin_email ) {
              $shipping_tracking_code = get_post_meta( $order->id, '_shipping_tracking_code', true );
              $url = 'https://www.rapido.bg/information/tracking-tovaritelnitsa';
              $order = new WC_Order( $order_id );
              if($order->status === 'shipped' ) {	  
              ob_start(); 
    	  ?>
    <h3 style="text-align: center;"><?php _e( 'Your order tracking no', 'my-plugin' ); ?></h3>
    <div>
    <p style="text-align: justify;"><?php _e( 'Your order has been shipped from our warehouse thru courier service Rapido. Waybill no is: &nbsp; ', 'my-plugin' ); ?><font size="3" color="red"><b><?php echo $shipping_tracking_code; ?></b></font></p>
    <p><a href="<?php echo $url; ?>" ><?php _e( 'Track your shipment', 'my-plugin' ); ?></a></p>
    </div>
    	  <?php
    	  echo ob_get_clean();  
              } else {
              echo '&nbsp;';
              }
    }
    
    ?>

    Above is the snippet which works well in customer note email

    Can’t understand why it doesn’t work here, I mean cannot get the valipue of $shipping_tracking_code

    Thread Starter anagam2016

    (@anagam2016)

    @bcworkz
    Hi, thanks, will do the home work.
    Sorry, I am just a beginner with no practical knowledge of coding.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘wordpress custom shortcode not working’ is closed to new replies.