2

I'm making an observer to check after a order is made, if there is any product inside that order that has the sync attribute marked as "Yes".

I'm confused about the models and methods I'd use to make it.

Is this the best way to do it? Can someone give me an example of the methods I would need?

  • 1
    which Magento version you are using? – Abhishek Panchal Aug 10 '17 at 17:33
  • version 1.9.3.4 – Stordon Aug 10 '17 at 17:54
3

For Magento 1 you can use sales_convert_quote_to_order event

<sales_convert_quote_to_order>
    <observers>
        <some_alias>
            <class>your_module/observer</class>
            <method>syncSomething</method>
        </some_alias>
    </observers>
</sales_convert_quote_to_order>

also in config.xml add this to global node

<sales>
    <quote>
        <item>
            <product_attributes>
                <sync />
            </product_attributes>
        </item>
    </quote>
</sales>

Observer.php

public function syncSomething(Varien_Event_Observer $observer)
{
    $quote = $observer->getQuote();
    foreach ($quote->getItemsCollection() as $item) {
        if ($item->getProduct()->getSync()) {
            # do something here
        }
    }
}
  • my supervisor told me I need to use event "sales_order_place_after" – Stordon Aug 10 '17 at 18:00
  • @Stordon have you tested it? I guess it works. sales_order_place_after can work too, then observer has to be adjusted ... $order = $observer->getOrder(); ... – sv3n Aug 10 '17 at 18:04
  • Gonna test both! Thank you very much. I didn't quite understand why "getSync()", could you explain it to me? You're getting the attribute "sync", but how come it is a method? – Stordon Aug 10 '17 at 18:12
  • $prod->getSync() is just a magic getter method for product attributes. It's same as $prod->getData('sync'). And if you say "set to YES" i guess attribute will return 0 or 1. – sv3n Aug 10 '17 at 18:15
  • 1
    He said its a pattern we follow inside our company. Your way worked, thank you! – Stordon Aug 10 '17 at 18:39
0

Create an observer for handling the event after checkout an order.

File app\code\YourNameSpace\YourModule\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_order_place_after">
    <observer name="acommerce_utm_after_checkout_event" instance="YourNameSpace\YourModule\Observer\SalesOrderAfterPlaceObserver" />
</event>

After that, create file app\code\YourNameSpace\YourModule\Observer\SalesOrderAfterPlaceObserver.php:

<?php
namespace YourNamespace\YourModule\Observer;

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

class SalesOrderAfterPlaceObserver implements ObserverInterface
{
    /**
    * @param EventObserver $observer
    * @throws \Exception
    */
    public function execute(EventObserver $observer)
    {

        /** @var \Magento\Sales\Model\Order $order */
        $order = $observer->getEvent()->getOrder();

        $products = $order->getAllItems();

       foreach ($products as $product) {
            $check = $product->getCustomAttribute('sync');
            //$check = $product->getSync();
            // Check your value here
       }
    }
}

Note: Remember to clean cache, and run command setup:upgrade in order for the event to work.

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.