Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

WooCommerce Crashing Based On Custom Fuction

This is a follow-on question to
https://www.experts-exchange.com/questions/29145432/Need-WooCommerce-Product-Short-Code-That-Uses-Thumbnail.html?headerLink=workspace_answered_questions

Open in new window


Here is my code
add_shortcode( 'cs_product_image', 'cs_product_image_callback');
function cs_product_image_callback($atts = []) {
    $atts = shortcode_atts( array(
        'id' => null,
    ), $atts, 'cs_product_image' );

    $product = new WC_product( $atts['id'] );
    $product_img = wp_get_attachment_image( $product->get_image_id(), 'woocommerce_gallery_thumbnail' );
    return $product_img;
}

Open in new window


For some reason, this is causing a fatal error.  Here are the log details:

2019-05-29T14:18:45+00:00 CRITICAL Uncaught Exception: Invalid product. in /data/web/theherbsplace.com/wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php:163
Stack trace:
#0 /data/web/theherbsplace.com/wp-content/plugins/woocommerce/includes/class-wc-data-store.php(159): WC_Product_Data_Store_CPT->read(Object(WC_Product))
#1 /data/web/theherbsplace.com/wp-content/plugins/woocommerce/includes/abstracts/abstract-wc-product.php(135): WC_Data_Store->read(Object(WC_Product))
#2 /data/web/theherbsplace.com/wp-content/themes/flatsome-child/functions.php(411): WC_Product->__construct('1440')
#3 /data/web/theherbsplace.com/wp-includes/shortcodes.php(325): cs_product_image_callback(Array, '', 'cs_product_imag...')
#4 [internal function]: do_shortcode_tag(Array)
#5 /data/web/theherbsplace.com/wp-includes/shortcodes.php(199): preg_replace_callback('/\\[(\\[?)(cs_pro...', 'do_shortcode_ta...', '[cs_product_ima...')
#6 /data/web/theherbsplace.com/wp-content/plugins/wordpress-seo/admin/ajax/class-shortcode-filter.php(35): in /data/web/theherbsplace.com/wp-content/plugins/woocommerce/includes/data-stores/class-wc-product-data-store-cpt.php on line 163

Open in new window


I don't understand how to fix the problem so please help.

Thanks,
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris is correct.

It appears you've defined a malformed shortcode.

Either...

1) Missing id.

2) Incorrect id.

3) Incorrect additional/unrecognized attr (like id=) passed.

Take a look at the related page/post + verify your short code is correctly formed.

If you short code is correctly formed, next step will be to ensure all your WooCommerce code is updated, then if problem persists, open a ticket with WooCommerce support.
Avatar of sharingsunshine

ASKER

Chris, I had transposed a number.  So thanks for letting me know what was going on.

David, I tried WooCommerce first and they said they can't help with custom coding issues.
No worries. Glad I could help.

It might be worth adding some error checking into the custom shortcode to make sure any errors are handled more elegantly than just crashing out.
I agree, I was thinking the same thing.