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

利昂M.

无论他选择了哪种产品,我都有一个代码可以自动将产品(定金)添加到客户购物车中-该代码位于functions.php中。这很好。

但是现在,如何扩展仅当客户从特定产品类别中选择产品后才自动将其添加到购物车的代码?例如,当客户购买礼品卡时,不应将定金添加到购物车中。

非常感谢!

/**
 * 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 {

        }
    }
}
LoicTheAztec

您将必须以hast_term()完全不同的方式使用Wordpress条件函数

如果来自产品类别的产品已经在购物车中,则以下代码将自动将预定义产品添加到购物车中:

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
    }
}

代码进入您的活动子主题(或活动主题)的functions.php文件中。经过测试和工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

禁用Woocommerce中特定类别的购物车商品的其他产品类别

仅当购物车商品来自3个不同商品类别时,才自动添加优惠券折扣

在Woocommerce中根据购物车商品尺寸添加费用

隐藏购物车中“ WooCommerce”产品类别的“删除商品”

获取WooCommerce购物车商品计数(产品类别除外)

在Woocommerce结帐中为缺货的购物车商品添加自定义通知

Woocommerce中的自定义购物车商品重量

有条件地向Woocommerce中的购物车商品和订单商品添加类

设置购物车商品仅针对Woocommerce中的特定商品生成的销售价格

在Woocommerce中按商品ID获取购物车商品数量

在Woocommerce购物车和结帐中显示商品类别名称

在Woocommerce购物车页面中的购物车商品名称后添加产品ID

Woocomerce在将其他特定商品添加到购物车时,删除特定的购物车商品

将购物车中的商品限制为来自同一商品类别

在WooCommerce中为特定类别的最便宜商品打折

如果运输类别适用于WooCommerce中的所有购物车商品,请取消设置运输价格

仅针对使用特定运输类别ID的购物车商品禁用特定的运输方式

更改WooCommerce购物车商品名称

当购物车商品具有特定的运输类别时,禁用运输方式吗?

将自定义购物车商品值添加到WooCommerce订单商品元数据

根据Woocommerce中产品类别添加到购物车的最大商品数量

用Woocommerce 3中特定产品类别的产品替换加售商品

当购物车中有特定产品ID时,设置Woocommerce产品类别商品价格

Woocommerce:如何将元数据添加到购物车商品?

最小购物车数量(商品类别除外)

购物车商品中的商品变化属性在WooCommerce中的显示方式有所不同

在Woocommerce购物车商品名称和订单商品名称中添加换行符

从 WooCommerce 中的自定义字段值动态更改购物车商品价格

在WooCommerce中获取购物车商品变体名称而不是变体ID