• Resolved bvedgie

    (@bvedgie)


    Hello all,

    It’s probably a syntax issue, but I got this while enabling the following snippet: ”Trying to get property ‘slug’ of non-object”.

    It’s for hiding product cats and works fine but there is this error in logs.
    Can you help?

    add_filter( ‘get_terms’, ‘filter_get_terms’, 10, 3 );
    
    function filter_get_terms( $terms, $taxonomies, $args ) {
    $new_terms = [];
    
    // if a product category and on the shop page
    
       if ( ! is_admin() ) {
          foreach ( $terms as $term ) {
             if ( ! in_array( $term->slug, [ ‘hors-saison’, ‘non-classe’ ] ) ) {
              $new_terms[] = $term;
             }
           }
        $terms = $new_terms;
        }
    return $terms;
    }

    Thanks
    Best regards

Viewing 5 replies - 1 through 5 (of 5 total)
  • Anonymous User

    (@anonymized-20801613)

    The https://phpcodechecker.com/ says No issues found – with your code. What does the error log says? Which PHP version are you using?

    Plugin Author Néstor Soriano Vilchez

    (@konamiman)

    Hi @bvedgie. The error means that the $term variable that you are processing in the loop isn’t actually an instance of WP_Term, and thus trying to access $term->slug fails.

    Without further investigation I can’t say why is this happening (you shouldn’t get anything but instances of WP_Term in that loop), but if the snippet is working and all you want is to get rid of the error message, you can do so by modifying the condition with an extra check, so from this:

    if ( ! in_array(...

    to this:

    if ( ( $term instanceof WP_Term ) && ! in_array(...

    Please let me know if that works for you.

    Thread Starter bvedgie

    (@bvedgie)

    @konamiman Perfect!
    It seems to work, the error msg disappeared ??

    Thanks a lot, @konamiman.

    Thread Starter bvedgie

    (@bvedgie)

    Hello there,

    I’m reopening this ticket because I just noticed that this filter was preventing a plugin from running ( WooCommerce Buy One Get One Free) and I don’t know how to do it. Do you have an idea ?

    Saif

    (@babylon1999)

    Hello @bvedgie,

    I’m reopening this ticket because I just noticed that this filter was preventing a plugin from running ( WooCommerce Buy One Get One Free) and I don’t know how to do it. Do you have an idea ?

    I’m afraid debugging custom solutions is not within our scope of support here in the forums.

    I recommend you post this in the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘TRYING TO GET PROPERTY ‘SLUG’ OF NON-OBJECT’ is closed to new replies.