简体   繁体   English

Woocommerce:按用户元自动将产品添加到购物车

[英]Woocommerce: auto add product to cart by user meta

I need to automatically add a product to the cart after user registration (which worked) but to decide which product to add by the user meta (which doesn't work).我需要在用户注册(有效)后自动将产品添加到购物车,但要决定由用户元添加哪个产品(无效)。

The first action was just to add a product after registration and it worked perfectly:第一个动作就是在注册后添加一个产品,它工作得很好:

add_action( 'user_register', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 115;
        $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->id == $product_id )
                    $found = true;
            }
            // if product not found, add it
            if ( ! $found )
                WC()->cart->add_to_cart( $product_id );
        } else {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}

Now I need to add a specific product according to lists I have of users promoID I got, but it doesn't add anything to the cart.现在我需要根据我拥有的用户 promoID 列表添加特定产品,但它不会向购物车添加任何内容。 example of the code:代码示例:

add_action( 'user_register', 'add_product_to_cart' );
function add_product_to_cart() {
    if ( ! is_admin() ) {

        $group1iid1 = array("1", "2", "3", "4");
        $group1iid2 = array("5", "6", "7", "8");

        if (in_array("2", $group1iid1)) {
            $product_id = 115;
            WC()->cart->add_to_cart( $product_id );
        } elseif (in_array("0", $group1iid2)) {
            $product_id = 219;
            WC()->cart->add_to_cart( $product_id );

        } else {
            $product_id = 231;
            WC()->cart->add_to_cart( $product_id );
        }
    }
}

If I take the code to a template file and just echo something instead of adding a product - it works ok, but when it's like this in the function.php > nothing happens.如果我将代码带到模板文件中,并且只是回显某些内容而不是添加产品 - 它可以正常工作,但是当它在 function.php 中是这样时 > 没有任何反应。

What am I missing?我错过了什么?

There are missing things in your code:您的代码中缺少一些内容:

  1. In your first condition you need also to add is_user_logged_in() condition, as I suppose that this code is for new registrated users only.在您的第一个条件中,您还需要添加is_user_logged_in()条件,因为我认为此代码仅适用于新注册用户。

  2. You need to get for the current user, HIS Promo ID value .您需要获取当前用户的 HIS Promo ID 值 I suppose that this value is set in user metadata, so to get this Promo ID value with get_user_meta() function, you have to define the correct meta_key .我想这个值是在用户元数据中设置的,所以要使用get_user_meta()函数获取这个 Promo ID 值,您必须定义正确的meta_key

  3. In your code you have to replace in your conditions '2' and '0' values by the current user Promo ID在您的代码中,您必须用当前用户的促销 ID替换条件中的'2''0'值……
    (Also elseif (in_array("0", $group1iid2)) { condition is going to be always false as "0" value doesn't exist in $group1iid2 ) (还有elseif (in_array("0", $group1iid2)) {条件将始终为假,因为$group1iid2中不存在"0"值)

As I can't test for real all this, Here is some kind of work around, based on your code (without any guaranty) :由于我无法对所有这些进行真实测试,因此根据您的代码(没有任何保证) ,这里有一些解决方法:

 add_action( 'user_register', 'add_product_to_cart' );
function add_product_to_cart( $user_id ) {
    if ( ! is_admin() && $user_id > 0 ) {
        
        // DEFINE BELOW THE META KEY TO GET THE VALUE FOR YOUR GROUP OF CURRENT USER
        $meta_key = 'your_group_meta_key';
        
        // Getting the current user group ID
        $user_promo_id = get_user_meta( $user_id, $meta_key, true );

        $group1_id1 = array('1', '2', '3', '4');
        $group1_id2 = array('5', '6', '7', '8');
        
        if (in_array( $user_promo_id, $group1_id1 ) ) {
            $product_id = 115;
        } elseif (in_array( $user_promo_id, $group1_id2 ) ) {
            $product_id = 219;
        } else {
            $product_id = 231;
        } 

        WC()->cart->add_to_cart( $product_id );
    }
}

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

相关问题 将元添加到购物车 WooCommerce 上的产品 - add meta to a product on cart WooCommerce 在Woocommerce 3中自动将产品添加到购物车 - Auto add a Product to Cart in Woocommerce 3 自动将产品添加到购物车,部分 WooCommerce 产品类别除外 - Auto add product to cart except for some WooCommerce product categories 在Woocommerce中自动添加或删除购物车中的免费赠品 - Auto add or remove a freebie product from cart in Woocommerce 将产品添加到购物车时自动添加包装 WooCommerce Multisite - Auto-add packaging when adding product to cart WooCommerce Multisite 根据 WooCommerce 购物车小计自动添加可变数量的特定产品 - Auto add a variable quantity of specific product based on WooCommerce cart subtotal 在 WooCommerce 中以编程方式自动将礼品产品变体添加到购物车? - Auto add to cart a Gift product variation programmatically in WooCommerce? 如果用户将特定类别的产品添加到购物车,则WooCommerce重定向 - WooCommerce Redirect if user add product from a certain category to cart Woocommerce-当用户是产品作者时,删除添加到购物车按钮 - Woocommerce - Remove add to cart button when user is the product author 如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车 - Avoid add to cart for specific product categories if user is unlogged in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM