简体   繁体   中英

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. I found this code and it works but it shows many php errors in logs.

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" ? 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:

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). Tested and works.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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