Removing auto added product from WooCommerce - php

So I can use this snippet from the woocommerce docs and it works to add a free product when a cart total is reached. However, our customers need to be able to remove the product from the cart if they don't want it (repeat customers that don't want the same gift every time). With this snippet, the remove from cart button doesn't work for the free product.
/**
* Add another product depending on the cart total
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 2831; //replace with your product id
$found = false;
$cart_total = 30; //replace with your cart total needed to add above item
if( $woocommerce->cart->total >= $cart_total ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->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 )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
}
``
code above is from here: https://docs.woocommerce.com/document/automatically-add-product-to-cart-on-visit/

Removing the product from the cart will actually work, but the problem is that the code will also automatically re-add the product again.
One way to solve this would be to remember that the product has already been added once, and to not re-add the product if the product would be added another time (after it has been removed from the cart for example).
A simple way to do that would be to save that data inside of the WooCommerce session, using WC()->session. Modified code below:
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 2831; //replace with your product id
$found = false;
$cart_total = 10; //replace with your cart total needed to add above item
if( $woocommerce->cart->total >= $cart_total && ! WC()->session->get( 'free_item_given', false ) ) { // <--- ADDED CHECK FOR free_item_given HERE
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->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 ) {
$woocommerce->cart->add_to_cart( $product_id );
WC()->session->set( 'free_item_given', true ); // <--- REMEMBER free_item_given HERE
}
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
WC()->session->set( 'free_item_given', true ); // <--- REMEMBER free_item_given HERE
}
}
}
}
I hope that it solves your problem!

Related

Woocommerce - automatically add product and double products in cart [duplicate]

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' );
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…

Add or remove automatically a free product in Woocommerce cart but not with subscription product on the cart

Hello this is similar question to the link below but just want to ask if it's possible to set a condition where it won't load the free product if the product that's been purchase is a subscription type? Thank you.
Add or remove automatically a free product in Woocommerce cart
/**
* Add another product depending on the cart total
*/
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 85942; //replace with your product id
$found = false;
$cart_total = 15; //replace with your cart total needed to add above item
if( $woocommerce->cart->total >= $cart_total ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
$isVirtualOnly = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values) {
$_product = $values[‘data’];
if ($_product != null)
if ($_product->get_type() != $_virtual)
$isVirtualOnly = false;
}
if ($isVirtualOnly != true) {
foreach ( $woocommerce->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 )
$woocommerce->cart->add_to_cart( $product_id );
}
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
}
/**
* END Add another product depending on the cart total
*/
add_action( 'template_redirect', 'add_product_to_cart_conditionally' );
function add_product_to_cart_conditionally() {
if ( is_admin() ) return; // Exit
// Below define the product Id to be added:
$product_A = 37; // <== For new customers that have not purchased a product before (and guests)
$product_B = 53; // <== For confirmed customers that have purchased a product before
$product_id = has_bought() ? $product_B : $product_A;
// If cart is empty
if( WC()->cart->is_empty() ) {
WC()->cart->add_to_cart( $product_id ); // Add the product
}
// If cart is not empty
else {
// Loop through cart items (check cart items)
foreach ( WC()->cart->get_cart() as $item ) {
// Check if the product is already in cart
if ( $item['product_id'] == $product_id ) {
return; // Exit if the product is in cart
}
}
// The product is not in cart: We add it
WC()->cart->add_to_cart( $product_id );
}
}

Woocommerce: add to cart automatically when visiting a page (variable product IDs)

I am aware that there's already an existing function where you can add a certain product on cart upon visiting the website. The problem is you can only do it with 1 product and I want it variable depending on the page you are visiting.
here's the existing code (added on functions.php file)
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 275;
$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 );
}
}
}
Inside my landing page called page-template.php, there's a custom field that echos the ID of a certain product selected. So when my user visits this page, I want this ID to replace the $product_id on the functions.php file. How do I achieve this?
Many thanks in advance for the help.
maybe the global variable is loaded after the function. instead of just setting the variable initiate the function passing the variable and avoid the hook:
in functions.php:
function add_product_to_cart($product_id) {
if ( ! is_admin() ) {
$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 );
}
}
}
in page-template.php:
add_product_to_cart(275);

Add item to cart automatically when another product is present

I'm using dynamic pricing to discount an item to free one another is present, i want to avoid having the client add the free product as a separate step and just let the system add it.
I started off with the snippet but I can not get this to work when the item is present
this is what i got so far:
<?php
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id_gift = 2287;
$found = false;
$product_id = 30;
$incart_free = false;
foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id ){
$incart_free = true;
}
return $incart_free;
}
if( $incart_free == true ) {
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id_gift )
$found = true;
}
// if product not found, add it
if ( $found != true )
$woocommerce->cart->add_to_cart( $product_id_gift );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id_gift );
}
}
}
}
add_action( 'init', 'add_product_to_cart' );
?>
Thank you!
If I understood your question correctly you are looking to make an item free if certain items are in the shopping cart. Here is my solution:
1. Create a coupon for WooCommerce in Wordpress. Make the coupon amount 100% and the discount type 'Product% Discount'. Go to Usage Restriction->Products and specify the specific product you want to be free, this will make the coupon only apply to that specific product.
2. Create a function that firsts checks if there are specific items present in the cart and if so, then adds and discounts the item you want to be free to the cart. The following code will do the trick (I have tested it and it worked fine, although it is not the cleanest solution):
add_action( 'init', 'product_discount' );
function product_discount(){
//variable declerations.
global $woocommerce;
$product_id = 1; // product to add
$products= array('2', '3', '4'); //specific product(s) to be present in the cart
$coupon_code = 'abc'; // coupon code from wp
//get the cart contents.
$cart_items = $woocommerce->cart->get_cart();
//check if the cart is not empty.
if(sizeof($cart_items) > 0){
//loop through the cart items looking for the specific products.
foreach ($cart_items as $key => $item){
//check if the cart items match to any of those in the array and check if the desired product is in the cart.
if(in_array($item['product_id'], $products) && $item['product_id'] != $product_id){
//add course.
$woocommerce->cart->add_to_cart($product_id);
//discount course.
$woocommerce->cart->add_discount(sanitize_text_field($coupon_code));
}else{
break; //to prevent the product from being added again for the next loop.
}
}
}
}
Hope this helps!
Your logic is completely wrong here because if ( $_product->id == $product_id_gift ) will never gonna be true as both product_id is different.
So logic should be :
1. Check all the products that are added in to the cart
2. Check that if any product in the cart having free products or not
3. If yes, then simply add the free product.
So code would be something like this :
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 30; //Your product ID here
$free_product_id = 2287; //Free product ID here
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->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 )
$woocommerce->cart->add_to_cart( $free_product_id );
}
}
}
NOTE: Untested.So let me know the output as well.

woocomerce Automatically add product to cart on visit - error

I'm trying to implement the following solution:
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'init', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
global $woocommerce;
$product_id = 64;
$found = false;
//check if product already in cart
if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
foreach ( $woocommerce->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 )
$woocommerce->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
$woocommerce->cart->add_to_cart( $product_id );
}
}
}
from here:
http://docs.woothemes.com/document/automatically-add-product-to-cart-on-visit/
but it throws an error:
Sorry, this product cannot be purchased.
Anyone knows what am I doing wrong?
maybe your product stock quantity is zero or product is not there. Please check that.

Categories