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 );
Related
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.
I have Magento 2.3 website, which is connected with third-party sales channels like Amazon, eBay and BOL to sync products and get orders data back into the Magento system and send shipment created by retailer in Magento to those third-party sales channels.
Our system was initially configured to have prices in Magento including VAT. But after EU VAT changes we needed to use Magento prices excluding VAT to properly manage prices on each sales channels and in Magento stores also.
For BOL sales channel, when I receive order, I need to deduct the shipping cost based on item weight and VAT % from the item price we receive from BOL, because BOL item price includes all amounts in item while in Magento we set shipping cost separately.
I am able to set the Custom Price after doing all the calculation required to set final excluded VAT item price in Price column(note, I renamed it to BOL Price) but our Magento system is configured to use Original Price(Magento product price) setup via admin to calculate taxes. In this case, our Original Price is not same as Custom Price we set after our custom calculations. So Product Sub-total and all other prices/calculation does not match the correct values. I know we can configure Magento to use Custom Price for TAX calculation but as we have other sales channel also depending on this setting and developed on Original Price TAX calculation, I need to use same configuration for this. Hence, this requires to set same Custom Price to set as Original Price also to get correct VAT and totals. But not able to get that Original Price updated with any method I could found. Which still uses Magento admin price instead of Price I wanted to set which is same as Price I calculated for Custom Price.
Please check code below, where I tried to update Product price before adding it to cart and also tried cart item product price update after adding it to the cart:
public function addProductToCart(string $quoteId, $item, string $storeCode)
{
$quoteIdMask = $this->quoteIdMaskFactory->create()->load($quoteId, 'masked_id');
/** #var \Magento\Quote\Model\Quote $quote */
$quote = $this->cartRepository->get($quoteIdMask->getData('quote_id'));
$price = $item->unitPrice()->toScalar(); //Item price on BOL
//CALCULATION TO GET FINAL PRICE BEFORE ADDING IT HERE
$product = $this->getProductByEan($item->product()->ean()->toString(), $storeCode); //Get product to add in quote by EAN
// Trying to set Original Price of product before adding product item to quote
$product->setPrice($price));
$product->setOriginalPrice($price);
$product->setBasePrice($price);
$product->setConvertedPrice($price);
$product->setBaseOriginalPrice($price);
$product->setPriceInclTax($price);
$product->setIsSuperMode(true);
$cartItem = $quote->addProduct(
$product,
$item->quantity()->toScalar()
);
$quote->setIsSuperMode(true);
//Update Custom Price [Works]
$cartItem->setOriginalCustomPrice($price);
$cartItem->setCustomPrice($price);
// Trying to set Original Price of cart item product after item added to quote [NOT WORKING]
$cartItem->getProduct()->setPrice($price);
$cartItem->getProduct()->setOriginalPrice($price);
$cartItem->getProduct()->setBasePrice($price);
$cartItem->getProduct()->setConvertedPrice($price);
$cartItem->getProduct()->setBaseOriginalPrice($price);
$cartItem->getProduct()->setPriceInclTax($price);
$cartItem->getProduct()->setIsSuperMode(true);
$this->cartRepository->save($cartItem->getQuote());
}
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.
I need to create a module in prestashop 1.7.3 to make available wholesale prices. The main idea: There are 3 types of prices in cart: retail price, small_wholesale price and BIG_wholesale price. I need to add small_wholesale price and big_wholesale price fields to my product and allow admin to add product and set them in prices tab(below retail price). Actually it's easy to create extra database fields in product table what i did, but still don't know what to override to create fields in product creation page, it should look like this.
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.