1

This function allows me to show Demo Url on the single product page before cart button. But i want to display it somewhere else using a shortcode. Please help me How to do that-

Here is the code:

/**
* Show a Live Demo button on a single product page
*/
function isa_before_add_to_cart_form() {
    global $product;
    $url = get_post_meta( $product->get_id(), '_isa_wc_product_demo_url', true );
    if ( $url ) {
        echo '<p><a href="' . esc_url( $url ) . '" id="livedemo" class="button primary is-shade box-shadow-3 box-shadow-5-hover" target="_blank" rel="noopener">' .
        __( 'Live Demo', 'textdomain') .
        '</a><p>';
    }
}
add_action('woocommerce_before_add_to_cart_form','isa_before_add_to_cart_form');
 
/**
 * Display the Demo URL field in the Product Data metabox
 */
function isa_wc_product_add_demo_url_field() {
    echo '<div class="options_group">';
    woocommerce_wp_text_input( 
        array( 
            'id'          => '_isa_wc_product_demo_url',
            'label'       => __( 'Demo URL', 'textdomain' ), 
            'placeholder' => '',
            'desc_tip'    => 'true',
                'description' => __( 'Set a URL that will be displayed on the product page to link to a demo of this product. Full URL starting with "https://"', 'textdomain' )
        )
    );
    echo '</div>';
}
add_action( 'woocommerce_product_options_general_product_data', 'isa_wc_product_add_demo_url_field' );
 
/**
 * Save the Demo URL field value when the produc is saved
 */
function isa_wc_product_save_demo_url_value( $post_id ) {
    $val = trim( get_post_meta( $post_id, '_isa_wc_product_demo_url', true ) );
    $new = sanitize_text_field( $_POST['_isa_wc_product_demo_url'] );
    if ( $val != $new ) {
        update_post_meta( $post_id, '_isa_wc_product_demo_url', $new );
    }
}
add_action( 'woocommerce_process_product_meta', 'isa_wc_product_save_demo_url_value' ); 

1 Answer 1

-1

You can use the add_shortcode function to do that. You just need to pass your isa_before_add_to_cart_form function as a shortcode like this:

add_shortcode( 'isa_show_link', 'isa_before_add_to_cart_form );

Now you can use the shortcode [isa_show_link] to display it anywhere. It's simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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