0

I need to get order Purchase Order Date and Purchased From fields in the observer.

Here is my code:

namespace Abc\Mkt\Observer;

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

class SalesOrderSaveAfter implements ObserverInterface {
    protected $scopeConfig;
    protected $date;
    public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Abc\Mkt\Model\Orderapi $orderData)
   { 
      $this->scopeConfig = $scopeConfig;
      $this->date = $date;
      $this->orderData = $orderData;
   }

    public function execute(\Magento\Framework\Event\Observer $observer) {
        $enable=$this->scopeConfig->getValue('abc_mkt/orderconfig/mkt_order_integration', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);
        if($enable){ 
            $order = $observer->getEvent()->getOrder();
            $status = 0;
            $name='';
            $data['Email'] = $order->getCustomerEmail();
            $data['FirstName'] = $order->getCustomerFirstname();
            $data['LastName'] = $order->getCustomerLastName();
            $response= $this->orderData->orderData($data,$status,$name);  
            $res= json_decode($response, true); 
            if($res['success']==1){ 

                $status = 1;
                $oname= $orderobjectname;

                $tdiscount = explode('-', $order["base_discount_amount"]);
                $couponApplied = $order->getCouponCode();                   
                $odata['orderId']= $order->getIncrementId();
                $odata['emailAddress'] = $order->getCustomerEmail();
                $odata['totalOrderAmount'] = $order->getGrandTotal();
                $odata['shippingCostInOrder'] =$order["shipping_amount"];
                $odata['totalQuantity'] = $order["total_qty_ordered"];
                $purchasedOrderDate = $order->getCreatedAt();
                $odata['purchaseOrderDate'] = $purchasedOrderDate;

                $billingObj = $order->getBillingAddress();
                $billStreet = $billingObj->getStreet();
                if(isset($billStreet[1])){ $billStreetComplete =  $billStreet[0].' '.$billStreet[1]; } else { $billStreetComplete = $billStreet[0]; }
                $odata['billingDetails'] =  trim($billingObj->getCompany()).','.$billStreetComplete.','.$billingObj->getRegion().','.$billingObj->getCity().','.$billingObj->getPostcode().';'.'T: '.$billingObj->getTelephone();

                $shippingObj = $order->getShippingAddress();
                if(!empty($shippingObj)){
                    $shipStreet = $shippingObj->getStreet();
                    if(isset($shipStreet[1])){ $shipStreetComplete =  $shipStreet[0].' '.$shipStreet[1]; } else { $shipStreetComplete = $shipStreet[0]; }
                    $odata['shippingDetails'] = trim($shippingObj->getCompany()).','.$shipStreetComplete.','.$shippingObj->getRegion().','.$shippingObj->getCity().','.$shippingObj->getPostcode().';'.'T: '.$shippingObj->getTelephone();
                }

                $odata['couponApplied'] = $couponApplied ;
                if($order["tax"]){
                            $odata['taxInOrder'] =$order["tax"] ;
                } else {
                    $odata['taxInOrder'] =0.00 ;
                }
                if($order->getBaseDiscountAmount()){
                    $tdiscount = explode('-', $order->getBaseDiscountAmount());
                    if($tdiscount){
                        $odata['totalOrderDiscount'] = $tdiscount[1];
                    }else{
                        $odata['totalOrderDiscount'] = 0.00;

                    }
                }else{
                    $odata['totalOrderDiscount'] = 0.00;
                }               

                $response= $this->orderData->orderData($odata,$status,$oname);
            }
        }
        return $this;
    }
}

Anyone have a solution for this?

Thanks

15
  • 1
    I don't know why someone gave negative on this. +1 Oct 31, 2019 at 5:39
  • Do you want to get it after place order? Oct 31, 2019 at 5:39
  • yes, after place order
    – Shomita
    Oct 31, 2019 at 5:47
  • there is an event sales_order_place_after here you can get order date and other stuff, but before that order is not placed and you can not get order date before placing order Oct 31, 2019 at 5:49
  • 1
    your code is fine, you need to change event from sales_order_place_after to sales_order_save_after Oct 31, 2019 at 6:50

3 Answers 3

1

Your code is fine you just have to change event

from sales_order_place_after to sales_order_save_after

after that you can get create at by this:

$order->getCreatedAt();

and Purchased From with this:

$order->getStoreName();

Note: The only issue that you would face is, this even will also fire when you save order on backend

1

Try to use this code Spinnet:

$appState = $objectManager->get('\Magento\Framework\App\State');
$appState->setAreaCode('frontend');
$orderDatamodel = $objectManager->get('\Magento\Sales\Model\Order')->getCollection();
    foreach($orderDatamodel as $orderDatamodel1){
       echo '<pre>'; print_r($orderDatamodel1->getData());  
     }

If this works fine, Use this as DI.

Hope this will help you!!

1

You can use sales_order_save_after event and get order date.

0

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.