Home > OS >  Custom php code error for Woocommerce Elementor Template with ACF
Custom php code error for Woocommerce Elementor Template with ACF

Time:12-22

I get an 500 error for the Product template I pasted the shortcode.

I created a Shortcode for displaying an video if user has bought the specific product.

The Video URL is from an ACF field.

// Shortcode for Video from ACF
function wpc_elementor_shortcode( $atts ) {
    global $product;
    if ( ! is_user_logged_in() ) return;
    if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
    echo get_field('video_tutorial');
    } 
}
add_shortcode( 'purchase_video_tutorial', 'wpc_elementor_shortcode');

On Front-End it works but within the elementor builder it shows 500 error if shortcode [purchase_video*_*tutorial] is placed.

Debugging showing error for code above:

Uncaught Error: Call to a member function get_id() on null in ..... functions.php:133

What do I need to add to the php code to fix this error for elementor pagebuilder?

CodePudding user response:

Validation means checking that object you are accessing isn't null, like this:

if ( $product != null && wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {

Also, the shortcode should return the string, not print it

echo get_field('video_tutorial');

to

return get_field('video_tutorial');

Also, wc_customer_bought_product, first parameter is customer's email

    $current_user = wp_get_current_user();
    $customer_email = $current_user->email;
    wc_customer_bought_product($customer_email, get_current_user_id(), $product->get_id() )
  • Related