• Resolved ebookdesigns

    (@ebookdesigns)


    Good afternoon,

    Do you have a shortcode to display the price of a product using it’s ID please?

    I was using this code:

    add_shortcode( 'cl_product_price', 'cl_woo_product_price_shortcode' ); 
    /** 
     * Shortcode WooCommerce Product Price. 
     * 
     */ 
    function cl_woo_product_price_shortcode( $atts ) { 
    	 
    	$atts = shortcode_atts( array( 
    		'id' => null 
    	), $atts, 'cl_product_price' ); 
      
    	if ( empty( $atts[ 'id' ] ) ) { 
    		return ''; 
    	} 
      
    	$product = wc_get_product( $atts['id'] ); 
      
    	if ( ! $product ) { 
    		return ''; 
    	} 
      
    	return $product->get_price_html(); 
    }

    However it has recently stopped working and I figured you guys would know how best to create a new shortcode. Thank you so much!

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Stuart Duff – a11n

    (@stuartduff)

    Automattic Happiness Engineer

    Hi @ebookdesigns,

    You can find a full list of all the default shortcodes which are available to be used within the free WooCommerce plugin within this documentation below.

    https://docs.woocommerce.com/document/woocommerce-shortcodes/

    The closest shortcode available in WooCommerce to what you’re looking to display would be the shortcode called Add to Cart. This would however display the price of a product but would also display an add to cart button too.

    https://docs.woocommerce.com/document/woocommerce-shortcodes/#section-19

    If you only wanted to display the price of a product if the custom shortcode code you’re using works for you I’d suggest to continue using that.

    Thread Starter ebookdesigns

    (@ebookdesigns)

    Thanks for your reply. Yeah, as I said, the custom shortcode stopped working ??

    Plugin Support Aashik P – a11n

    (@aashik)

    Hi there!

    Replacing get_price_html with get_price should do the trick.

    add_shortcode( 'cl_product_price', 'cl_woo_product_price_shortcode' ); 
    /** 
     * Shortcode WooCommerce Product Price. 
     * 
     */ 
    function cl_woo_product_price_shortcode( $atts ) { 
    	 
    	$atts = shortcode_atts( array( 
    		'id' => null 
    	), $atts, 'cl_product_price' ); 
      
    	if ( empty( $atts[ 'id' ] ) ) { 
    		return ''; 
    	} 
      
    	$product = wc_get_product( $atts['id'] ); 
      
    	if ( ! $product ) { 
    		return ''; 
    	} 
      
    	return $product->get_price(); 
    }
    Thread Starter ebookdesigns

    (@ebookdesigns)

    Wow, thank you so much for your kind help! Much appreciated ??

    Plugin Support Aashik P – a11n

    (@aashik)

    Wow, thank you so much for your kind help! Much appreciated ??

    Glad that helped!

    I’ll mark this topic as solved for now. Please feel free to open a new topic if you have any other questions.

    Hi!

    I am currently looking for the same thing. How can I adapt this for my product, which variable would I need to change?

    many Thanks!

    Plugin Support Gabriel – a11n

    (@gabrielfuentes)

    Hi @bpcspaul ??

    Please open a new topic so that we can help you there.

    Cheers.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Product Price Shortcode’ is closed to new replies.