• 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)
  • is_on_sale is a function: is_on_sale(), not a variable: $is_on_sale.

    I’ve not tested the rest of it.

    if( $_product->is_on_sale() == true){

    Thread Starter dustyryan85

    (@dustyryan85)

    Perfect, thanks @lorro and @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.