0

I'm trying to create a custom shortcode that will allow me to input custom order field data into an auto generated outbound email template via the Custom Order Status for WooCommerce plugin.

My understanding of PHP is limited at best but I came up with is a modified version of the below code that came from a similar question previously answered by Krzysiek Dróżdż:

function wcal_abandoned_cart_id_shortcode_callback( $atts ) {
    $atts = shortcode_atts( array(
        'post_id' => get_the_ID(),
    ), $atts, 'wcal_abandoned_cart_id' );

    return get_post_meta( $atts['post_id'], 'wcal_abandoned_cart_id', true );
}
add_shortcode( 'wcal_abandoned_cart_id', 'wcal_abandoned_cart_id_shortcode_callback' );

Wordpress and the plugin seem to recognize the shortcode [wcal_abandoned_cart_id] however the output value is blank. The value that should return for this specific order is "428". I'm hoping someone can help point me in the right direction.

Thanks in advance.

0

The shortcode callback function should always echo its output, not return it. Your code should be something like this:

function wcal_abandoned_cart_id_shortcode_callback( $atts ) {
    $atts = shortcode_atts( array(
        'post_id' => get_the_ID(),
    ), $atts, 'wcal_abandoned_cart_id' );

    echo get_post_meta( $atts['post_id'], 'wcal_abandoned_cart_id', true );
}
add_shortcode( 'wcal_abandoned_cart_id', 'wcal_abandoned_cart_id_shortcode_callback' );

If the output of your callback was HTML, besides echoing, you could just write your HTML outside of the PHP tag.

0

Get the ID using this:

add_shortcode('wcal_abandoned_cart_id',function(){
    return get_the_ID();
});

Use the shortcode [wcal_abandoned_cart_id]

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.