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.
Related
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 );
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)
In Magento 2 I have created a custom module category discount. When active its working fine for product list, details and cart view page.
But when I remove this discount it shows the original price for list and details page which is correct but mini cart view shows me the discounted price which shouldn't be the case. However if I update the quantity then it's fine.
I don't want to click the update button in the mini cart view for the original price. I want to do this using an observer.
I have few variation products on my site. As you can see in the image, the price of the product added to cart it 199 Rupees. But when added, the price shown in the cart above is 179! I checked in the admin panel, edited the product but everything is fine. What could be the issue? I have also tried updating wordpress and woocommerce.
I am using <?php echo WC()->cart->get_cart_subtotal(); ?> inside header.php to get the total price but it returns (TotalPrice - DiscountedPrice) when coupon is applied. I want to show only total price and exclude discounted price.
SO the problem is, if the coupon is applied, which is saved, the price shown in cart is the discounted price. But when you go to cart page, it adds the tax rate and applies coupon again!. How to fix this issue!
It might be as simple as clearing the cache..
Woocommerce>>System Status >> Tools >> WC Transients >> Clear Transients
We have followed this guys tutorial on changing the prestashop add to cart animation
http://nemops.com/better-prestashop-add-to-cart/
However, now the problem we face is if someone adds a quantity to the cart that is greater than the quantity we have in stock. The popup box still shows and we still get the default prestashop error "There isn't enough product in stock".
We thought that in the ajax-cart.js where we added the new code that we could run and if statement against the quantity added and the quantity in stock. If the quantity added is greater than what we have in stock then do not display the box. We are stuck on how to check the quantity added vs the quantity in stock.
Get the quantity you have
var availableQuantity = {$product->quantity};
and then dissable add to cart button if available quantity is less then added to cart value