My WordPresss related task is simple, but I can't find the solution. I have 2 products in my woocommerce shop, and I would like to show both of them on the cart-page in the woocommerce table, let the customer set the quantity of them. If the customer don't want to buy something, just leave it on 0.
The problem is the cart table is not shown if it is empty and I can only see the items that I put in there.
you can add product like that and add condition according you i set for admin and product id. change product id with your product id
/*
* goes in theme functions.php or a custom plugin
**/
// add item to cart on visit
add_action( 'template_redirect', 'add_product_to_cart' );
function add_product_to_cart() {
if ( ! is_admin() ) {
$product_id = 64;
$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 );
}
}
}
Related
I'm trying to figure out how to add a specific item to cart, if this variation ID is added to cart. For example I have like 4 variations of a configurable product.
11264
11265
11266
11267
If product variation 11264 and 11265 is added to cart, then product with product ID 111 should also be added to cart. If 11266 and 11267 is added to cart, then product with product ID 222 should also be added to cart.
I found this code online that can add an item to cart based on the product category:
function aaptc_add_product_to_cart( $item_key, $product_id ) {
$product_category_id = 123; // cricket bat category id
$product_cats_ids = wc_get_product_term_ids( $product_id, 'product_cat' );
if ( ! is_admin() && in_array( $product_category_id, $product_cats_ids ) ) {
$free_product_id = 1522; // 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() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
}
}
add_action( 'woocommerce_add_to_cart', 'aaptc_add_product_to_cart', 10, 2 );
That code works as intended. But now I want to make it conditional. So I modified this line:
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
And changed it to:
if ($found && WC()->cart->(11264,11265))
WC()->cart->add_to_cart(111);
So that if variation ID 11264 or 11265 exists in cart, then add product ID 111. I think my syntax is wrong, but I'm not sure what's the next step. Tried googling it for hours but I cannot find any decent examples.
Slight Modification in foreach loop to get & compare variations might resolve the issue
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$variation_ids = array(11264,11265);
//check if variation exist in cart & if found add product in cart with ID 111
if( in_array( $values['variation_id'], $variation_ids ) ) {
WC()->cart->add_to_cart(111);
}
else {
//Do something
}
}
So I have almost achieved my goal - to create a new discounted price on a specific product, should another specific product be in the customer's cart.
I am using ACF to select the core product and the discounted one, which is all being pulled fine. The issue lies in the order in which the products are added to the cart.
If I add the discounted product BEFORE the core product, the discounted product correctly adjusts to $9.99 (the ACF determined new price). However, if I add the core product first, and THEN add the product that should be discounted, the price remains the same - with no discount applied.
I used this code for reference: https://stackoverflow.com/a/47500323/16291715
My code:
add_action( 'woocommerce_before_calculate_totals', 'boga_discount', 20, 1 );
function boga_discount( $cart ) {
if (is_admin() && !defined('DOING_AJAX'))
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// First loop to check if CORE product is in cart
foreach ( $cart->get_cart() as $cart_item ){
$is_in_cart = $cart_item['product_id'] == 1249 ? true : false;
}
// Second loop change prices
foreach ( $cart->get_cart() as $cart_item ) {
// Get an instance of the WC_Product object (or the variation product object)
$product = $cart_item['data'];
// Here we target DISCOUNT ID 17
if( $product->get_id() == 1361 ) {
// GET THE NEW PRICE
$new_price = 9.99; // <== Add your code HERE
// When product CORE product is in cart
if( $is_in_cart ){
$product->set_price( $new_price );
}
}
}
}
I can't understand why this would happen for the life of me, but I am sure there is some minor detail I am missing.
In the first loop you have
$is_in_cart = $cart_item['product_id'] == 813 ? true : false;
Suppose there are 2 products in the cart 813 & 815.
While going through the cart items (first loop), suppose product 813 is first found, so $is_in_cart is true. Then in the same loop, however, $cart_item['product_id'] is now equal to 815. The condition says again, it is equal to 813? this does not match, so $is_in_cart will be false. That's the issue
So you don't need to use 2 foreach loops, this should suffice:
function action_woocommerce_before_calculate_totals( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// Settings
$core_product = 813;
$discounted_product = 815;
$new_price = 9.99;
// Initialize
$flag_core_product = false;
$flag_discounted_product = false;
// Loop through cart items
foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
// Compare
if ( $cart_item['product_id'] == $core_product ) {
$flag_core_product = true;
} elseif ( $cart_item['product_id'] == $discounted_product ) {
$flag_discounted_product = true;
// Store data (discounted product)
$cart_item_data = $cart_item['data'];
}
}
// Both are true
if ( $flag_core_product && $flag_discounted_product ) {
// Set new price
$cart_item_data->set_price( $new_price );
}
}
add_action( 'woocommerce_before_calculate_totals', 'action_woocommerce_before_calculate_totals', 10, 1 );
Description:
I have use case where I need to check if a specific product has been added to cart before customer adds another products to cart.
Background:
We are renting products out based on packages(which are products). Packages include multiple products and products do not have price, packages do. So basically you would need to add a package(with price) and products(with no price) to cart.
Problem:
At the moment customers can add products before packages and they can continue to shopping cart with total zero.
Some of the progress what I have:
if ( ! WC()->cart->is_empty() )
foreach( WC()->cart->get_cart() as $cart_item )
// now i would need to check if cart consists a package
And i could solve adding product to cart with no package in cart like this:
if (WC()->cart->is_empty() )
//then don't add product to cart, tell customer to add package first.
Use the woocommerce_add_to_cart_validation hook
Comment with explanation added in the code
function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
// Product (ID) in cart
$product_first_in_cart = 30;
// Compare
if ( $product_first_in_cart != $product_id ) {
// Set variable
$in_cart = false;
// Cart NOT empty
if ( ! WC()->cart->is_empty() ) {
// Loop trough cart
foreach( WC()->cart->get_cart() as $cart_item ) {
// Search for the specific product
if ( $cart_item['data']->get_id() == $product_first_in_cart ) {
// Found, break loop
$in_cart = true;
break;
}
}
}
// NOT in cart
if ( ! $in_cart ) {
wc_add_notice( __( 'Please add product "A" before adding other products', 'woocommerce' ), 'error' );
$passed = false;
}
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
I'm using the following code to add a free product to the basket when over a certain cart total.
When I drop below the cart total it hits the if statement but won't remove the product.
I think this may be because the quantity is set to 1 on the basket form and my code isn't overriding that quantity to set it to 0 (and remove it).
Is there another way to remove it or override it?
/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 20;
$free_product_id = 85028; // Product Id of the free product which will get added to cart
if ( $woocommerce->cart->total >= $cart_total ) {
echo "Over the limit";
$quantity = 1;
WC()->cart->add_to_cart( $free_product_id, $quantity );
} elseif($woocommerce->cart->total < $cart_total) {
echo "Under the limit";
$quantity = 0;
WC()->cart->remove_cart_item( $free_product_id, $quantity );
}
}
add_action( 'template_redirect', 'aapc_add_product_to_cart' );
I've tried using this question but cannot get it to work, it won't even add the item to my basket.
I'm using Woocommerce version 3.6.5 if that helps.
I'm not sure on how Woocommerce works but I assume the remove_cart_item 'quantity' parameter takes the amount of the item to remove from the basket. So set the value of $quantity to 1 and it should remove one of the free product from the basket. If there happens to be more than one free item in the basket then you'll need to set your $quantity value accordingly.
Furthermore, you are adding a free item every time a product is added when the basket value is higher than $cart_total. Maybe add a check to see if it already exists in the basket before adding it?
Hope I understood your question correctly
/*
* Automatically adding the product to the cart when cart total amount reach to £20.
*/
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 20;
$free_product_id = 85028; // Product Id of the free product which will get added to cart
if ( WC()->cart->total >= $cart_total ) {
$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() == $free_product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $free_product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id );
}
} elseif(WC()->cart->total < $cart_total) {
$quantity = 0;
$prod_unique_id = WC()->cart->generate_cart_id( $free_product_id );
// Remove it from the cart by un-setting it
unset( WC()->cart->cart_contents[$prod_unique_id] );
WC()->cart->remove_cart_item( $free_product_id );
}
}
add_action( 'woocommerce_after_calculate_totals', 'aapc_add_product_to_cart' );
IN WooCommerce I would like to add 10% of discount with WooCommerce Coupon feature, only when customer purchases products from 3 different product categories (like Category1, Category2, Category3).
How this can be done with WooCommerce Coupon feature?
Any help on this will be appreciated.
Update note: I only 3 parent product categories with no sub categories. Each product is assigned to one category. Some products are variable and some others are simple.
Here's a solution that is not using coupon codes that I recycled from this previous question.
add_action( 'woocommerce_cart_calculate_fees' , 'add_multiple_category_discount' );
function add_multiple_category_discount( $cart ){
if( $cart->cart_contents_count < 3 ){
return;
}
$product_cats = array();
foreach( $cart->get_cart() as $item ) {
$product = wc_get_product( $item['product_id'] );
foreach( $product->get_category_ids() as $key => $cat_id ) {
if( ! in_array( $cat_id, $product_cats ) )
$product_cats[] = $cat_id;
}
}
// If we have 3 distinct categories then apply a discount
if( count( $product_cats ) >= 3 ) {
// Add a 10% discount
$discount = $cart->subtotal * 0.1;
$cart->add_fee( 'You have 3 different product categories in your cart, a 10% discount has been added.', -$discount );
}
}
To handle this functionality with a coupon you will need to have one product category by product or one parent category by product, because a product can have many categories and subcategories set for it.
This custom function will add a coupon discount when cart items are from 3 different product categories. If cart items are removed from cart and there is not anymore 3 different product categories, the coupon code will be auto removed.
Also you will need to set in the function the coupon code name and an array of all your matching product categories IDs.
Here is the code:
add_action( 'woocommerce_before_calculate_totals', 'add_discount_for_3_diff_cats', 10, 1 );
function add_discount_for_3_diff_cats( $wc_cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// HERE set your coupon code and your parent product categories in the array
$coupon_code_to_apply = 'summer';
// HERE define your product categories IDs in the array
$your_categories = array( 11, 13, 14 ); // IDs
// If coupon is already set
if( $wc_cart->has_discount( $coupon_code_to_apply ) )
$has_coupon = true;
foreach( $wc_cart->get_cart() as $cart_item ) {
$product_id = $cart_item['product_id'];
$product = wc_get_product($product_id);
foreach( $product->get_category_ids() as $category_id ) {
if( has_term( $your_categories, 'product_cat', $product_id ) && in_array( $category_id, $your_categories ) ){
// Set the categories in an array (avoiding duplicates)
$categories[$category_id] = $category_id;
}
}
}
$count_cats = count($categories);
$has_discount = $wc_cart->has_discount( $coupon_code_to_apply );
if ( 3 <= $count_cats && ! $has_discount ) {
$wc_cart->add_discount($coupon_code_to_apply);
} elseif ( 3 > $count_cats && $has_discount ) {
$wc_cart->remove_coupon($coupon_code_to_apply);
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works with simple and variable products…