Globals Not Loading to use in custom shortcode
-
I’m trying to manipulate the price on a single product page and have it not cache utilizing your plugin. My understanding of it is I need to create my own shortcode for the custom PHP that goes into functions.php, then reference that shortcode in the shortcode generated by this plugin since I can’t add PHP code to the shortcode text area for this plugin. That part is working in testing, the custom shortcode I added to functions.php properly adds content where I tell it to. It however does not add the content I’m looking to actually add. So I can send it basic information and it will display that, but if I try to call any WP or WooCommerce attributes, the global functions return nothing, and the script fails:
global $product; function new_price(){ $attr = esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); $price = $product->get_price_html(); return '<p class=".$attr.">.$price.</p>'; } add_shortcode('n_price', 'new_price');
The most recent bit of code I was tinkering with to try and see what I CAN get to work does return what I want, but obviously it’s doing it based off of a static post ID “108680”.
add_shortcode('n_price', 'new_price'); function no_cache_price($price, $product){ global $post; $product = wc_get_product( 108680 ); $attr = esc_attr( apply_filters( 'woocommerce_product_price_class', 'price' ) ); $price = $product->get_price_html(); return '<p class="'.$attr.'">'.$price.'post '.$post.'post_id: '.$post_id.'</p>'; }
The most recent bit of code does return the price with the get_price_html() utilizing the data from wc_get_product( 108680 ). My goal is to have this just work dynamically with whatever page its on, but no matter where I put different global $post, $woocommerce, $post, etc. it doesn’t pull anything in for those. The return line with the different variables is just to see if anything at all is being defined for $post, $post_id with the different tests I’ve been doing, and nothing has worked to identify the page I’m on. So I’m at a loss as to how/why the script doesn’t know what page it’s on.
- The topic ‘Globals Not Loading to use in custom shortcode’ is closed to new replies.