Trying to get property 'name' of non-object error while trying to change woocommerce breadcrumbs

665 Views Asked by At

I use following function to add WooCommerce parent & sub-categories in breadcrumbs:

// Load Woocommerce product parent & sub-categories in breadcrumbs

add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);
function post_parent_trail ($crumbs, $args) {
    if ( is_product() || is_shop() ) {
        global $product;
        $category = wc_get_product_term_ids( $product->get_id(), 'product_cat' );

        if ( count( $category ) > 1 ) {

            $cat_array             = array();
            $cat_array[1]['label'] = $category[1]->name;
            $cat_array[1]['url']   = get_term_link( $category[1]->term_id );
            $cat_array[2]['label'] = $category[0]->name;
            $cat_array[2]['url']   = get_term_link( $category[0]->term_id );

            array_splice($crumbs, 2, 0, $cat_array);
        }
    }
    return $crumbs;  
}

but got this error :

[18-Jul-2021 11:58:36 UTC] PHP Notice: Trying to get property 'name' of non-object in /home/user/public_html/site/wp-content/themes/pro-child/functions.php on line 969 [18-Jul-2021 11:58:36 UTC] PHP Notice: Trying to get property 'term_id' of non-object in /home/user/public_html/site/wp-content/themes/pro-child/functions.php on line 970 [18-Jul-2021 11:58:36 UTC] PHP Notice: Trying to get property 'name' of non-object in /home/user/public_html/site/wp-content/themes/pro-child/functions.php on line 971 [18-Jul-2021 11:58:36 UTC] PHP Notice: Trying to get property 'term_id' of non-object in /home/user/public_html/site/wp-content/themes/pro-child/functions.php on line 972 [18-Jul-2021 11:58:36 UTC] PHP Fatal error: Uncaught Error: Object of class WP_Error could not be converted to string in /home/user/public_html/site/wp-includes/formatting.php:1098

Could you please guide me to solve this issue?

0

There are 0 best solutions below