允许特定产品单独购买Woocommerce

时间:2020-02-15 05:04:13

标签: php wordpress woocommerce hook-woocommerce

假设我有一个产品A,可以单独购买。

如果购物车中已经有其他物品,则会显示该消息。 “不允许”。我尝试了各种方法,但无法为此找到合适的解决方案。

当某人试图单击“添加到购物车”按钮时,它必须通过代码检查是否允许购物车中的其他物品不被放入购物车并显示消息。

产品A是允许单独购买的。

我尝试了类别比较,并且有效。但我只想使用产品ID。

const str = `Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10`;

Array.prototype.chunk = function(size) {
  const chunked_arr = [];
  let index = 0;
  while (index < this.length) {
    chunked_arr.push(this.slice(index, size + index));
    index += size;
  }
  return chunked_arr;
}

const matches = str.split("\n").chunk(5);
console.log(matches);

此代码仅适用于我只允许使用产品ID而不是类别的add_filter('woocommerce_add_to_cart_validation', 'dont_add_paint_to_cart_containing_other', 10, 5); function dont_add_paint_to_cart_containing_other($validation, $product_id) { // Set flag false until we find a product in cat paint $cart_has_paint = false; // Set $cat_check true if a cart item is in paint cat foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { $products_ids = 137817; $product = $cart_item['data']; if (has_term('miscellaneous', 'product_cat', $product->id)) { $cart_has_paint = true; // break because we only need one "true" to matter here break; } } $product_is_paint = false; if (has_term('miscellaneous', 'product_cat', $product_id)) { $product_is_paint = true; } // Return true if cart empty if (!WC()->cart->get_cart_contents_count() == 0) { // If cart contains paint and product to be added is not paint, display error message and return false. if ($cart_has_paint && !$product_is_paint) { echo '<script type="text/javascript">'; echo ' alert("Hello,Sorry, “custom order” items must be purchased separately! To purchase this item, please either checkout with your current cart or remove any “custom order” items from your cart to enable you add the regular items.")'; //not showing an alert box. echo '</script>'; $validation = false; } // If cart contains a product that is not paint and product to be added is paint, display error message and return false. elseif (!$cart_has_paint && $product_is_paint) { echo '<script type="text/javascript">'; echo ' alert("Sorry, “custom order” item must be purchased separately! To purchase any “custom order” items, please either checkout with your current cart or empty cart to enable you add the “custom order” items.")'; //not showing an alert box. echo '</script>'; $validation = false; } } // Otherwise, return true. return $validation; } 类别。

1 个答案:

答案 0 :(得分:1)

假设只能从产品A购买1个数量

function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // Product id to bought alone 
    $product_id_alone = 30;

    // Set variable
    $alone = true;

    // If passed
    if ( $passed ) {
        // If cart is NOT empty when a product is added
        if ( !WC()->cart->is_empty() ) {

            // If product id added = product id alone
            if ( $product_id_alone == $product_id ) {
                $alone = false;
            } else {
                // Generate a unique ID for the cart item
                $product_cart_id = WC()->cart->generate_cart_id( $product_id_alone );

                // Check if product is in the cart
                $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

                // If product is already in cart
                if ( $in_cart ) {
                    $alone = false;
                }
            }
        } else {
            // If product is added when cart is empty but $quantity > 1
            if ( $product_id_alone == $product_id && $quantity > 1 ) {
                $alone = false;         
            }
        }
    }

    if ( $alone == false ) {
        // Set error message
        $message = 'PRODUCT A is allow bought alone.';
        wc_add_notice( __( $message, 'woocommerce' ), 'error' );
        $passed = false;

        // Empty the cart
        WC()->cart->empty_cart();

        // Add specific product with quantity 1
        WC()->cart->add_to_cart($product_id_alone, 1 );
    }

    return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );