ShipmentCollectionLoadObserver.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Refer to LICENSE.txt distributed with the Temando Shipping module for notice of license
  4. */
  5. namespace Temando\Shipping\Observer;
  6. use Magento\Framework\Event\Observer;
  7. use Magento\Framework\Event\ObserverInterface;
  8. use Magento\Sales\Model\ResourceModel\Order\Shipment\Grid\Collection as ShipmentCollection;
  9. use Magento\Sales\Model\ResourceModel\Order\Shipment\Order\Grid\Collection as OrderShipmentCollection;
  10. /**
  11. * Temando Shipment Collection Load Observer
  12. *
  13. * @package Temando\Shipping\Observer
  14. * @author Christoph Aßmann <christoph.assmann@netresearch.de>
  15. * @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  16. * @link https://www.temando.com/
  17. */
  18. class ShipmentCollectionLoadObserver implements ObserverInterface
  19. {
  20. /**
  21. * Add the shipment status column, aliased with namespace prefix to avoid collisions.
  22. *
  23. * @param Observer $observer
  24. * @return void
  25. */
  26. public function execute(Observer $observer)
  27. {
  28. $collection = $observer->getData('collection');
  29. if ($collection instanceof ShipmentCollection || $collection instanceof OrderShipmentCollection) {
  30. $index = 'shipment_status';
  31. $alias = 'temando_shipment_status';
  32. try {
  33. $collection->getSelect()->columns([$alias => $index]);
  34. $where = $collection->getSelect()->getPart(\Zend_Db_Select::WHERE);
  35. $collection->getSelect()->setPart(\Zend_Db_Select::WHERE, str_replace($alias, $index, $where));
  36. } catch (\Zend_Db_Select_Exception $exception) {
  37. return;
  38. }
  39. }
  40. }
  41. }