8

I'm building an extension which imports orders created on a third-party system into Magento. The third-party system should be the single source of truth for the order's prices, tax rate, tax paid, etc.

When creating a quote item for each order line to be imported, I can successfully override the product's price with setOriginalCustomPrice and setCustomPrice. Essentially, I'm looking for a way to do the equivalent for the tax rate (or even just the tax paid).

I've attempted using setTaxPercent, setTaxAmount, setBaseTaxAmount, etc. to no avail. I've seen suggestion of hooking into sales_quote_collect_totals_before, but I don't want to effect any orders created on the front-end of the Magento store (i.e. orders not from the third-party system).

Can someone please point me in the right direction!

3
  • I think your best bet to be sure that Magento does not mess around with these values is to set them directly on the order object after converting the quote. Commented Mar 8, 2016 at 0:43
  • @fschmengler Thanks. I had experimented with that; I wanted to avoid manually calculating the subtotals, totals, etc., but perhaps it's the only way to go Commented Mar 8, 2016 at 1:09
  • @AngusFretwell I'm currently struggling with this same issue and am curious if you found a good solution? Commented Apr 11, 2016 at 17:22

1 Answer 1

0

You can use the event 'sales_model_service_quote_submit_success' and add the custom value in quote to check the Quote data, and Apply Condition there, So It Won't Effect the frontend orders.

app/code/Vendor/ModuleName/etc/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_model_service_quote_submit_success">
        <observer name="vendor_modulename_order_save_quote" instance="Vendor\Modulename\Observer\Order\SaveQuote" />
    </event>
</config>

app/code/Vendor/ModuleName/Observer/Order/SaveQuote.php

<?php
namespace Vendor\ModuelName\Observer\Order;

use Magento\Framework\Event\ObserverInterface;

class SaveQuote implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        try {
            $tax = '67.99'; // tax amount
            $price = '107.99'; // price
            $quote = $observer->getEvent()->getData('quote');
            $customOrderFlag = $quote->getData('custom_order'); // This Is the Check For the Custom order
            if (!$customOrderFlag) {
                foreach ($quote->getAllItems() as $item) {
                    $sku = $item->getProduct()->getSku();
                    $item->setTaxAmount($tax); // Setting Custom Tax amount
                    $item->setBaseTaxAmount($tax);
                    $item->save();
                }
            }
        } catch (\Exception $e) {
            // Silence
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.