• Hi,

    I set up a snippet to make a change to a section in My Account. It had been working fine, but I noticed an error and realized that the snipped had been deactivated. I’m getting this error message:

    We encountered an error activating your snippet, please check the syntax and try again. Error message: Call to a member function get_downloadable_products() on null

    I’m not a programmer, so I’m not sure what this means! The snippet is used to change the wording on the “Downloads” section of My Account. I changed the tab from “Downloads” to “Gift Certificates” and created this snippet to change the message that shows up when no gift certificates are available.

    What does the error message mean, and, why did it work for weeks and then suddenly deactivate?

    <?php
    
    if ( ! defined( 'ABSPATH' ) ) {
    exit;
    } $downloads = WC()->customer->get_downloadable_products();
    $has_downloads = (bool) $downloads; do_action( 'woocommerce_before_account_downloads', $has_downloads ); ?> <?php do_action( 'woocommerce_before_available_downloads' ); ?> <?php do_action( 'woocommerce_available_downloads', $downloads ); ?> <?php do_action( 'woocommerce_after_available_downloads' ); ?> <?php $wp_button_class = wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : ''; wc_print_notice( esc_html__( 'No Gift Certificates available.', 'woocommerce' ) . ' <a class="button wc-forward' . esc_attr( $wp_button_class ) . '" href="' . esc_url( apply_filters( 'woocommerce_return_to_shop_redirect', wc_get_page_permalink( 'shop' ) ) ) . '">' . esc_html__( 'Browse products', 'woocommerce' ) . '</a>', 'notice' ); // phpcs:ignore WooCommerce.Commenting.CommentHooks.MissingHookComment ?> <?php do_action( 'woocommerce_after_account_downloads', $has_downloads );

    Thanks so much!

    Nan

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

    (@gripgrip)

    Hi Nan,

    The error in this case is caused by trying to call get_downloadable_products on the WC()->customer object without first checking if WC()->customer is set. You might also be executing the snippet too early.

    You can wrap the code in a check like

    if ( ! is_null(WC()->customer) ) {
    // Your code here
    }

    You can also try limiting execution of the snippet just for logged-in users – that could also prevent the error from being thrown.

    Thread Starter nanetteg

    (@nanetteg)

    Hi Mircea,

    Thank you for this! I first tried limiting the execution and that didn’t work, so I added the check and didn’t get the error. However, it’s not executing the snippet like before. I’m still getting the original message of: “No downloads available yet.”

    I will keep digging….

    Thanks!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Syntax error activating snippet’ is closed to new replies.