The function does not work on the server. Cannot use function return value in record context

I use this function to check if certain products are in the cart in my woocommerce. This works on my localhost, but gives me:

Cannot use function return value in record context

on server.

function product_is_in_the_cart() { $ids = array( '139, 358, 359, 360' ); $cart_ids = array(); // Find each product in the cart and add it to the $cart_ids array foreach( WC()->cart->get_cart() as $cart_item_key => $values ) { $cart_product = $values['data']; $cart_ids[] = $cart_product->id; } // Si uno de los productos introducidos en el array esta, devuelve false if ( ! empty( array_intersect( $ids, $cart_ids ) ) ) { return true; } else { return false; }} 

I try to find other methods for this, but I can not find the answer to my problem, I think this is due to empty (), but how can I do it differently?

+5
source share
3 answers

I see it is tagged php 5.3

In php versions prior to 5.5, empty () will only accept a variable. You will need to assign it first as follows:

 $isEmpty = array_intersect($ids, $cart_ids); if ( !empty($isEmpty) ) { ... } 
+4
source

Update your PHP server.

Check the version of PHP on your computer and server. as indicated in the documentation , in an older version you can only pass a variable.

Prior to PHP 5.5, empty () only supports variables;

+2
source

Now the function works as follows:

 function product_is_in_the_cart() { global $woocommerce; $items = array( '139, 240, 242, 358, 359, 360' ); // Create array from current cart $cartitems = $woocommerce->cart->get_cart(); // Count items in cart $itemcount = count( $cartitems ); foreach($cartitems as $cartitem) { $productid = $cartitem[product_id]; if(in_array($productid,$items)) { return true; } else { return false; } } } 
0
source

Source: https://habr.com/ru/post/1239034/


All Articles