Home > Software engineering >  Add Shortcode to woocommerce Product Page
Add Shortcode to woocommerce Product Page

Time:11-16

I am trying to create a cross sell slider on my product pages in Woocommerce. I have a plugin that generates a shortcode -

[gpsc_slider_carousel id="8307"]

I am trying to add it to the content-single-product page as I want to to display before the product description and review information.

i am using the following but it doesn't work. Nothing appears on the product page.

<?php echo do_shortcode("[gpsc_slider_carousel id="8307"]"); ?> 

The entire code looks like this -

defined( 'ABSPATH' ) || exit;

/**
 * Hook: woocommerce_before_single_product.
 *
 * @hooked wc_print_notices - 10
 */
do_action( 'woocommerce_before_single_product' );

if ( post_password_required() ) {
    echo get_the_password_form(); // WPCS: XSS ok.
    return;
}
?>
<div id="product-<?php the_ID(); ?>" <?php wc_product_class(); ?>>

    <?php
        /**
         * Hook: woocommerce_before_single_product_summary.
         *
         * @hooked woocommerce_show_product_sale_flash - 10
         * @hooked woocommerce_show_product_images - 20
         */
        do_action( 'woocommerce_before_single_product_summary' );
    ?>

    <div >
        <?php
            /**
             * Hook: woocommerce_single_product_summary.
             *
             * @hooked woocommerce_template_single_title - 5
             * @hooked woocommerce_template_single_rating - 10
             * @hooked woocommerce_template_single_price - 10
             * @hooked woocommerce_template_single_excerpt - 20
             * @hooked woocommerce_template_single_add_to_cart - 30
             * @hooked woocommerce_template_single_meta - 40
             * @hooked woocommerce_template_single_sharing - 50
             * @hooked WC_Structured_Data::generate_product_data() - 60
             */
            do_action( 'woocommerce_single_product_summary' );
        ?>
    </div>
    
<?php echo do_shortcode("[gpsc_slider_carousel id="8307"]"); ?> 
    
    <?php
        /**
         * Hook: woocommerce_after_single_product_summary.
         *
         * @hooked woocommerce_output_product_data_tabs - 10
         * @hooked woocommerce_upsell_display - 15
         * @hooked woocommerce_output_related_products - 20
         */
        do_action( 'woocommerce_after_single_product_summary' );
    ?>
</div>

<?php do_action( 'woocommerce_after_single_product' ); ?>

What am I doing wrong and also, am I putting it in the right place if I want it before the description? Thanks

CodePudding user response:

you should use single ' in do_shortcode().

Try using this :-

<?php echo do_shortcode('[gpsc_slider_carousel id="8307"]'); ?> 
  • Related