Apply Coupon code discount to the final order total - php

We sell trails and the trails are "products" in WooCommerce. >>
Check this link
Try clicking on the "Let's Play" link and it will take you to the checkout. >> (Screenshot)
The price of each trail is set to zero (in WooCommerce admin) because the pricing is "per person".
We had to use the following code snippet to make the products "work", with the "$0" price.
add_filter( 'woocommerce_is_purchasable', 'wpa_109409_is_purchasable', 10, 2 );
function wpa_109409_is_purchasable( $purchasable, $product ){
if( $product->get_price() >= 0 )
$purchasable = true;
return $purchasable;
}
After making the products "work", with the "$0" price, we successfully added some required fields on the checkout page which are used for adding a "fee" for every "person", the visitor will select.
The order total will only update when the user will select the number of players field on the checkout page, notice the "Select No of Players" options.
(Screenshot)
The Issue:
Whenever we apply a "Coupon" (For example "enjoy10" - 10% discount), it only applies to the product price, which is currently set-to "$0".
(Check this Screen Record)
So, technically, the coupon code doesn't work and applied only "$0" discount because the coupon doesn't apply on the order total, it only applies to the product price, which is set to "$0", because we're charging on a per person basis.
I want the coupon code to apply to the final order total, not just the product price.

In default, the coupon will be applied to subtotal or on the product price. Subtotal means the total amount of product.
When you submit some additional fields in the checkout, the additional amount will be added as fee in WooCommerce. The fee is the same as the coupon. Using fee, we can increase or reduce the total amount. When we apply a coupon, WooCommerce applies a negative fee on the subtotal and that so the total amount is reduced.
The options possible are either change your coupon amount as per the latest fee or add a negative fee (additional discount) for the checkout field fee by calculating the amount of coupon.

Related

WooCommerce override "Tax Included In Price" setting for user role?

We've got a custom user role called "Trade Customer."
For normal customers, the price is set in the system as INCLUDING tax - so £59.99 is the price including tax. For Trade Customers, the price is imported from an external system and for example would be £15.00 and that's NOT INCLUDING tax.
Under WooCommerce > Settings > Tax we've got the Prices entered with tax option set to Yes, I will enter prices inclusive of tax.
The tax band is 20%.
So when a customer buys the £59.99 item at the cart it shows £47.99 + £12 TAX.
With a trade customer, we want this to work the other way round, so the price in the store is £15.00, then when they get to the checkout it shows £15.00 + £3 TAX for a total of £18.
We've tried the following code to change the tax to show at the cart for Trade Users:
// Change tax setting for trade
function s9_override_tax_settings_trade( $value ) {
if ( s9_has_user_role('trade_customer') ) {
return 'excl';
}
return $value;
}
add_filter( 'pre_option_woocommerce_tax_display_shop', 's9_override_tax_settings_trade' );
add_filter( 'pre_option_woocommerce_tax_display_cart', 's9_override_tax_settings_trade' );
The problem is, this only inverts the price so instead of showing £15.00 + £3 VAT, it shows £12.00 + £3 VAT when we want it to show £15.00 + £3 VAT.
We effectively need to override the Prices entered with tax option of Yes, I will enter prices inclusive of tax to No, I will enter prices exclusive of tax but only for trade customers but I can't seem to find a filter for this.
Any ideas on how this can be achieved?

How to change Original Price (not custom price) of an item in quote programmatically in Magento?

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());
}

Save the custom created fee in checkout page and in the place order

Here's my problem, I'm having a hard time adding the custom fee to the total price of the order page, I can add the custom fee on the checkout page but after I place the order, the total price is not accurate in the total price on checkout page
enter image description here

WooCommerce remove specific fee

I'm currently adding a feee on checkout;
add_action( 'woocommerce_cart_calculate_fees', 'custom_cart_total' );
function custom_cart_total($order_total) {
WC()->cart->add_fee( __('Military Discount', 'woocommerce'), -$totalDiscount );
}
I have some additional conditional logic to add where I may want to remove this specific fee Military Discount. I don't see an option of remove_fee. I could 0 out the discount but then it will show on the invoice which I don't want if it is 0.
How can I remove this discount based on some of my stores logic?
Further details -
I could only add the discount based on logic instead of removing it however, this WooCommerce store is unique in the fact the shopping is done in-store. We have issues where customers are getting the discount due to cache/cookies/ or sessions. So I'm looking to force clear all the discounts for the next user.

How to show product total price calculation in Magento cart

I'm having problems trying to show the product full total price in Magento, and by product total price I mean full product price without the discount; the price has to be multiplied for each product quantity added to the cart, and custom options additional price should be added as well. Which files should I edit in Magento? is it possible to show the product discount inside the cart (amount that varies with the amount of ordered products)?
Thanks
Edit:
screenshot
In the blue rectangle it shows the regular price, which should be regular price X 2 (since product quantity is 2) with added 122€ as chosen in product special options. Instead now it shows regular price with discount applied and for only 1 product, without special option added.

Categories