-2

I am generating the following notice in frontend when testing my theme in wp_debug mode.

I can see where the problem is but do not know how to fix this issue?

Error is showing below notice.

Notice: Trying to get property of non-object in /home/wp-content/plugins/bodycenter-shortcodes/shortcodes-woo/templates/sns_cat_info.php on line 20

Catchable fatal error: Object of class WP_Error could not be converted to string in /home/wp-content/plugins/bodycenter-shortcodes/shortcodes-woo/templates/sns_cat_info.php on line 29

<?php
$output = '';
$atts = vc_map_get_attributes( 'sns_cat_info', $atts );
extract( $atts );
if( class_exists('WooCommerce') ){
    $uq = rand().time();
    $class = 'sns-cat-info';
    // $class .= ( esc_attr($style) !='' )?' style-'.esc_attr($style):'';
    $class .= ( trim(esc_attr($extra_class))!='' )?' '.esc_attr($extra_class):'';
    $class .= ' '.esc_attr( bodycenter_getCSSAnimation( $css_animation ) );
    if ( $info_want == '3' ) {
        $class .= ' show-btn-shopnow';
    }elseif ( $info_want == '4' ) {
        $class .= ' show-btn-seeall';
    }
    $l_href = $l_name = $l_target = '_self' ;
    if ( $r_type == '1' ){
        $cat_info = get_term_by('slug', $cat, 'product_cat');
        $l_href = get_term_link($cat, 'product_cat');
        $l_name = trim($ctitle) != '' ? $ctitle : $cat_info->name;
        $l_target = '_self';
    }elseif ($r_type == '2' && !empty( $clink ) ) {
        $clink = vc_build_link( $clink );
        $l_href = $clink['url'];
        $l_name = $clink['title'] ? esc_attr( $clink['title'] ) : esc_html__('You should enter title', 'bodycenter-shortcodes');
        $l_target = esc_attr( $clink['target'] ) ;
    }
    $output .= '<div class="'.$class.'">';
    $output .= '<a class="cat-img" href="'.$l_href.'">';
    if($cat_image != ''){
        $cat_image = preg_replace('/[^\d]/', '', $cat_image);
        $img =   wp_get_attachment_image_src( $cat_image , '');
        $output .= '<img src="'.$img[0].'" alt="'.$l_name.'" />';
    }
    $output .= '</a>';
    $output .= '<div class="cat-info">';
    $output .= '<h4 class="cat-title"><a class="second-font" href="'.$l_href.'" target="'.$l_target.'">'.$l_name.'</a>';
    if ( $info_want == '4' ){
        $output .= '<a class="btn-seeall" href="'.$l_href.'">' . esc_html__('See all', 'bodycenter-shortcodes') . '</a>';
    }
    $output .= '</h4>';
    if ( $info_want == '1' ){
        $output .= '<span class="cat-prd-num">'.$cat_info->count. ' ' . esc_html__('Products', 'bodycenter-shortcodes') .'</span>';
    }else if ( $info_want == '2' ){
        $output .= '<span class="jf-price second-font">' . esc_html__('Just from', 'bodycenter-shortcodes') .' <span class="price-num">'.$jf_price.'</span></span>';
    }
    $output .= '</div>';
    if ( $info_want == '3' ){
        $output .= '<div class="btn-wrap"><a class="btn-shopnow button" href="'.$l_href.'">' . esc_html__('Shop now', 'bodycenter-shortcodes') . '</a></div>';
    }
    $output .= '</div>';
}
echo $output;

closed as off-topic by Jacob Peattie, Michael, fuxia Jul 5 at 19:40

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • Are you the developer of bodycenter-shortcodes? If not, you’re just going to have to report the issue to its developer. – Jacob Peattie Jul 5 at 14:00
0

The problem is this line:

$cat_info = get_term_by('slug', $cat, 'product_cat');

You're trying to get a category by slug, but $cat is undefined, so $cat_info is false, which means that this line:

$l_name = trim($ctitle) != '' ? $ctitle : $cat_info->name;

Is throwing the first error, because you can't access ->name on false.

And on this line:

$l_href = get_term_link($cat, 'product_cat');

$l_href is becoming a WP_Error object because — again — $cat doesn't exist. This means that:

$output .= '<a class="cat-img" href="'.$l_href.'">';

Is throwing the second error, because you're can't concatenate a WP_Error object into a string.

  • thank for help. – shashi sawardekar Jul 5 at 14:37
  • i added this code but there is new error which is below:--------Notice: Undefined variable: cat_info in /home/wp-content/plugins/bodycenter-shortcodes/shortcodes-woo/templates/sns_cat_info.php on line 18 Notice: Undefined variable: cat_info in /home/wp-content/plugins/bodycenter-shortcodes/shortcodes-woo/templates/sns_cat_info.php on line 20 – shashi sawardekar Jul 5 at 14:39
  • That one should be self explanatory. You need to pay attention to what variables you're using and where they're coming from. – Jacob Peattie Jul 5 at 14:45
  • hi jacob peattie, sir i m web designer i do not unerstand php. kindly guide me this is theme which i purchase from theme forest. please help me solve this error. as per your code i made changes and same part of website is showing properly. – shashi sawardekar Jul 5 at 14:58
  • 1
    You paid for this? Get a refund and find another plugin. I'm not doing unpaid work to fix a plugin somebody is charging for. – Jacob Peattie Jul 5 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.