在Woocommerce 3中自动将产品添加到购物车 [英] Auto add a Product to Cart in Woocommerce 3

查看:150
本文介绍了在Woocommerce 3中自动将产品添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望任何访问我的Woocommerce商店的人都能在他的购物车中找到免费的产品。我找到了这段代码,它可以正常运行,但是它在日志中显示了许多php错误

I would like that anybody who visits my Woocommerce shop finds a free product in his cart. I found this code and it works but it shows many php errors in logs.

您知道为什么不干净吗?

Do you have an idea why is not "clean"?

对此有任何帮助。

/*
 * AUTOMATICALLY ADD A PRODUCT TO CART ON VISIT
 */
function aaptc_add_product_to_cart() {
    if ( ! is_admin() ) {
        $product_id = 99999;  // Product Id of the free product which will get added to cart
        $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 {
            // if no products in cart, add it
            WC()->cart->add_to_cart( $product_id );
        }
    }
}
add_action( 'init', 'aaptc_add_product_to_cart' );

编辑后再回复
谢谢,您的代码没有更多错误,但是您可以检查是否可以根据SKU惊喜选择免费产品?再次感谢大家。

EDIT AFTER GREAT REPLY Thank you, no more errors with your code, but can you check if it's okay to select the free product based on SKU "Surprise" ? Thanks again for all.

    function sku2id( $sku = false ) {
    global $wpdb;
    if( !$sku )
        return null;
    $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) );
    if ( $product_id )
        return $product_id;
    return null;
}
add_action( 'template_redirect', 'auto_add_product_to_cart' );
function auto_add_product_to_cart() {
    if ( is_admin() ) return;

    // Product Id of the free product which will get added to cart;
    $product_id = sku2id("Surprise");  // FREE PRODUCT MUST HAVE SKU Surprise

    if ( WC()->cart->is_empty() ) 
    {
        // No products in cart, we add it
        WC()->cart->add_to_cart( $product_id ); 
    } 
    else
    {
        $found  = false;

        //check if product already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['data']->get_id() == $product_id )
                $found = true;
        }

        // if the product is not in cart, we add it
        if ( ! $found )
            WC()->cart->add_to_cart( $product_id ); 
    }
}


推荐答案

尝试而是使用template_redirect操作钩子重新访问了类似的代码:

Try instead this revisited similar code using template_redirect action hook:

add_action( 'template_redirect', 'auto_add_product_to_cart' );
function auto_add_product_to_cart() {
    if ( is_admin() ) return;

    // Product Id of the free product which will get added to cart;
    $product_id = 99999;

    if ( WC()->cart->is_empty() ) 
    {
        // No products in cart, we add it
        WC()->cart->add_to_cart( $product_id ); 
    } 
    else
    {
        $found  = false;

        //check if product already in cart
        foreach ( WC()->cart->get_cart() as $cart_item ) {
            if ( $cart_item['data']->get_id() == $product_id )
                $found = true;
        }

        // if the product is not in cart, we add it
        if ( ! $found )
            WC()->cart->add_to_cart( $product_id ); 
    }
}

代码进入function.php文件您的活动子主题(或活动主题)。经过测试并有效。


注意: init 钩在这里是不正确的,因此不能用于此……

Note: The init hook is not correct here and not to be used for this…

这篇关于在Woocommerce 3中自动将产品添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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