Certain products when have tag value "buynow" or categories by featue products added to cart I want to remove all other product in cart and dont allow to add more product in cart. When one user added one product of this type and tries to add another product in cart, It will show error notice.
I have tried this code for product id 1024
add_filter( 'woocommerce_add_cart_item_data', 'wdm_empty_cart', 10, 3);
function wdm_empty_cart( $cart_item_data, $product_id, $variation_id )
{
global $woocommerce;
if($product_id == '1024'){
$woocommerce->cart->empty_cart();
wc_add_notice( 'Error!', 'error' );
}
elseif ( WC()->cart->get_cart_contents_count() >=1 ){
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$cartproductid[] = $_product->ID;
}
foreach($cartproductid as $id){
if($id == 1024){
wc_add_notice( 'Error!', 'error' );
$woocommerce->cart->empty_cart();
$woocommerce->cart->add_to_cart('1024');
}
}
}
return $cart_item_data;
}
My code works as follows:
When user add 1024 product id to the cart it empty the cart and add only the 1024 product. when user add product after adding 1024 not the same product it adds one more to the cart the stops adding further product.
My requirement is when 1024 product added to the cart no other product can not add to the cart. When user try to add it shows the notice "Error!"
Any solution is appreciated. Thanks in advance.
Anyone's help appreciated.
Instead of woocommerce_add_cart_item_data you can use woocommerce_add_to_cart_validation. this works.
add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validation_CB', 30, 3);
function woocommerce_add_to_cart_validation_CB( $bool, $product_id, $quantity )
{
global $woocommerce;
if($product_id == '1024'){
$woocommerce->cart->empty_cart();
}
elseif ( WC()->cart->get_cart_contents_count() >= 1 ){
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
if( '1024' == $values['product_id'] ){
wc_add_notice( 'Error!', 'error' );
return false;
}
}
}
return $bool;
}
If you want do this for featured product then use following code.
add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validation_CB', 30, 3);
function woocommerce_add_to_cart_validation_CB( $bool, $product_id, $quantity )
{
global $woocommerce;
if( isFeaturedProduct($product_id)){
$woocommerce->cart->empty_cart();
}
elseif ( WC()->cart->get_cart_contents_count() >= 1 ){
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
if(isFeaturedProduct($values['product_id']) ){
wc_add_notice( 'Error!', 'error' );
return false;
}
}
}
return $bool;
}
function isFeaturedProduct( $product_id ){
$wc_product_obj = new WC_Product($product_id);
return $wc_product_obj->is_featured();
}
Related
I am trying to add different product in cart. There are two type of product one is other and another is simple.
I need to grab if simple product in the cart then only simple can added and other in cart then only other.
I don't want to add simple and other type of product simultaneously.
add_action('woocommerce_add_to_cart', 'custome_add_to_cart', 10, 6);
function custome_add_to_cart($cart_id, $product_id, $request_quantity, $variation_id, $variation, $cart_item_data) {
global $woocommerce;
//print_r($cart_item_data); //Current
// die();
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product_type = $cart_item['type'];
if ($product_type == 'other'){
if($cart_item_data['type'] == 'other'){
WC()->cart->add_to_cart( $product_id );
// print_r($cart_item); //Old Cart
// die();
}else{
wc_print_notice( 'This type of product not added 1', 'notice' );
}
}else{
//print_r($cart_item); //Old Cart
if($cart_item_data['type'] != 'other'){
WC()->cart->add_to_cart( $product_id );
}else{
wc_print_notice( 'This type of product not added 2', 'notice' );
exit;
}
}
}
}
}
I am using this hook but it's not working for me. It show 503 error.
Instead of using woocommerce_add_to_cart use woocommerce_add_to_cart_validation
This filter returns $passed, $product_id, $quantity, $variation_id, $variations but we need only the type of product.
add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_to_cart_validationcustom', 10, 2 );
function woocommerce_add_to_cart_validationcustom( $passed, $product_id ) {
global $woocommerce;
if(WC()->cart->is_empty()) return true;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$current_type = $product->get_type();
}
//Get the product we are adding as object
$product_to_add = wc_get_product( $product_id );
$type_to_add = $product_to_add->get_type();
// Check if the product type is the same
if ( $current_type !== $type_to_add){
wc_add_notice( sprintf( __( "This is my custom error", "your-theme-language" ) ) ,'error' );
return false;
} else {
return true;
}
}
I want to add automatically one variations product into cart when cart total is become more than 25. The automatically adding variation product price need to set as 0 and also customers cant change the qty or need to disable the Qty filed for free product. I added below code and its working fine only the issue when page reloading or cart updating the the Qty of free product is increasing every time.I only want to sell 1 qty of free product. How to do that ? Code is following below.
function aapc_add_product_to_cart() {
global $woocommerce;
$cart_total = 25;
if ( $woocommerce->cart->total >= $cart_total ) {
if ( ! is_admin()) {
$free_product_id = 2696;
$variation_id = 2697;
$arr = array();
$arr['Color'] = 'Blue';// 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,1,$variation_id,$arr );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $free_product_id,1,$variation_id,$arr );
}
}
}
}
add_action( 'template_redirect', 'aapc_add_product_to_cart' );
function add_custom_price( $cart_object ) {
$cart_total = 25; // If cart total is more than this value.
$free_product_id = 2697; // Set price to 0 of this free product.
$carttotal = 0;
foreach ( $cart_object->cart_contents as $key => $value ) {
$_product = $value['data'];
if ( $_product->get_id() == $free_product_id ){
continue;
}
$carttotal += $value['line_total'];
}
if ( $carttotal >= $cart_total ) {
$custom_price = 0; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$_product = $value['data'];
if ( $_product->get_id() == $free_product_id ){
$value['data']->set_price( $custom_price );
}
}
}
}
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
add_filter( 'woocommerce_quantity_input_args', 'hide_quantity_input_field', 20, 2 );
function hide_quantity_input_field( $args, $product ) {
// Here set your product IDs in the array
$product_ids = array(2697);
// Handling product variation
$the_id = $product->is_type('variation') ? $product->get_variation_id() : $product->get_id();
//var_dump($the_id);
// Only on cart page for a specific product category
if( is_cart() && in_array( $the_id, $product_ids ) ){
$input_value = $args['input_value'];
$args['min_value'] = $args['max_value'] = $input_value;
}
return $args;
}
Try adding below code in the functions.php and replace with required product id
add_filter( 'woocommerce_add_to_cart_validation', 'allowed_products_variation_in_the_cart', 10, 5 );
function allowed_products_variation_in_the_cart( $passed, $product_id, $quantity, $variation_id, $variations) {
$product_b = 14091;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$cart_product_id = $cart_item['product_id'];
if ($cart_item['variation_id']) {
$cart_product_id = $cart_item['variation_id'];
}
if ( ( $variation_id == $product_b) ) {
wc_add_notice(__('You can\'t add this product.', 'domain'), 'error');
$passed = false; // don't add the new product to the cart
// We stop the loop
break;
}
}
return $passed;
}
I have one variable product X that product has two variations, Variations A and Variations B.
If a customer added variation A in cart then I want to block the customer to add variations B in cart. At a time customer only order one of variations of that product.
I have added below code but it's not working well because if I added one variations of product X in cart then I tried to add another product Y in the cart it's not adding to cart.
My code current code is the following.
function wph_add_the_cart_validation_for_zoomarine_e_ticket( $passed ) {
// The product id of variable product X
$product_id = 44050;
$in_cart = false;
foreach( WC()->cart->get_cart() as $cart_item ) {
$product_in_cart = $cart_item['product_id'];
if ( $product_in_cart === $product_id ) $in_cart = true;
}
if ( $in_cart ) { ?>
<script type="text/javascript">
alert("The product is already in cart. You can only add one E ticket per order");
</script>
<?php
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'wph_add_the_cart_validation_for_zoomarine_e_ticket', 10, 5 );
The following code solved my issue.I got the answer from here https://wordpress.stackexchange.com/questions/349916/allow-only-1-quantity-of-particular-product-in-cart-woocommerce/349929#349929
add_filter( 'woocommerce_add_to_cart_validation', 'allowed_products_variation_in_the_cart', 10, 5 );
function allowed_products_variation_in_the_cart( $passed, $product_id, $quantity, $variation_id, $variations) {
$product_a = 14576;
$product_b = 14091;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$cart_product_id = $cart_item['product_id'];
if ($cart_item['variation_id']) {
$cart_product_id = $cart_item['variation_id'];
}
if ( ($cart_product_id == $product_a && $variation_id == $product_b) || ($cart_product_id == $product_b && $variation_id == $product_a) ) {
wc_add_notice(__('You can\'t add this product.', 'domain'), 'error');
$passed = false; // don't add the new product to the cart
// We stop the loop
break;
}
}
return $passed;
}
I have a woocommerce site I use the booster plugin to enable sold individually for each product but now I want to sold individually for each category and if someone add a product to cart on a category then can not add another product of that category to cart
In the code below there is 2 functions:
The 1st one is a conditional function checking categories in cart items
The 2nd one will avoid add to cart when a product category already exist in cart items and will display a custom message.
The code:
// Conditional function: Checking product categories in cart items
function check_cats_in_cart( $product_id ) {
$taxonomy = 'product_cat';
$has_term = true;
foreach( WC()->cart->get_cart() as $item ){
foreach( wp_get_post_terms( $item['product_id'], $taxonomy ) as $term ){
// Allowing add to cart the same product
if( has_term( $term->slug, $taxonomy, $product_id ) ){
$has_term = false;
break; // stops the 2nd loop
}
}
// Allowing the same product to be added (not activated. you can uncomment lines below)
# if ( $item['product_id'] == $product_id )
# $has_term = true;
if( $has_term )
break; // stops the 1st loop
}
return $has_term;
}
// Avoid add to cart when a product category already exist in cart items, displaying a custom error message
add_filter( 'woocommerce_add_to_cart_validation', 'sold_individually_by_cats_valid', 10, 3 );
function sold_individually_by_cats_valid( $passed, $product_id, $quantity) {
$passed = check_cats_in_cart( $product_id );
if( ! $passed ){
// Displaying a custom message
$message = __("This product category is already in cart. Try another product", "woocommerce");
wc_add_notice( $message, 'error' );
}
return $passed;
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
If you want to allow adding to cart again the same product (that increases only the quantity of an existing product), you will uncomment this code in the function:
if ( $item['product_id'] == $product_id )
$has_term = true;
You can try it this one
function is_product_the_same_cat($valid, $product_id, $quantity) {
global $woocommerce;
if($woocommerce->cart->cart_contents_count == 0){
return true;
}
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms( $_product->id, 'product_cat' );
$target_terms = get_the_terms( $product_id, 'product_cat' );
foreach ($terms as $term) {
$cat_ids[] = $term->term_id;
}
foreach ($target_terms as $term) {
$target_cat_ids[] = $term->term_id;
}
}
$same_cat = array_intersect($cat_ids, $target_cat_ids);
if(count($same_cat) > 0) return $valid;
else {
wc_add_notice( 'This product is in another category!', 'error' );
return false;
}
}
add_filter( 'woocommerce_add_to_cart_validation', 'is_product_the_same_cat',10,3);
I found it from here
I am attempting to have php check if the cart has 2 items specified from an array before applying a free product and coupon. This specific example I've included is checking the price is over a certain amount. I have tried this because I could not get the two items to work.
function bbloomer_add_gift_if_sku_added_cart( $passed, $product_id, $quantity ) {
global $woocommerce;
$skuswithgift = array('SMWB-M23','001-SLW');
$giftsku = 'comb';
$coupon_code = 'combfree';
$product = wc_get_product( $product_id );
if ($product->get_sku() && in_array($product->get_sku(), $skuswithgift) && $woocommerce->cart->total > 26.00) {
WC()->cart->add_to_cart( wc_get_product_id_by_sku($giftsku) );
wc_add_notice( __( 'Hey there! As promised, you recieved a free comb with the purchase of two towels and we added it to your cart for you!', 'woocommerce' ), 'success' );
$woocommerce->cart->add_discount( $coupon_code );
}
else {
WC()->cart->remove_cart_item( wc_get_product_id_by_sku($giftsku) );
$woocommerce->cart->remove_coupon( $coupon_code );
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_add_gift_if_sku_added_cart', 10, 3 );
Tried this and it's not working
function bbloomer_add_gift_if_sku_added_cart( $passed, $product_id, $quantity ) {
global $woocommerce;
$skuswithgift = array('SMWB-M23','001-SLW');
$giftsku = 'comb';
$coupon_code = 'combfree';
$product = wc_get_product( $product_id );
$total_towels = 0;
// Determine how many towels there are
// Loop through every cart item
foreach ( $woocommerce->cart->get_cart() as $cart_item ) {
$product = $cart_item['data'];
//$product = $cart_item;
// Is it in Gift SKUs
$is_towel = in_array($product->get_sku(), $skuswithgift);
if($is_towel){
// Add this quantity to the total towels
$total_towels += intval($cart_item['quantity']);
}
}
// Apply Discount
if ($total_towels <= 2) {
WC()->cart->add_to_cart( wc_get_product_id_by_sku($giftsku) );
wc_add_notice( __( 'Hey there! As promised, you recieved a free comb with the purchase of two towels and we added it to your cart for you!', 'woocommerce' ), 'success' );
$woocommerce->cart->add_discount( $coupon_code );
}
else {
WC()->cart->remove_cart_item( wc_get_product_id_by_sku($giftsku) );
$woocommerce->cart->remove_coupon( $coupon_code );
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'bbloomer_add_gift_if_sku_added_cart', 10, 3 );
Try below code
add_action('woocommerce_add_to_cart_redirect','customeApplyCode');
function customeApplyCode()
{
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$productSku = array();
foreach($items as $item => $values) {
// Retrieve WC_Product object from the product-id:
$product = wc_get_product( $values['product_id'] );
// Get SKU from the WC_Product object:
$productSku[] = $product->get_sku();
}
$skuswithgift = array('SMWB-M23','001-SLW');
$result=array_intersect($skuswithgift,$productSku);
if(count($result)>0)
{
//product id of free product
$product_id = 1312;
$woocommerce->cart->add_to_cart($product_id);
add_filter( 'woocommerce_product_needs_shipping', 'hide_shipping_when_free_is_available', 10, 2 );
}
}
function hide_shipping_when_free_is_available( $rates, $package ) {
return false;
}