0

I am currently developing a module which pre prefills the order items grid automatically when a backend user creates an order from our CRM.

This is done via the observer event:

adminhtml_sales_order_create_process_data_before

Here's the (reduced) code:

public function execute(\Magento\Framework\Event\Observer $observer)
{
    /** @var \Magento\Sales\Model\AdminOrder\Create $orderCreateModel */
    $orderCreateModel = $observer->getData('order_create_model');

    $crmProduct = $this->someFunctionWhichGivesMeTheProductFromTheCrm(1);

    $params = [
        'qty' => $crmProduct->qty,
        'custom_price' => $crmProduct->price
    ];

    $orderCreateModel->addProduct(25, $params);

    /** just tried several methods in desperation - none of them worked... **/      

    $orderCreateModel->recollectCart();
    $orderCreateModel->getQuote()->collectTotals();
    $orderCreateModel->recollectCart();
    $orderCreateModel->saveQuote();
}

This is the result:

order items grid

The products are added but all the totals are not calculated correctly. If I hit the button "Updated Items and Quantities" manually the prices are calculated - but with the result you see below:

enter image description here

Can someone please give me an advice on how to add the products correctly and or collect the totals?

0

Try following way:


namespace SR\Stackexchange\Observer;

use Magento\Framework\Event\Observer as EventObserver;
use Magento\Framework\Event\ObserverInterface;

class AdminhtmlSalesOrderCreateProcessDataBefore implements ObserverInterface
{
    /**
     * @var \Magento\Framework\ObjectManagerInterface
     */
    protected $objectManager;

    /**
     * AdminhtmlSalesOrderCreateProcessDataBefore constructor.
     *
     * @param \Magento\Framework\ObjectManagerInterface $objectManager
     */
    public function __construct(
        \Magento\Framework\ObjectManagerInterface $objectManager
    ) {
        $this->objectManager = $objectManager;
    }

    public function execute(EventObserver $observer)
    {

        $params = [
            'qty' => 8,
            'custom_price' => 55
        ];
        $productId = 1;
        $product = $this->objectManager->create(
            \Magento\Catalog\Model\Product::class
        )->setStore(
            $this->getSession()->getStore()
        )->setStoreId(
            $this->getSession()->getStoreId()
        )->load(
            $productId
        );
        $config = new \Magento\Framework\DataObject($params);
        $this->getQuote()->addProduct($product, $config);

        return $this;
    }

    /**
     * Retrieve session object
     *
     * @return \Magento\Backend\Model\Session\Quote
     */
    protected function getSession()
    {
        return $this->objectManager->get('Magento\Backend\Model\Session\Quote');
    }

    /**
     * Retrieve quote object
     *
     * @return \Magento\Quote\Model\Quote
     */
    protected function getQuote()
    {
        return $this->getSession()->getQuote();
    }
}
3
  • Thanks for your answer! But still one problem exists: The subtotal, row total and the overall calculated values are still 0. After i hit the button Update Items and Quantities it's calculated correctly. I tried to fix this by calling getQuote()->collectTotals() but this broke everything.
    – tobias
    Jun 7 '18 at 14:35
  • Hey working fine M2.2.4. Which version you tried?
    – Sohel Rana
    Jun 7 '18 at 15:26
  • Was running on M2.2.2 and just upgraded to 2.2.4 - still the same result.
    – tobias
    Jun 11 '18 at 10:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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