Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function not working on server. Can't use function return value in write context

I'm using this function to check if certain products are in the cart on my woocommerce. This works on my localhost but is giving me a:

Can't use function return value in write 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'm trying to find other methods to do this but i cant find a answer to my problem, I think is because of empty() but how can i do this on other way?

like image 377
MidouCloud Avatar asked Dec 23 '15 10:12

MidouCloud


2 Answers

I see this is tagged php 5.3

In versions of php before 5.5 empty() will only accept a variable. You will need to assign it first like so:

$isEmpty = array_intersect($ids, $cart_ids);

if ( !empty($isEmpty) ) {
...
}
like image 128
danjam Avatar answered Nov 15 '22 05:11

danjam


Upgrade your server's PHP.

Check the PHP version on your machine and the server. as mentioned in the documentation, in older version you could only pass the variable.

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

like image 31
Ali Avatar answered Nov 15 '22 04:11

Ali