使特定产品在 WooCommerce 中互斥

情况

我的网站提供两种产品。如果用户购买产品 A,他们就可以访问所有电子书。如果用户购买产品B,他们将被允许选择一本电子书。在 WooCommerce 中,我创建了电子书作为产品 B 中的变体。

产品 A ID = 477

产品 B ID = 297 。虽然 297 是主要 ID,但我相信我应该使用变体 ID,例如。1483、997。

我试图解决的问题

我希望这两种产品是互斥的。毕竟,如果用户购买了所有电子书(即产品 A)的访问权限,那么他们就不需要购买产品 B 下的个别书籍。

最终结果

我尝试了一些代码(如下所示),但它根本不起作用。这意味着我仍然可以将产品 A 和 B 的变体添加到同一个购物车中。

问题1:我该如何解决这个问题?

问题 2:是否有更简单的方法来不指定每个产品 B 变体?这是一个非常长的列表,从长远来看不是一个可扩展的解决方案。


海绵宝宝撒
浏览 64回答 1
1回答

沧海一幻觉

有一种更简单的方法可以使用“添加到购物车”验证挂钩:add_filter( 'woocommerce_add_to_cart_validation', 'wc_add_to_cart_validation_filter_callback', 10, 3 );function wc_add_to_cart_validation_filter_callback( $passed, $product_id, $quantity ) {&nbsp; &nbsp; if( WC()->cart->is_empty() )&nbsp; &nbsp; &nbsp; &nbsp; return $passed;&nbsp; &nbsp; $product_id1 = 37; // Single product Id&nbsp; &nbsp; $product_id2 = 19; // Variable product Id&nbsp; &nbsp; // Loop through cart items&nbsp; &nbsp; foreach( WC()->cart->get_cart() as $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; if( ( $product_id = $product_id1 && $cart_item['product_id'] == $product_id2 )&nbsp; &nbsp; &nbsp; &nbsp; || ( $product_id = $product_id2 && $cart_item['product_id'] == $product_id1 ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; wc_add_notice( sprintf(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; __("This product can't not be purchased toggether with %s."),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '"<strong>' . $cart_item['data']->get_name() . '</strong>"'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ), 'error' );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $passed;}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。添加 (与您的评论相关):另一种方法是当两种产品都在购物车中时,静默删除第一项:add_action('woocommerce_before_calculate_totals', 'keep_last_variation_from_variable_products', 10, 1 );function keep_last_variation_from_variable_products( $cart ) {&nbsp; &nbsp; if ( is_admin() && ! defined( 'DOING_AJAX' ) )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; $product_id1 = 37; // Single product Id&nbsp; &nbsp; $product_id2 = 19; // Variable product Id&nbsp; &nbsp; $product_ids = array($product_id1, $product_id2);&nbsp; &nbsp; $items_keys&nbsp; = array();&nbsp; &nbsp; // Loop through cart items&nbsp; &nbsp; foreach (&nbsp; $cart->get_cart() as $cart_item_key => $cart_item ) {&nbsp; &nbsp; &nbsp; &nbsp; // If cart item matches with one of our defined product&nbsp; &nbsp; &nbsp; &nbsp; if ( in_array( $cart_item['product_id'], $product_ids ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set the cart item key in an array (avoiding duplicates)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $items_keys[$cart_item['product_id']] = $cart_item_key;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // If both products are in cart just keep the last one silently&nbsp; &nbsp; if( count($items_keys) > 1 ) {&nbsp; &nbsp; &nbsp; &nbsp; $cart->remove_cart_item( reset($items_keys) ); // remove the first one&nbsp; &nbsp; }}代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP