• Resolved anempadest

    (@anempadest)


    Have made a snippet.

    add_shortcode('product_qty', function () {
    	$out = ' Цена указана за 1 килограмм.';
    	$slugs = array("oil-essential");
    	global $product;
    	static $cats;
    	$cats = get_the_terms($product->id,'product_cat');
    	$product_in_category = false;
    	foreach ($cats as $key => $cat) {
    		if (in_array($cat->slug, $slugs)) {
    			$product_in_category = true;
    			break;
    		}
    	}
    	if ($product_in_category) {
    		$out = $out . ' Купить можно от 100 грамм.';
    	}
    	return $out;
    });

    Looks fine. But only on the Main page it shows an error messege:
    – Warning: Invalid argument supplied for foreach() in /var/www/chb73f2d7f/www/essenti.com.ua/wp-content/plugins/code-snippets/php/snippet-ops.php(469) : eval()’d code on line 12

    What can I do to get rid of this?

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You need to account for situations where get_the_terms returns false. Something like this should do the trick:

    add_shortcode( 'product_qty', function () {
    	global $product;
    	static $cats;
    	
    	$out = ' Цена указана за 1 килограмм.';
    	$slugs = array( "oil-essential" );
    	$cats = get_the_terms( $product->id, 'product_cat' );
    	$product_in_category = false;
    	
    	if ( ! $cats ) {
    		return $out;
    	}
    	
    	foreach ( $cats as $key => $cat ) {
    		if ( in_array( $cat->slug, $slugs ) ) {
    			$product_in_category = true;
    			break;
    		}
    	}
    	if ( $product_in_category ) {
    		$out = $out . ' Купить можно от 100 грамм.';
    	}
    
    	return $out;
    } );
Viewing 1 replies (of 1 total)
  • The topic ‘FOREACH Invalid argument supplied’ is closed to new replies.