When the user selects their quantity and clicks the add to cart button, the item is added to the cart with the correct quantity. However, if the user clicks add to cart again for the same item but with a different quantity, then that will be added to the original quantity.
What I want to happen is the original item quantity to be removed and updated with the new item quantity.
How would this be possible?
Thanks to the help from Lucky Chingi I managed to make it work.
add_filter( 'woocommerce_add_to_cart_validation', 'woo_custom_add_to_cart_before' );
function woo_custom_add_to_cart_before( $cart_item_data ) {
$cart = WC()->instance()->cart;
$id = $_POST['product_id'];
$cart_id = $cart->generate_cart_id($id);
$cart_item_id = $cart->find_product_in_cart($cart_id);
if($cart_item_id){
$cart->set_quantity($cart_item_id,0);
}
return true;
}
Related
I am the new developer to make the Woocommerce website. I found this useful code in another post. It works to overwrite the stock under product id.
If I have two variation products under the same product id, it will delete all the variation product under same product id in the cart. How can I amend the following code?
add_filter('woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 1, 3);
function remove_cart_item_before_add_to_cart($passed, $product_id, $quantity) {
$already_in_cart = false;
foreach( WC()->cart->get_cart() as $key => $item ){
// Check if the item is already is in cart.
if( $item['product_id'] == $product_id ){
$already_in_cart = true;
$existing_product_key = $key;
break;
}
}
if( $already_in_cart ){
WC()->cart->remove_cart_item($existing_product_key);
}
return $passed;
}
For Example:
Cart
Product A (Blue) x 1 qty
Product A (Red) x 1 qty
What I want is if I try to add 2 qty of Product A (Blue), the cart will only overwrite the quantity of Product A (Blue) and keep the quantity of Product A (Red) remain unchanged.
I need to update the order item meta in a woocommerce oder on checkout page or while woocommerce creates the order.
I'm using the plugin visual product configurator and it is not passing the right quantity of some items of the order to woocommerce order meta, especially when I use multiple variations on the same product.
Is there a hook for me to use to update the item quantity for a certain order item and how can I use it?
The plugin returns me an array with all the cart information and I can only check if an item of the order appears multiple times - if yes I need to change the quantity of that item to that number in the woocommerce order/database.
I was thinking of adding the following hook to my functions.php
add_action('woocommerce_checkout_create_order', 'change_qty', 1,1);
function change_qty($item_qty){
foreach($item_qty as $qty) {
$qty['product_id'] = $id;
$qty['qty'] = $new_qty
$order->update_meta_data('quantity', $new_qty, $id)
}
}
Whereas $item_qty is be an multi-dimensional array containing the item_ids and adjusted quantities.
Another problem I'm facing is that I dont know when I need to call that function because I get the array from the plugin on the checkout page, but I think WooCommerce has not yet created an order at that moment?
The result should be an adjusted item quantity in the woocommerce order summary in the backend.
To update the order item quantity, you can use WC_Order_Item_Product set_quantity() method.
The correct hook to update order items (line items) is woocommerce_checkout_create_order_line_item action hook, that is triggered during order creation, before data is saved to databased.
add_action('woocommerce_checkout_create_order_line_item', 'change_order_line_item_quantity', 10, 4 );
function change_order_line_item_quantity( $item, $cart_item_key, $cart_item, $order ) {
// Your code goes below
// Get order item quantity
$quantity = $item->get_quantity();
$new_qty = $quantity + 2;
// Update order item quantity
$item->set_quantity( $new_qty );
}
The function arguments (variables) are defined and usable:
$item is the WC_Order_Item_Product Object (not saved yet to database)
$cart_item_key is the related cart item key
$cart_item is the related cart item data
$order is the WC_Order Object (not saved yet to database)
Related:
Get Order items and WC_Order_Item_Product in Woocommerce 3
WC_Order_Item_Product Class and methods API Documentation
WC_Checkout and woocommerce_checkout_create_order_line_item action hook located in the create_order_line_items() method
This can help you (we hook into payment completed notification from the payment provider). If you want to update the _qty just after the order was created, I can change my function. But for now I would update it only when the payment was successful.:
/**
* Update order item qty after payment successful
*/
add_filter( 'woocommerce_payment_complete_order_status', 'update_order_item_qty', 10, 2 );
function update_order_item_qty( $order_status, $order_id ) {
//Get the order and items
$order = new WC_Order( $order_id );
$items = $order->get_items();
//New qty
$new_qty = 0;
foreach ( $items as $item_id => $item_data ) {
update_meta_data( '_qty', $new_qty, $item_id );
}
}
Please try if this is what you'r looking for.
Right now, I am bringing users to a Contact 7 Form, and adding to cart using the href link, http://example.com/checkout/?add-to-cart=1000
I have a unique case where if there is 1 item left of product 1000, and the user chooses 2 in the dropdown box, then I should increase product 1000's quantity by 2 and allow the user to checkout normal.
I have looked at the woocommerce_add_to_cart and woocommerce_add_to_cart_validation hooks, but couldn't figure it out.
I just need to understand, how I can make the modification before the user is directed to the checkout page and are not shown the "You cannot add that amount to the cart" message.
add_filter( 'woocommerce_product_get_stock_quantity', 'hook_get_stock_quantity', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_quantity', 'hook_get_stock_quantity', 10, 2 );
function hook_get_stock_quantity( $value, $product ) {
if($value < 2) $value = 10;
return $value;
}
Hy guys.
I have three product in my cart. Let's try to imagine that the image is the cart and i have modify the quantity of product and then press update cart.
When i press update cart, my function runs, than prints all my item after modify, into ITEMS.json. There are all my items and with the quantity modify of the Shoes, but into my file there is also Hoodie with quantity = 3. How is it possible that i delete it? it and i print items after update cart.
add_action('woocommerce_after_cart_item_quantity_update','my_function');
function my_function()
{
global $woocommerce;
$items = $woocommerce->cart->get_cart();
file_put_contents('ITEMS.json', print_r($items, true));
}
Is there something to distinguish the products delete from product modify?
Is there a hook which returns the list of products deleted ?
According to the documentation, you can retrieve all needed information from the parameters of the hooked function:
function my_function( $cart_item_key, $quantity, $old_quantity ) {
global $woocommerce;
$items = $woocommerce->cart->get_cart();
// Check that quantity is not equal to 0 and that the item exists in the cart
if($quantity && isset($items[$cart_item_key])) {
$content = '';
// TODO: Here you must fill $content with needed information from $items[$cart_item_key]
// Below, I add the new quatity to the $content string.
$content .= $quantity;
// Write in JSON file
file_put_contents('ITEMS.json', $content);
}
};
add_action( 'woocommerce_after_cart_item_quantity_update', 'my_function', 10, 3 );
Problem I am having here is that people go to a Product page, set a quantity on how much they want of that product and add to cart (which than takes them to the cart page). Client wants the ability to go back to the shop/product page if they want to make further changes to that product (honestly this is strange, since they can do this on the cart page, but whatever). But when people go to update the quantity on the product page, instead of updating the quantity, it adds to the existing quantity for that product.
How to get it to update the existing quantity when the quantity has been submitted more than once on the product page? Is there a woocommerce loop that I can take advantage of here?
Thanks guys, I have dug through the plugin and coded the solution below:
add_action('woocommerce_add_to_cart_handler', 'update_product_in_cart', 11, 2);
function update_product_in_cart($p, $q) {
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
$wCart = $woocommerce->cart->get_cart();
// If cart already exists, and product exists, than remove product, and add the new product to it.
if ($wCart)
{
$cart_item_keys = array_keys($wCart);
foreach($cart_item_keys as $key)
{
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
if ($productItemId == $currentProductId)
{
// If you want to empty the entire cart...
// $woocommerce->cart->empty_cart();
// If you want to remove just the product from the cart...
$woocommerce->cart->set_quantity($key, 0);
}
}
}
}
// This adds the product to the cart...
return $q;
}
This gets added to functions.php, and basically, if the product id exists in the cart, it removes the product and the return of $q adds in the new product info.
Works a charm!