Woocommerce: Custom field per product on the cart page - php

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.

Related

Update quantity based on variation to cart in WooCommerce

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?

Woocommerce option product price not updated on quantity change

I am using some custom fileds from advanced custom fields plugin. In the case I want to add a addon product to the variation and sum up the value. I can achieve that only if the product quantity is 1. It is a flat price rather based on quantity.
What I want to achieve is update Cart price and checkout page with the updated price.

Woocommerce purchase event

I have woocommerce store and I have integration with manufacturer based on JSON, where I'm parsing his JSON and adding his product quantities to woocommerce warehouse.
But additionally I have my own stock of this same products, that's why I need 2 ecommerce warehouses - for manufacturer stock and for my own stock.
I've build those 2 warehouses, where my stock is on separate mysql table than woocommerce, but now I need to add event, that if someone purchase product, the quantity of product will be decreased from my warehouse (external from woocommerce).
How to find woocommerce purchase event, to which I could add function which will decrease my stock product quantity in not woocommerce table?
You could use the woocommerce's 'woocommerce_after_pay_action' hook.
Reference: https://woocommerce.github.io/code-reference/hooks/hooks.html
Add this code in a must-use plugin or in functions.php
function after_purchase_action($order) {
// do something
}
add_action('woocommerce_after_pay_action', 'after_purchase_action', 10, 1 );

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.

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