如果其他购物车项目在特定类别中,则将产品添加到 woocommerce 购物车 [英] Add a product to woocommerce cart if other cart items are in a specific category

查看:36
本文介绍了如果其他购物车项目在特定类别中,则将产品添加到 woocommerce 购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果购物车中的任何商品来自特定类别,我需要能够有条件地将产品的一个添加到 WooCommerce 订单/购物车.这是我尝试过的代码,但它会锁定结帐和购物车页面,不返回 woocommerce 信息,或之后的任何内容(没有页脚,没有其他页面元素等......直到前面的 WooCommerce 数据为止)-end,后面有一个很大的空格).请,如果有人能指出我正确的方向,将不胜感激!

I need to be able to conditionally add only one of a product to a WooCommerce order/cart, if any of the items in the cart are from a specific category. This is the code I have tried, but it locks up the checkout and cart pages, returning NO woocommerce info, or anything after (no footer, no additional page elements, etc....just everything up until the WooCommerce data on the front-end, with a big blank space after). Please, if anyone could point me in the right direction, it would be SO appreciated!

这是我的functions.php文件中的内容:

Here's what I have in my functions.php file:

        /**
 * Checks the cart to verify whether or not a product from a Category is in the cart
 * @param $category Accepts the Product Category Name, ID, Slug or array of them
 * @return bool
 */
function qualifies_basedon_product_category( $category ) {
    foreach( WC()->cart->cart_contents as $key => $product_in_cart ) {
        if( has_term( $category, 'fee', get_id_from_product( $product_in_cart, false ) ) ) {
            return true;
        }
    }
    // Return false in case anything fails
    return false;
}

/**
 * Adds a specific product to the cart
 * @param $product_id Product to be added to the cart
 */
function add_incentive_to_cart( $product_id ) {
    // Check the cart for this product
    $cart_id = WC()->cart->generate_cart_id( $product_id );
    $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );
    // Add the product only if it's not in the cart already
    if( ! $prod_in_cart ) {
        WC()->cart->add_to_cart( $product_id );
    }
}



add_action( 'woocommerce_check_cart_items', 'qualifies_for_incentive' );
function qualifies_for_incentive() {
    // Incentive product
    $incentive_product_id = 506;

    if( qualifies_basedon_product_category( 'fee' ) ) {
        add_incentive_to_cart( $incentive_product_id );
    } 
}

推荐答案

你可以研究下面的代码-

you can investigate the following code-

add_action('woocommerce_add_to_cart', 'my_woocommerce_add_to_cart', 10, 6);

function my_woocommerce_add_to_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ){
    $incentive_product_id = 506;
    $category = 'fee';
    $taxonomy = 'fee';

    if( has_term( $category, $taxonomy, $product_id ) ){    
        $cart_id = WC()->cart->generate_cart_id( $incentive_product_id );
        $prod_in_cart = WC()->cart->find_product_in_cart( $cart_id );

        // Add the product only if it's not in the cart already
        if( ! $prod_in_cart ) {
            WC()->cart->add_to_cart( $incentive_product_id );
        }
    }
}

希望对您有所帮助.

这篇关于如果其他购物车项目在特定类别中,则将产品添加到 woocommerce 购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆