1

我想从 wordpress、woocommerce 中的购物车中删除变体产品。

我可以删除一个简单的产品,但不能删除一个变体产品。使用此代码,我可以删除一个简单的产品。我尝试传入变体 id 和父变体 id。我不知道为什么它不起作用,如果有人知道的话将不胜感激。

$product_cart_id = WC()->cart->generate_cart_id( $aCurrentUserDataItem->id );
$cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id );
( $cart_item_key ) WC()->cart->remove_cart_item( $cart_item_key );
4

1 回答 1

1

您还可以使用如下的 foreach 循环来定位并从购物车中删除特定的变体 ID:

$remove_variation_id = 41; // The variation id to remove

// loop through cart items
foreach ( WC()->cart->get_cart() as $item_key => $item ) {
    // If the targeted variation id is in cart
    if ( $item['variation_id'] == $remove_variation_id ) {
        WC()->cart->remove_cart_item( $item_key ); // we remove it
        break; // stop the loop
    }
}

测试和工作。

于 2020-10-12T11:07:34.260 回答