|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Downloadable\Observer; |
| 8 | + |
| 9 | +use Magento\Framework\Event\ObserverInterface; |
| 10 | + |
| 11 | +/** |
| 12 | + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
| 13 | + */ |
| 14 | +class UpdateLinkPurchasedObserver implements ObserverInterface |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Core store config |
| 18 | + * @var \Magento\Framework\App\Config\ScopeConfigInterface |
| 19 | + */ |
| 20 | + protected $_scopeConfig; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var \Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory |
| 24 | + */ |
| 25 | + protected $_purchasedFactory; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var \Magento\Framework\DataObject\Copy |
| 29 | + */ |
| 30 | + protected $_objectCopyService; |
| 31 | + |
| 32 | + /** |
| 33 | + * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig |
| 34 | + * @param \Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory $purchasedFactory |
| 35 | + * @param \Magento\Framework\DataObject\Copy $objectCopyService |
| 36 | + */ |
| 37 | + public function __construct( |
| 38 | + \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, |
| 39 | + \Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory $purchasedFactory, |
| 40 | + \Magento\Framework\DataObject\Copy $objectCopyService |
| 41 | + ) { |
| 42 | + $this->_scopeConfig = $scopeConfig; |
| 43 | + $this->_purchasedFactory = $purchasedFactory; |
| 44 | + $this->_objectCopyService = $objectCopyService; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * re-save order data after order update |
| 49 | + * @param \Magento\Framework\Event\Observer $observer |
| 50 | + * @return $this|void |
| 51 | + */ |
| 52 | + public function execute(\Magento\Framework\Event\Observer $observer) |
| 53 | + { |
| 54 | + $order = $observer->getEvent()->getOrder(); |
| 55 | + |
| 56 | + if (!$order->getId()) { |
| 57 | + //order not saved in the database |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + $purchasedLinks = $this->_createPurchasedCollection()->addFieldToFilter( |
| 62 | + 'order_id', |
| 63 | + ['eq' => $order->getId()] |
| 64 | + ); |
| 65 | + |
| 66 | + foreach ($purchasedLinks as $linkPurchased) { |
| 67 | + $this->_objectCopyService->copyFieldsetToTarget( |
| 68 | + \downloadable_sales_copy_order::class, |
| 69 | + 'to_downloadable', |
| 70 | + $order, |
| 71 | + $linkPurchased |
| 72 | + ); |
| 73 | + $linkPurchased->save(); |
| 74 | + } |
| 75 | + |
| 76 | + return $this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @return \Magento\Downloadable\Model\ResourceModel\Link\Purchased\Collection |
| 81 | + */ |
| 82 | + protected function _createPurchasedCollection() |
| 83 | + { |
| 84 | + return $this->_purchasedFactory->create(); |
| 85 | + } |
| 86 | +} |
0 commit comments