I want to force sell based on the product quantity if the customer adds 5 product 1 accessories should add. if they add more then 5, 2 accessories should add.
so the accessories should add in the multiple of five.
Try this:
add_action( 'woocommerce_add_to_cart', 'prefix_add_additional_product', 10, 6 );
/**
* Adds additional product to cart based on the quantity of an added product
*/
function prefix_add_additional_product( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// Get the cart
$cart = WC()->cart->get_cart();
// Nothing, if the cart is empty
if ( 0 == count( $cart ) ) {
return;
}
// Enter the free product ID
$additional_product_id = 96;
// Don't add accessory when adding the same accessory
$added_product_id = 0 < $variation_id ? $variation_id : $product_id;
if ( $added_product_id == $additional_product_id ) {
return;
}
// You can add checks for specific product ID or some other requirements here
$additional_product_quantity = (int) ( $quantity / 5 );
WC()->cart->add_to_cart( $additional_product_id, $additional_product_quantity );
}
https://gist.github.com/vanbo/f42cfa8bdf593013f77dda605a9876bb
The code should add the $additional_product_id to cart with a quantity that matches the amount of times the added product to cart quantity can be devided by 5.
The code is hooked to the add to cart hook.
Related
This code currently works to clear cart before adding a product to cart.
// Empty cart when product is added to cart, so we can't have multiple products in cart
add_action( 'woocommerce_add_cart_item_data', function( $cart_item_data ) {
wc_empty_cart();
return $cart_item_data;
} );
However I'm having trouble with making it product specific.
I'd like it to function as - if product ID#1 is added to cart, empty cart then add product (at whatever quantity).
How can I determine via the woocommerce_add_cart_item_data hook which product is added to the cart?
The woocommerce_add_cart_item_data hook contains 3 arguments versus 1,
and since $product_id is the 2nd it can be used to answer your question:
function filter_woocommerce_add_cart_item_data( $cart_item_data, $product_id, $variation_id ) {
// Get product ID
$product_id = $variation_id > 0 ? $variation_id : $product_id;
// When product ID is in array (multiple product IDs can be added, separated by a comma)
if ( in_array( $product_id, array( 1, 32, 30, 817 ) ) ) {
wc_empty_cart();
}
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'filter_woocommerce_add_cart_item_data', 10, 3 );
I am using Woocommerce plugin with WP and when I add to cart an item and then I add to cart the same item again, at the cart page it's displayed as one item with quantity of two and with one order_id. What I want is to be able to add to cart multiple times the same product and each product to get a unique order_id. Is there a way to do that? Thank you.
function bbloomer_split_product_individual_cart_items( $cart_item_data, $product_id ){
$unique_cart_item_key = uniqid();
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'bbloomer_split_product_individual_cart_items', 10, 2 );
//add_filter( 'woocommerce_is_sold_individually', '__return_true' );
#***************************************************
add_action( 'woocommerce_add_to_cart', 'mai_split_multiple_quantity_products_to_separate_cart_items', 10, 6 );
function mai_split_multiple_quantity_products_to_separate_cart_items( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// If product has more than 1 quantity
if ( $quantity > 1 ) {
// Keep the product but set its quantity to 1
WC()->cart->set_quantity( $cart_item_key, 1 );
// Run a loop 1 less than the total quantity
for ( $i = 1; $i <= $quantity -1; $i++ ) {
/**
* Set a unique key.
* This is what actually forces the product into its own cart line item
*/
$cart_item_data['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );
// Add the product as a new line item with the same variations that were passed
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item_data );
}
}
}
I want to split cart items of same product in individual lines. When I increase quantity on single product page and add to cart, it shows as separate cart items.
Im using WooCommerce - Treat cart items separate if quantity is more than 1 answer code.
I want when quantity is updated on cart page and clicked on 'Update cart', then items must split on individual lines.
How can I do that?
The code below will split items on individual lines when quantity is updated on the cart page.
Comment with explanation added to the code.
function on_action_cart_updated( $cart_updated ) {
if ( $cart_updated ) {
// Get cart
$cart_items = WC()->cart->get_cart();
foreach ( $cart_items as $cart_item_key => $cart_item ) {
$quantity = $cart_item['quantity'];
// If product has more than 1 quantity
if ( $quantity > 1 ) {
// Keep the product but set its quantity to 1
WC()->cart->set_quantity( $cart_item_key, 1 );
// Run a loop 1 less than the total quantity
for ( $j = 1; $j <= $quantity -1; $j++ ) {
// Set a unique key.
$cart_item['unique_key'] = md5( microtime() . rand() . "Hi Mom!" );
// Get vars
$product_id = $cart_item['product_id'];
$variation_id = $cart_item['variation_id'];
$variation = $cart_item['variation'];
// Add the product as a new line item with the same variations that were passed
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $variation, $cart_item );
}
}
}
}
}
add_action( 'woocommerce_update_cart_action_cart_updated', 'on_action_cart_updated', 20, 1 );
I used this code to filter the products added to the cart of a certain category (id = 70).
My goal is to count the number of step quantities added to the cart of a product belonging to the '70' category.
Example:
Minimum quantity: 0
Step: 100
I added the product to the cart with 300 quantities.
So the step number is 3.
Is there a way to get the number of step quantities of a product added to the cart?
// Utility function that count specific product category cart items
add_shortcode('palletcompleto', 'cat_cart_count');
function cat_cart_count( $term_ids ) {
$quantity = 0; // Initializing
// Loop through cart items
foreach ( WC()->cart->get_cart() as $cart_item ) {
if ( has_product_category( $cart_item['product_id'], array( 70 ) ) ) {
$quantity += $cart_item['step_quantity'];
}
}
// Returning category count
return $quantity == 0 ? false : $quantity;
}
I am using some code like has_product_category() function from this thread:
Set item quantity to multiples of “x” for products in a specific category in Woocommerce
// Custom conditional function that checks also for parent product categories
function has_product_category( $product_id, $category_ids ) {
$term_ids = array(); // Initializing
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, 'product_cat' ) as $term ){
if( $term->parent > 0 ){
$term_ids[] = $term->parent; // Set the parent product category
$term_ids[] = $term->term_id;
} else {
$term_ids[] = $term->term_id;
}
}
return array_intersect( $category_ids, array_unique($term_ids) );
}
I want to apply the discount coupon to the specific product in cart from checkout page.
Inside the cart:
product1 w/ upgrades
product2 w/o upgrades
when I apply the discount coupon I want that the discount will take effect only in product2's price. I tried to search in some forums but the coupon will take effect in the whole cart items.
You can try this piece of code:
function calculate_variation_addon_price( $cart_object ) {
global $isProcessed;
// Loop for all products in cart
foreach ( $cart_object->cart_contents as $key => $value ) {
// Your condition here for product specific i.e. id == 25
if((!isset($isProcessed) || empty($isProcessed))) {
$orgPrice = floatval( $value['data']->get_price() );
$cart_object->cart_contents[$key]['data']->set_price( $orgPrice * 0.5 );
}
}
$isProcessed = 1;
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_variation_addon_price', 2, 1 );
While adding coupon from admin panel, find a tab there, called 'Usage Restriction'.
There you can add products or product categories or even exclude some for the coupon code to be applied.
You can so that using product id, sku or name
function filter_woocommerce_coupon_get_discount_amount( $discounting_amount, $price_to_discount , $cart_item, $single, $coupon ) {
// On backorder
if ( $cart_item['data']->get_id() !== 'product2_id' ) {
$discounting_amount = 0;
}
return $discounting_amount;
}
add_filter( 'woocommerce_coupon_get_discount_amount', 'filter_woocommerce_coupon_get_discount_amount', 10, 5 );
You can use in_array instead of !== for multiple products
You also replace get_id() by other properties or use custom attributs...