Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purchasing a downloadable product as guest then creating an account on the onepagesuccess step doesn't link product with account #21711

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
Jitheesh marked this conversation as resolved.
Show resolved Hide resolved
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Downloadable\Observer;

use Magento\Framework\Event\ObserverInterface;

/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
Jitheesh marked this conversation as resolved.
Show resolved Hide resolved
*/
class UpdateLinkPurchasedObserver implements ObserverInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Jitheesh. Thank you for your contribution. Could you explain, please, is there any reason why the observer was created instead of fixing the issue directly in the code where it appears?
Thank you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @rogyar

This issue is related to downloadable component and issue is appears at sales component,

public function create($orderId)
{
$order = $this->orderRepository->get($orderId);
if ($order->getCustomerId()) {
throw new AlreadyExistsException(
__('This order already has associated customer account')
);
}
$customer = $this->customerExtractor->extract($orderId);
/** @var AddressInterface[] $filteredAddresses */
$filteredAddresses = [];
foreach ($customer->getAddresses() as $address) {
if ($this->needToSaveAddress($order, $address)) {
$filteredAddresses[] = $address;
}
}
$customer->setAddresses($filteredAddresses);
$account = $this->accountManagement->createAccount($customer);
$order = $this->orderRepository->get($orderId);
$order->setCustomerId($account->getId());
$order->setCustomerIsGuest(0);
$this->orderRepository->save($order);
return $account;
}

so I thought it would be good to add this fix on corresponding component. We already have other Observer based on this rule.

class SetLinkStatusObserver implements ObserverInterface

{
/**
* Core store config
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
protected $_scopeConfig;

/**
* @var \Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory
*/
protected $_purchasedFactory;
Jitheesh marked this conversation as resolved.
Show resolved Hide resolved

/**
* @var \Magento\Framework\DataObject\Copy
*/
protected $_objectCopyService;
Jitheesh marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory $purchasedFactory
* @param \Magento\Framework\DataObject\Copy $objectCopyService
*/
public function __construct(
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Downloadable\Model\ResourceModel\Link\Purchased\CollectionFactory $purchasedFactory,
\Magento\Framework\DataObject\Copy $objectCopyService
) {
$this->_scopeConfig = $scopeConfig;
$this->_purchasedFactory = $purchasedFactory;
$this->_objectCopyService = $objectCopyService;
}

/**
* re-save order data after order update
* @param \Magento\Framework\Event\Observer $observer
* @return $this|void
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$order = $observer->getEvent()->getOrder();

if (!$order->getId()) {
//order not saved in the database
return $this;
}

$purchasedLinks = $this->_createPurchasedCollection()->addFieldToFilter(
'order_id',
['eq' => $order->getId()]
);

foreach ($purchasedLinks as $linkPurchased) {
$this->_objectCopyService->copyFieldsetToTarget(
\downloadable_sales_copy_order::class,
'to_downloadable',
$order,
$linkPurchased
);
$linkPurchased->save();
}

return $this;
}

/**
* @return \Magento\Downloadable\Model\ResourceModel\Link\Purchased\Collection
*/
protected function _createPurchasedCollection()
{
return $this->_purchasedFactory->create();
Jitheesh marked this conversation as resolved.
Show resolved Hide resolved
}
}
1 change: 1 addition & 0 deletions app/code/Magento/Downloadable/etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
</event>
<event name="sales_order_save_after">
<observer name="downloadable_observer" instance="Magento\Downloadable\Observer\SetLinkStatusObserver" />
<observer name="downloadable_observer_assign_customer" instance="Magento\Downloadable\Observer\UpdateLinkPurchasedObserver" />
</event>
<event name="sales_model_service_quote_submit_success">
<observer name="checkout_type_onepage_save_order_after" instance="Magento\Downloadable\Observer\SetHasDownloadableProductsObserver" />
Expand Down