1

I searched everywhere and could not find a shortcode for the product content area of woocommerce, it's available in elementor as a widget but what about a shortcode ?

  • Does [this answer](https://stackoverflow.com/a/68976332/15040627) help you? Also there is a link to the woocommerce product shortcodes. – Ruvee Sep 01 '21 at 05:12

1 Answers1

2

You have to create product content shortcode by product id.

Now I have created a shortcode for you like this:

function woo_product_details($atts = array()){
   if(isset($atts['product_id']) && !empty($atts['product_id'])){
        $html = '';
        $product_id                = $atts['product_id'];
        $product                   = wc_get_product($product_id);

        $product_name              = $product->get_name();
        $product_slug              = $product->get_slug();
        //$product_sku               = $product->get_sku();
        //$product_description       = $product->get_description();
        $product_short_description = $product->get_short_description(); 
        //$product_price             = $product->get_price();
        $product_formated_price    = $product->get_price_html();
        //$product_regular_price     = $product->get_regular_price();
        //$product_sale_price        = $product->get_sale_price();  
        $product_image= wp_get_attachment_image_src( get_post_thumbnail_id($product_id), 'single-post-thumbnail' );
        
        $html .= '<div class="card">
            <img src="'.$product_image[0].'" alt="'.$product_name.'" data-id="'.$product_id.'" style="width:100%">
            <h1>'.$product_name.'</h1>
            <p class="price">'.$product_formated_price.'</p>
            <p>'.$product_short_description.'</p>
            <p><a href="'.site_url().'?add-to-cart='.$product_id.'&quantity=1">Add to Cart</a></p>
        </div>';
        return $html;
    }
}
add_shortcode('product_detail', 'woo_product_details');

You can add/ update HTML and product info accordingly, If you want to get another product variable to set/change from an existing function show should check https://www.businessbloomer.com/woocommerce-easily-get-product-info-title-sku-desc-product-object/ and copy page accordingly on given shortcode function.

How to User shortcode

a) You can call directly on your PHP page like this:

<?php echo do_shortcode('[product_detail product_id = '1002']'); ?>

b) You can call on your post and page from admin panel like this:

[product_detail product_id = '1002']

Where 1002 is product id

Rajeev Singh
  • 1,724
  • 1
  • 6
  • 23