Update quantity based on variation to cart in WooCommerce - php

I have custom coded Woocommerce template, and used variation to make a bulk discount for my site. For example i have like this one:
but when select Buy 2 for 159.99 variation and press "Add to Cart" button, into cart, for quantuty still stay 1 but should be 2 based on my selected variation. I want to modify my code or function to get updated quantity into cart, based on selected variation. I have hidden my quantity selector, on my site using this function:
function custom_remove_all_quantity_fields( $return, $product ) {return true;}
add_filter( 'woocommerce_is_sold_individually','custom_remove_all_quantity_fields', 10, 2 );
Some tip or idea, how to get this working?

Related

Empty function clears grouped product

I used the following function to stop people from adding multiple products to their cart, e.g. someone clicks the link and doesn’t buy, then clicks the link again and there’s x2 in their cart.
add_action( 'woocommerce_add_cart_item_data', 'woocommerce_clear_cart_url' );
function woocommerce_clear_cart_url() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
This works for Single Product Checkout links (/checkout/?add-to-cart=53007) , but not for Grouped Products.
With a Grouped Product Checkout link, e.g. /checkout/?add-to-cart=53007&quantity[48582]=1&quantity[46646]=1&quantity[29667]=1, only 1 product from the group is in the checkout, the others have been removed by the above function.
How can I change this function to allow grouped products to work ?

WooCommerce - Empty the current cart on add-to-cart event

So I'm selling a digital product with WooCommerce but I don't have a shop page or anything, I just have a big CTA button on my landing page which adds the given product ID to the cart and proceeds to the checkout page upon click.
But let's say the user wants to go back to the landing page to double-check something and complete the checkout process then the product quantity becomes 2 and they can't edit the cart because I've removed all of the shop pages except for the checkout.
So I need to write a hook/filter function that tells WC to empty the current cart before adding the new product to the cart but I also have this upsell plugin that promotes my upsell products on the checkout submit event, right before the payment is charged.
As you can see, I need to check the product ID so my function wouldn't empty the cart if it's an Upsell product, which would be something like the example below, in pseudo-code;
add_filter('woocommerce_add_to_cart_validation', function(...$filterArgs){
(int) $productID; // ID of the product being added to cart
(int) $upsellProductID; // ID of the Upsell product
(bool) $isCartEmpty; // check if the current cart is empty or not
if($productID !== $upsellProductID && $isCartEmpty) {
emptyCartFunctionOfWooCommerce();
return $cartVariableFromFilterArgs
}
});
I'm not fluent in WP hooks yet so do you have any idea how can I do this? Thank you so much.
EDIT: I just did it without ID checking and it didn't interfere with the Upsell function. If anyone has the same issue, you can use something like the code below.
// This will empty the current cart before adding the product to the cart.
add_filter( 'woocommerce_add_to_cart_validation', function($passed, $product_id, $quantity){
if (!WC()->cart->is_empty()) {
WC()->cart->empty_cart();
}
return $passed;
}, 20, 3 );
Edit the product, go to Inventory, and select "Sold individually" "Enable this to only allow one of this item to be bought in a single order". This will stop customers adding more than 1 of that product to the cart.
Once you have done this, change your button to an <a> tag.
Buy Now
This should take you to the cart page and add the product to your cart, but limit it to 1.

Woocommerce: Custom field per product on the cart page

I'm building a website for a wholesale business that is selling plants. They want to provide their customers an option on the cart page to add labels to the plants. Therefore I want to add a custom input field per product in the cart where customers can type in the amount of plants they need. When the update cart button is clicked I want the amount of labels to be added to the cart item data.
I found that I can use woocommerce_add_cart_item_data hook this way:
add_filter( 'woocommerce_add_cart_item_data', 'add_labels', 10, 3 );
function add_labels( $cart_item_data, $product_id, $variation_id ) {
$cart_item_data['amount_of_labels'] = 200;
return $cart_item_data;
}
And when a product is added to the cart, a value of 200 is stored for the key 'amount_of_labels' in the product cart item data. This value is retrievable on the cart and checkout page and can be store in the order item meta data.
My question is:
Is there a same sort of hook/action available for updating cart data on the cart page itself?
I see that there is woocommerce_after_cart_item_quantity_update hook. But it is not working properly for my case. With that hook I can't update my custom cart item data (adding updating or removing).
Example screenshot:
Here you can see what I am trying to build. The inputs in the screenshot take the amount of labels and those should be added to the cart item data when updating the cart.

Products on separate lines at woocommerce checkout when quantity is >1

I am using woocommerce subscription with the gifting extension. With this it is possible to gift a subscription to a recipient. I am trying to make it possible to gift a subscription to multiple people in one order.
The tricky thing is that each product by default can only be gifted to 1 person. So in order to gift to multiple people, the same product has to be added to the cart multiple times.
With the use of this thread: WooCommerce - Treat cart items separate if quantity is more than 1
I have managed to add the same product to the cart and show in on multiple lines. So if I enter quantity 5 of my subscription product and add it to the cart I get five entries in my cart. Which is great, as now I can gift every line to another person.
However, when I proceed to checkout it gets messed up again. Instead of showing the product on separate lines, everything get jammed together. Thus on checkout I get to see 1 line with quantity 5, instead of 5 lines with quantity 1.
This is where the question comes in:
How can I make sure that the order review table on the checkout page shows separate lines for a product with quantity x>1 instead of 1 line with quantity x?
Remark:
I figured out that it only does not work when the woocommerce subscription gifting add-on is active. It overrides seem to override the wc_cart_item_data
you can use this code
function separate_individual_cart_items( $cart_item_data, $product_id ) {
$unique_cart_item_key = md5( microtime() . rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'separate_individual_cart_items', 10, 2 );
you can put this code into functions.php or into yout own plugin ( a best solution).
And a observation because this question is similar like this question
WooCommerce - Treat cart items separate if quantity is more than 1
but I can't close the question, sorry... my reputation is low)

Changing cart product price in Woocommerce

I wanted to implement a buy 3 free 1 feature, so I wrote a script that detect whether customer has 3 same items in cart and automatically add 1 more same item to the cart. Then using another hook, I overwrite the price of the product to 0.
I googled the solution and used the same approach found on:
WooCommerce: Add product to cart with price override?
woocommerce add custom price while add to cart
Here is the code sample:
function setGiftPriceToZero($cart_object){
foreach($cart_object->cart_contents as $k=>$item):
if(isset($item['variation']['promo']) && ($item['variation']['promo']) == 'buy 3 free 1'):
$item['data']->price = 0;
endif;
endforeach;
}
add_action('woocommerce_before_calculate_totals', 'setGiftPriceToZero');
When Woocommerce calculate the subtotal for the cart, it always add in the original price of the product that is supposed to be free. For example, when I added 3 $100 item to cart, the cart subtotal ends up with $400 instead of $300.
I digged deeper into the Woocommerce code and found that in https://docs.woocommerce.com/wc-apidocs/source-class-WC_Cart.html#1139, $item['data']->get_price() is used which always return the original price for the item.
Is there anyway to fix this using hooks/apis instead of editing Woocommerce core file?
I have found the culprit of this error. It's caused by conflict from another plugin called Woocommerce Role Based Price. This plugin overwrite the cart item price at the end of the cart total calculation flow. This is why get_price() function always return the specified price for the item.
Now I only have to edit the plugin file so that it plays nicely with my logic.

Categories