简体   繁体   English

在Woocommerce 3中自动将产品添加到购物车

[英]Auto add a Product to Cart in Woocommerce 3

I would like that anybody who visits my Woocommerce shop finds a free product in his cart. 我希望任何访问我的Woocommerce商店的人都能在他的购物车中找到免费的产品。 I found this code and it works but it shows many php errors in logs. 我找到了这段代码,它可以正常工作, 但是它在日志中显示了许多php错误

Do you have an idea why is not "clean"? 您是否知道为什么不干净?

Any help on this please. 请对此提供任何帮助。

/*
 * 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' );

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" ? 再次答复后进行编辑谢谢,您的代码没有更多错误,但是您可以检查是否可以根据SKU“惊喜”选择免费产品吗? 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 ); 
    }
}

Try instead this revisited similar code using template_redirect action hook: 尝试使用template_redirect操作钩子重新访问类似的代码:

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

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

Note: The init hook is not correct here and not to be used for this… 注意: init钩在这里是不正确的,因此不能用于此…

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM