I need a hand with a development that I am doing for my work which I have been working for a week and I cannot move forward.
My task consists of an Observer to capture the moment when an order changes status and in a given table change the value of date_of_modification.
Here is my code:
<?php
namespace MovistarAdminExportsObserver;
use MagentoFrameworkEventObserverInterface;
use MagentoFrameworkEventObserver;
use MovistarAdminExportsModelExportForAnalysis;
use MagentoSalesModelResourceModelOrderCollectionFactory;
use MovistarAdminExportsModelResourceModelExportForAnalysisRM;
use PsrLogLoggerInterface;
class SalesOrderStateChangeBefore implements ObserverInterface
{
/**
* @var ExportForAnalysis
*/
protected $_exportForAnalysis;
/**
* @var LoggerInterface
*/
protected $_logger;
/**
* @var ExportForAnalysisRMCollectionFactory
*/
protected $_exportForAnalysisCollectionFactory;
/**
* @var ExportForAnalysisRM
*/
protected $_exportForAnalysisResource;
/**
* @var CollectionFactory
*/
protected $_collectionFactory;
/**
* SalesOrderStateChangeBefore constructor.
* @param ExportForAnalysis $exportForAnalysis
* @param LoggerInterface $logger
* @param ExportForAnalysisRMCollectionFactory $exportForAnalysisCollectionFactory
* @param CollectionFactory $collectionFactory
*/
public function __construct(
ExportForAnalysis $exportForAnalysis,
LoggerInterface $logger,
ExportForAnalysisRMCollectionFactory $exportForAnalysisCollectionFactory,
ExportForAnalysisRM $exportForAnalysisResource,
CollectionFactory $collectionFactory
)
{
$this->_exportForAnalysis = $exportForAnalysis;
$this->_logger = $logger;
$this->_exportForAnalysisCollectionFactory = $exportForAnalysisCollectionFactory;
$this->_exportForAnalysisResource = $exportForAnalysisResource;
$this->_collectionFactory = $collectionFactory;
}
/**
* @param MagentoFrameworkEventObserver $observer
* @return SalesOrderStateChangeBefore
* @throws Exception
*/
public function execute(Observer $observer)
{
$order = $observer->getEvent()->getOrder();
if ($order->getStatus() == 'processing' || $order->getStatus() == 'aprobado_mp' || $order->getStatus() == 'canceled' || $order->getStatus() == 'complete' || $order->getStatus() == 'closed'){
$exportForAnalysisCollection = $this->_exportForAnalysisCollectionFactory->create();
$exportForAnalysisCollection->addFieldToFilter('pedido_magento', [ 'in' => $order->getData('increment_id')]);
$timezone = 'America/Argentina/Buenos_Aires';
$timestamp = time();
$dateTime = new DateTime("now", new DateTimeZone($timezone));
$dateTime->setTimestamp($timestamp);
$this->_exportForAnalysis->setData('fecha_de_modificacion', $dateTime->format('d-m-Y'));
}
}
}
Aca mi 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_state_change_before">
<observer name="movistar_adminexports_sales_order_state_change_before" instance="MovistarAdminExportsObserverSalesOrderStateChangeBefore" />
</event>
</config>
The problem is that I change the status of an order and in the table called export_for_analysis that has a date_of_modification column, the date is not updated as it should.
From already thank you very much!!!
Desde ya muchas gracias!!