提问者:小点点

如何在Woocommerce中为每个品牌创建购物车


所以我用Woo Brand插件来显示我在我的商店里使用的所有品牌。 我想做的是限制顾客一次从几个品牌购买。 例如,如果我有品牌A和品牌B,客户可以只从品牌A或只从品牌B添加产品到购物车。如果他们的篮子里只有品牌A的产品,他们从品牌B转到页面,在品牌B的页面上我想有一个新的篮子,在这个篮子里他们只能添加品牌B的产品。如果他们从品牌A返回页面,他们的篮子里仍然有只从品牌A添加的产品。我如何实现这一点?

目前我有这段代码,可以添加在购物车只从一个特定的品牌,例如(如果我在品牌一页,我可以添加只从他们)

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_brand_allowed', 20, 3 );
function only_one_product_brand_allowed( $passed, $product_id, $quantity) {

// Getting the product brand term slugs in an array for the current product
$brand_slugs = wp_get_post_terms( $product_id, 'product_brand', array( 'fields' => 'slugs' ) );


$cart_contents = WC()->cart->get_cart();
$cart_item_keys = array_keys ( $cart_contents );

// Get the brand name for first item from cart
$first_item = $cart_item_keys[0];
$first_item_id = $cart_contents[$first_item]['product_id'];
$brand_name = get_the_terms($first_item_id, 'product_brand');
$current_product_brand = get_the_terms($product_id, 'product_brand');




// Loop through cart items
foreach ($cart_contents as $cart_item_key => $cart_item ){

    // Check if the product category of the current product don't match with a cart item
    if( ! has_term( $brand_slugs, 'product_brand', $cart_item['product_id'] ) ){

        // phpAlert('Trebuie golit cosul');

        // Displaying a custom notice
       wc_add_notice( __('You can add products to the cart only from the same brand. In your cart are products from <strong>'.$brand_name[0]->name. '.' ), 'error' );

        // Avoid add to cart
        return false; // exit
    }
}
return $passed;
}

共1个答案

匿名用户

您不能在WooCommerce中为每个产品品牌设置不同的购物篮(购物车),您只能避免客户组合不同品牌的商品,并优化您的代码,如:

add_filter( 'woocommerce_add_to_cart_validation', 'only_one_product_brand_allowed', 20, 3 );
function only_one_product_brand_allowed( $passed, $product_id, $quantity) {
    $taxonomy    = 'product_brand';
    $field_names = array( 'fields' => 'names');

    // Getting the product brand term name for the current product
    if( $term_name = wp_get_post_terms( $product_id, $taxonomy, $field_names ) ) {
        $term_name = reset($term_name);
    } else return $passed;


    // Loop through cart items
    foreach (WC()->cart->get_cart() as $cart_item ){
        // Get the cart item brand term name
        if( $item_term_name = wp_get_post_terms( $cart_item['product_id'], $taxonomy, $field_names ) ) {
            $item_term_name = reset($item_term_name);
        } else continue;

        // Check if the product brand of the current product exist already in cart items
        if( isset($term_name) && isset($item_term_name) && $item_term_name !== $term_name ){
            // Displaying a custom notice 
            wc_add_notice( sprintf( __("You are not allowed to combine products from different brands. There is already a cart item from <strong>%s</strong> brand.", "woocommerce" ), $item_term_name ), 'error' );

            // Avoid add to cart and display message
            return false;
        }
    }
    return $passed;
}

代码放在活动子主题(或活动主题)的function.php文件中。 经过测试并正常工作。

现在你可以做的是在多个子订单中吐出一个订单,每个品牌(或商家)一个,保留你的原始订单。