Title: Need help with custom shortcode
Last modified: January 27, 2017

---

# Need help with custom shortcode

 *  [dustyryan85](https://wordpress.org/support/users/dustyryan85/)
 * (@dustyryan85)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/need-help-with-custom-shortcode/)
 * I am trying to make a shortcode to use on a pricing table that automatically 
   updates the price. I have this so far…
 *     ```
       //DISPLAY PRODUCT PRICE SHORTCODE
       function display_price( $atts ) {
           $atts = shortcode_atts( array('id' => null,), $atts, 'bartag' );
       	$_product = wc_get_product( $atts['id'] );
   
           if( $is_on_sale == true){
       		 $html = "<span style=\"color:grey; font-size:.6em;\"><s>" . "$" . $_product->get_variation_regular_price() . "</s></span> " . $_product->get_variation_sale_price() . "<span style=\"font-size:0.6em;\">/mo</span>";
       		 return $html;
       	} else {
       		$html = $_product->get_variation_regular_price() . "<span style=\"font-size:0.6em;\">/mo</span>"; 
       		return $html;
       	}
       }
   
       add_shortcode( 'woocommerce_price', 'display_price' );
       ```
   
 * I cant get it to work. It shows else no matter if its on sale or not.

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * (@lorro)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/need-help-with-custom-shortcode/#post-8704355)
 * is_on_sale is a function: is_on_sale(), not a variable: $is_on_sale.
 * I’ve not tested the rest of it.
 *  [kursora](https://wordpress.org/support/users/kursora/)
 * (@kursora)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/need-help-with-custom-shortcode/#post-8705233)
 * `if( $_product->is_on_sale() == true){`
 *  Thread Starter [dustyryan85](https://wordpress.org/support/users/dustyryan85/)
 * (@dustyryan85)
 * [9 years, 4 months ago](https://wordpress.org/support/topic/need-help-with-custom-shortcode/#post-8707516)
 * Perfect, thanks [@lorro](https://wordpress.org/support/users/lorro/) and [@kursora](https://wordpress.org/support/users/kursora/)!
 * The only thing I’m missing now is to make it work well with variable products.
   
   Would you be willing to show me how to add variation IDs to the shortcode args??
   Took me forever just to get the id arg to work, lol, I’m learning. I would REALLY
   appreciate it!!
 * Ex. [woocommerce_price id=”123″ variation=”456″]

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Need help with custom shortcode’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [price](https://wordpress.org/support/topic-tag/price/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)

 * 3 replies
 * 3 participants
 * Last reply from: [dustyryan85](https://wordpress.org/support/users/dustyryan85/)
 * Last activity: [9 years, 4 months ago](https://wordpress.org/support/topic/need-help-with-custom-shortcode/#post-8707516)
 * Status: not resolved