Skip to content

Commit

Permalink
Merge branch 'bugfix/mark-subscription-items-as-onetime-purchase' int…
Browse files Browse the repository at this point in the history
…o release-week-15
  • Loading branch information
michielgerritsen committed Apr 18, 2024
2 parents 4aaeeee + a3312bf commit 1f200cf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Observer\AdminhtmlSalesOrderCreateProcessItemBefore;

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Exception\NoSuchEntityException;

class SetPurchaseTypeOnCreateOrderItem implements ObserverInterface
{
/**
* @var ProductRepositoryInterface
*/
private $productRepository;

public function __construct(
ProductRepositoryInterface $productRepository
) {
$this->productRepository = $productRepository;
}

public function execute(Observer $observer)
{
/** @var RequestInterface $request */
$request = $observer->getData('request_model');

if ($request->has('item') && !$request->getPost('update_items')
) {
$itemsChanged = false;
$items = $request->getPost('item');
foreach ($items as $item => $requestData) {
if (!$this->productAllowsOneTimePurchase($item)) {
continue;
}

$itemsChanged = true;
$items[$item]['purchase'] = 'onetime';
}

if ($itemsChanged) {
$request->setPostValue('item', $items);
}
}
}

private function productAllowsOneTimePurchase(int $productId): bool
{
try {
$product = $this->productRepository->getById($productId);
} catch (NoSuchEntityException $e) {
return false;
}

return !!$product->getData('mollie_subscription_product');
}
}
3 changes: 3 additions & 0 deletions etc/events.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,7 @@
<event name="sales_quote_payment_import_data_before">
<observer name="mollie_clear_issuer_on_method_change" instance="Mollie\Payment\Observer\SalesQuotePaymentImportDataBefore\ClearIssuerOnMethodChange" />
</event>
<event name="adminhtml_sales_order_create_process_item_before">
<observer name="mollie_set_purchase_type_on_create_order_item" instance="Mollie\Payment\Observer\AdminhtmlSalesOrderCreateProcessItemBefore\SetPurchaseTypeOnCreateOrderItem" />
</event>
</config>

0 comments on commit 1f200cf

Please sign in to comment.