PluginUs.Net - Business Tools for WooCommerce and WordPress

[realize your idea - make your dreams come true]

Support Forum

You need to log-in to create request (topic) to the support

Show custom price shortcode

The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday. If you have problems with registration ask help on contact us page please
If you not got email within 24~36 business hours, firstly check your spam box, and if no any email from the support there - back to the forum and read answer here. DO NOT ANSWER ON EMAILS [noreply@pluginus.net] FROM THE FORUM!! Emails are just for your info, all answers should be published only here.
The support doesn work on Saturdays and Sundays, so some Friday requests can be answered on Monday.

Hi

I'm currently using [woocs_show_custom_price value=xxx], which works great.

However, I now use that shortcode in multiple places with the same value provided. Which means if that value needs to be updated I need to remember to update the shortcode multiple times around the site, and I'm worried I'll forget one.

Is it possible that instead of a raw value it can also accept a woo product id instead (e.g. id=xxxx)? Then if I update the price in the product itself, the shortcodes used around the site will automatically also stay correct? (or is there another function or shortcode that does that I am unaware of)?

As an aside, I actually have my own shortcode that pulls the price from a woo product (as a raw integer, e.g. 3987), and I tried to nest that inside your shortcode, but that didn't work :-(

e.g. [woocs_show_custom_price value=[adz_product_price id="8167"][/woocs_show_custom_price]

Thanks in advance.

 

Hello

https://c2n.me/48UPMqb.png - of course this won't work

You need code customization

EXAMPLE:

<?php

$some_price = do_shortcode("[adz_product_price id="8167"]");

echo  do_shortcode(" [woocs_show_custom_price value=".$some_price."]");

?>

Thanks for the reply.

So, presumably, I could wrap something like that sample code you supplied inside another manual shortcode - which I can then place around the site?

Hello

I  think  yes - https://www.wpbeginner.com/wp-tutorials/how-to-add-a-shortcode-in-wordpress/

Ok - sorted it out myself in the end. Thanks for pointing me in the right direction!  :-)

Code below in case you want to show it in your codex in the future.

/**
 * Shortcode for WooCommerce Product Price (to insert in various pages)
 * E.G. [adz_product_price id=8167] - where id is a woo product id.
 */
add_shortcode( 'adz_product_price', 'adz_woo_product_price_shortcode' );
function adz_woo_product_price_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'id' => null
    ), $atts, 'adz_product_price' );

    if ( empty( $atts[ 'id' ] ) ) {
        return '';
    }

    $product_price = wc_get_product( $atts['id'] );

    if ( ! $product_price ) {
        return '';
    }

    $unrendered_price = $product_price->get_price();    // Price as plain number
    return do_shortcode( "[woocs_show_custom_price value=". $unrendered_price ."]" );
}

Hello

Ok! Thank you  for  cooperation