【发布时间】:2020-07-16 06:47:04
【问题描述】:
情况
我的网站提供两种产品。如果用户购买产品 A,他们可以访问所有电子书。如果用户购买产品 B,他们将被允许选择一本电子书。在 WooCommerce 中,我创建了电子书作为产品 B 中的变体。
产品 A ID = 477
产品 B ID = 297 (https://i.imgur.com/Sdo4XnT.png)。虽然 297 是主要 ID,但我相信我应该使用变体 ID,例如。 1483、997 (https://i.imgur.com/CEMFNLD.png)。
我要解决的问题
我希望两种产品互斥。毕竟,如果用户购买了所有电子书的访问权限(即产品 A),那么他们就不需要购买产品 B 下的单本书籍。
最终结果
我尝试了一些代码(如下所示),但它根本不起作用。这意味着我仍然可以将产品 A 和 B 的变体一起添加到同一个购物车中。
问题 1:我该如何解决?
问题 2: 有没有更简单的方法来不指定每个产品 B 变体?这是一个非常长的列表,从长远来看不是一个可扩展的解决方案。
我尝试了什么
这似乎符合我的要求:Woocomerce Remove a specific cart items when adding to cart another specific items
由于产品 B 的变化,我不得不稍微修改它。
add_action( 'woocommerce_add_to_cart', 'check_product_added_to_cart', 10, 6 );
function check_product_added_to_cart($cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
// Set HERE your targeted product ID
$target_product_ids1 = array(477);
$target_product_ids2 = array(1483, 997,998,999,1000,1001);
// Set HERE the product ID to remove
$item_id_to_remove1 = array(1483, 997,998,999,1000,1481);
$item_id_to_remove2 = array(477);
// Initialising some variables
$has_item1 = $has_item2 = $is_product_id1 = $is_product_id2 = false;
foreach( WC()->cart->get_cart() as $key => $item ){
// Check if the item to be removed 1 is in cart
if( in_array( $item['product_id'], $item_id_to_remove1) ){
$has_item1 = true;
$key_to_remove1 = $key;
}
// Check if the item to be removed 2 is in cart
if( in_array( $item['product_id'], $item_id_to_remove2) ){
$has_item2 = true;
$key_to_remove2 = $key;
}
// Check if we added to cart any targeted product IDs 1
if( in_array( $product_id, $target_product_ids1 ) ){
$is_product_id1 = true;
}
// Check if we added to cart any targeted product IDs 2
if( in_array( $product_id, $target_product_ids2 ) ){
$is_product_id2 = true;
}
}
if( $has_item1 && $is_product_id1 ){
WC()->cart->remove_cart_item($key_to_remove1);
// Optionaly displaying a notice for the removed item:
wc_add_notice( __( 'The product 1 "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );
}
if( $has_item2 && $is_product_id2 ){
WC()->cart->remove_cart_item($key_to_remove2);
// Optionaly displaying a notice for the removed item:
wc_add_notice( __( 'The product 2 "blab bla" has been removed from cart.', 'theme_domain' ), 'notice' );
}
}
【问题讨论】:
标签: php wordpress woocommerce product cart