PHOENIX-MEDIA / Magento-CashOnDelivery

Cash on Delivery allows to book additional fee on delivery depending on inland or international delivery.
43 stars 46 forks source link

Programaticaly created order: COD fee not set #50

Closed postadelmaga closed 6 years ago

postadelmaga commented 6 years ago

If I create an order with a script cod fee is correctly set in the quote but not in the order. So the order total is wrong.

More info Cod fee is set on the order by Phoenix_CashOnDelivery_Model_Observer::sales_order_payment_place_end

public function sales_order_payment_place_end(Varien_Event_Observer $observer) 
    {        
        // .....
        $order = $payment->getOrder();
        $quote = Mage::getSingleton('checkout/session')->getQuote();

        if (!$quote->getId()) {
            $quote = Mage::getSingleton('adminhtml/session_quote')->getQuote();
        }
      // .....

I can see the following issue:

I would suggest to use the <fieldsets> configuration way because it reduce this kind of problems

If you, instead, decide to go for the observer the below code fix the issue

public function sales_order_payment_place_end(Varien_Event_Observer $observer)
    {
        $payment = $observer->getPayment();
        if ($payment->getMethodInstance()->getCode() != 'phoenix_cashondelivery') {
            return $this;;
        }

        $order = $payment->getOrder();
        $quote = Mage::getSingleton('checkout/session')->getQuote();

        if (!$quote->getId()) {
            $quote = Mage::getSingleton('adminhtml/session_quote')->getQuote();
        }
        // Fix for api/script execution
        if (!$quote->getId()) {
            $quote = $order->getQuote();
        }

        $order->setCodFee($quote->getCodFee());
        $order->setBaseCodFee($quote->getBaseCodFee());
        $order->setCodTaxAmount($quote->getCodTaxAmount());
        $order->setBaseCodTaxAmount($quote->getBaseCodTaxAmount());
        $order->save();

        return $this;
    }
PHOENIX-MEDIA commented 6 years ago

You are absolutely right. It will use a fieldset instead of the observer.