• Resolved biohazardousx

    (@biohazardousx)


    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.

Viewing 1 replies (of 1 total)
  • Plugin Author Jose

    (@giuse)

    Hi @biohazardousx,

    unfortunately, CNC is not intended for this kind of advanced use.
    It works if you just add the content without touching any code.

    Your code will never work because CNC calls your shortcode via Ajax, and during the Ajax call the global variable $product will not exist. It exists only when you load the page where you add the CNC shortcode, not when that page calls Ajax.

    What you are trying to do will be possible with the PRO version that will come in the future.

    Write me a message using my contact form, and I will provide you with a PRO beta version. Beta testers will have a lifetime license for free when the PRO version is public.

    I will close this thread because with the free version what you are trying to achieve is not possible. If interested, let’s continue via email.

    Have a great day!

    Jose


Viewing 1 replies (of 1 total)
  • The topic ‘Globals Not Loading to use in custom shortcode’ is closed to new replies.