简体   繁体   English

为 WooCommerce 中特定产品类别的购物车项目自动添加产品

[英]Auto add a product for cart item from specific product categories in WooCommerce

I have a code to add a product (a deposit) automatically to a customers cart, no matter what product he has chosen - with the code below inside the functions.php.我有一个代码可以自动将产品(存款)添加到客户的购物车,无论他选择了什么产品 - 在函数中使用下面的代码。php。 This works fine.这很好用。

But now, how to extend the code that this product will only be automatically added to the cart when the customer has chosen a product from a specific product category?但是现在,如何扩展只有当客户从特定产品类别中选择产品时,该产品才会自动添加到购物车的代码? Eg the deposit shouldn't be added to the cart when a customer buys a gift card.例如,当客户购买礼品卡时,押金不应添加到购物车中。

Thanks a lot!非常感谢!

/**
 * Automatically adds product to cart on visit
 */
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 1267; //product added automatically
        $found = false;
        //check if product already in cart
        if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
            foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( $_product->get_id() == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {

        }
    }
}

You will have to use the Wordpress conditional function hast_term() in a completely different way.您将不得不以完全不同的方式使用 Wordpress 条件 function hast_term()

The following code will auto add to cart a pre-defined product, if a product from a product category is already in cart:如果产品类别中的产品已在购物车中,以下代码将自动将预定义产品添加到购物车:

add_action( 'woocommerce_before_calculate_totals', 'auto_add_item_based_on_product_category', 10, 1 );
function auto_add_item_based_on_product_category( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $required_categories = array('t-shirts'); // Required product category(ies)
    $added_product_id = 1267; // Specific product to be added automatically
    $matched_category = false;

    // Loop through cart items
    foreach ( $cart->get_cart() as $item_key => $item ) {
        // Check for product category
        if( has_term( $required_categories, 'product_cat', $item['product_id'] ) ) {
            $matched_category = true;
        }
        // Check if specific product is already auto added
        if( $item['data']->get_id() == $added_product_id ) {
            $saved_item_key = $item_key; // keep cart item key
        }
    }

    // If specific product is already auto added but without items from product category
    if ( isset($saved_item_key) && ! $matched_category ) {
        $cart->remove_cart_item( $saved_item_key ); // Remove specific product
    }
    // If there is an item from defined product category and specific product is not in cart
    elseif ( ! isset($saved_item_key) && $matched_category ) {
        $cart->add_to_cart( $added_product_id ); // Add specific product
    }
}

Code goes in functions.php file of your active child theme (or active theme).代码进入您的活动子主题(或活动主题)的 functions.php 文件。 Tested and works.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁用 Woocommerce 中特定类别的购物车项目的其他产品类别 - Disable other product categories for a cart item from specific category in Woocommerce 自动将产品添加到购物车,部分 WooCommerce 产品类别除外 - Auto add product to cart except for some WooCommerce product categories 隐藏产品价格并禁用 Woocommerce 中特定产品类别的添加到购物车 - Hide product price and disable add to cart for specific product categories in Woocommerce 更改Woocommerce中特定产品类别的购物车项目价格 - Change cart item prices for specific product categories in Woocommerce 显示特定产品类别的 WooCommerce 购物车项目简短描述 - Display WooCommerce cart item short description for a specific product categories WooCommerce 中特定产品类别的最小购物车商品数量 - Minimum cart item quantity for specific product categories in WooCommerce 在Woocommerce 3中自动将产品添加到购物车 - Auto add a Product to Cart in Woocommerce 3 如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车 - Avoid add to cart for specific product categories if user is unlogged in Woocommerce 在特定的WooCommerce产品类别上自定义“添加到购物车”按钮 - Customize Add to Cart button on specific WooCommerce product categories Woocommerce 中定义的产品类别中只允许购物车中的一件商品 - Allow only one item in cart from defined product categories in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM