Shortcode prevents Cornerstone from saving

Hi there,

i cant save my Cornerstone Page when the Shortcode [ppm] is implemented.
The Shortcode outputs a function i added manually to the funtions.php.

It includes the following Code:

 function price_per_month() {

    global $product;
    $price = $product->get_price();
    $months = 12;

    $price_per_month = ($price / $months);
    $total_per_month = round($price_per_month, 2);

    return "
    <div id='ppm-container'>
        <div id='ppm-text'>
            oder nur $total_per_month EUR pro Monat *
        </div>
    </div>
    ";
}

add_shortcode( 'ppm', 'price_per_month' );

Can somebody tell me why this is happening?

Hi @partnerundsoehne,

Thanks for reaching out.
It seems that the global $product is not pointing to the product and for that reason, the method of the product object like get_price is causing the error, which blocks the saving.
Please replace the global $product; with the following line of code;

$product = wc_get_product( $post->ID ); // or you can mention and specific product id in place of $post->ID

Remember that the above code will work if copied as it is and don’t conflict with any existing style.
Please note that the code provided serves only as a guide to help you get started custom coding on your own if there’s no option offered in our theme or the products we bundle.
We do not provide support for custom codes that means we can’t fix it in case it conflicts with something on your site nor will we enhance it.

Thanks

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.