• Resolved pprince3145

    (@pprince3145)


    I’ve created a universal snippet using a public PHP function declared in an installed, working plugin but the snippet won’t save because it can’t find it

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @pprince3145,

    That may be happening if the function is defined in the plugin later than the checks we run before activating the snippet.

    I recommend adding a check using function_exists before calling that function as that should also be helpful if, for example, the other plugin is deactivated for any reason.

    If you share the code of the snippet we’re happy to try to provide a clear example of how to add that check.

    Thread Starter pprince3145

    (@pprince3145)

    Thank for your prompt response

    I’ve commented where the unrecognised function is called

    <form id="calcForm" class="calcForm-container"> 
    <label class="calcformLabel">Bike Type:</label>
    <select id="sku" name="sku" form="calcForm">
    <?php

    // Query Posts.

    // Query arguments.
    $query_args = array( 'post_type' => array('product', 'product_variation'),
    'order' => 'ASC',
    'orderby' => 'ID',
    'posts_per_page' => -1, // Get all products
    );

    // Run the query.
    $query = new WP_Query( $query_args );

    if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
    $query->the_post();
    $product = wc_get_product(get_the_ID());
    $sku = $product->get_sku();
    $name = $product->get_name();

    $role_based_price = get_price_of_product($product); // this is the function that cannot be found when the snippet is saved


    if($sku !== "")echo "<option value=\"" . $sku . "|" . $role_based_price . "\">" . $name . "<option>\n";
    }
    }

    // Reset post data.
    wp_reset_postdata();
    ?>
    </select>
    <label class="calcformLabel">Delivery Postcode:</label>
    <input type="text" class="calcformInput" id="delivery_postcode" name="delivery_postcode" placeholder="Enter delivery postcode">
    <button class="calcformlabel" type="button" id="calculate" onClick="calcformSubmit()">Get Quote</button> </form>

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @pprince3145,

    Thank you for sharing your code. I am not familiar with the get_price_of_product function, I presume that is from a WooCommerce extension.

    In any case, you can add a check on that line to prevent the error if the function is not found:

    $role_based_price = function_exists('get_price_of_product') ? get_price_of_product($product) : '';

    If the function exists when the snippet is later executed on the frontend, the value returned will be used, otherwise it will be empty.

    I suggest also double-checking that the function get_price_of_product exists.

    Thread Starter pprince3145

    (@pprince3145)

    sadly that didn’t work

    all that did was allow me to save the snippet. Not finding the function declared in a working plugin is still the issue

    I’m sure the snippet is executing before the plugin declares the function but I have no idea how to resolve that or if it can be

    thank you

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @pprince3145,

    Can you please share a documentation article or reference of where the function get_price_of_product is defined?

    That might help us pinpoint what is causing this issue.

    If you are calling the snippet as a shortcode, for example, the snippet is executed when the shortcode is called so at that point all plugins are definitely loaded.

    Thread Starter pprince3145

    (@pprince3145)

    I am calling the snippet as a shortcode via Elementor

    this where the plugin and function are defined

    this is the function I’m trying to call in the snippet

    I hope that helps, thank you

    Plugin Author Mircea Sandu

    (@gripgrip)

    Hi @pprince3145,

    Thank you for sharing the context. That is a method as part of a class that cannot be used as a function in the way you are trying to use it.

    Functions that can be used the way you are trying to are defined as

     
    function get_price_of_product() {}
    

    In order to use that method, you need to access it in an instance of the class that is part of it.
    I can’t see the name of the class in the screenshot if shared, but the way that would is something like this:

    
    $instance = new PriceClass();
    
    $instance->get_price_of_product( $product );
    

    The above is just an example, you need to find the instance of that class that is likely already loaded and available in a global object or create a new instance yourself in order to use that method.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.