功能在服务器上不起作用。不能在写入上下文中使用函数 return 值

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

我正在使用此功能检查某些产品是否在我的 woocommerce 购物车中。这适用于我的本地主机,但给了我一个:

Can't use function return value in write context

在服务器上。

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;
}} 

我正在尝试寻找其他方法来执行此操作,但我找不到问题的答案,我认为是因为 empty() 但我如何通过其他方式执行此操作?

升级您的服务器 PHP。

检查您的计算机和服务器上的 PHP 版本。如 documentation 中所述,在旧版本中你只能传递变量。

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

我看到这个被标记了 php 5.3

在 5.5 之前的 php 版本中 empty() 将只接受一个变量。您需要先像这样分配它:

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

if ( !empty($isEmpty) ) {
...
}

函数现在是这样工作的:

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;
            }
        }
}