Skip to content

Commit

Permalink
Merge branch 'bugfix/check-salable-before-reminder' into release-week-42
Browse files Browse the repository at this point in the history
  • Loading branch information
michielgerritsen committed Oct 16, 2023
2 parents 7e58b25 + e2f1b4f commit 5373ffe
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
1 change: 0 additions & 1 deletion Cron/SendPendingPaymentReminders.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public function __construct(
$this->sortOrderFactory = $sortOrderFactory;
$this->dateTime = $dateTime;
$this->paymentReminder = $paymentReminder;
$this->config = $config;
}

public function execute()
Expand Down
27 changes: 27 additions & 0 deletions Service/Order/PaymentReminder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderItemInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Sales\Model\Order;
use Mollie\Payment\Api\Data\PendingPaymentReminderInterface;
Expand Down Expand Up @@ -87,6 +88,20 @@ public function send(PendingPaymentReminderInterface $pendingPaymentReminder): O
return $order;
}

if (!$this->orderIsInStock($order)) {
$this->logger->addInfoLog(
'info',
sprintf(
'On or more products from order #%s are not stock, not sending payment reminder',
$order->getIncrementId()
)
);

$this->pendingPaymentReminderRepository->delete($pendingPaymentReminder);

return $order;
}

$this->logger->addInfoLog(
'info',
sprintf('Preparing to send the payment reminder for order #%s', $order->getIncrementId())
Expand Down Expand Up @@ -132,4 +147,16 @@ private function isAlreadySend(OrderInterface $order): bool
return false;
}
}

private function orderIsInStock(OrderInterface $order): bool
{
/** @var OrderItemInterface $item */
foreach ($order->getAllVisibleItems() as $item) {
if (!$item->getProduct()->isSaleable()) {
return false;
}
}

return true;
}
}
31 changes: 31 additions & 0 deletions Test/Integration/Service/Order/PaymentReminderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Mollie\Payment\Test\Integration\Service\Order;

use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Sales\Model\Order;
use Mollie\Payment\Api\Data\PendingPaymentReminderInterface;
use Mollie\Payment\Service\Order\PaymentReminder;
use Mollie\Payment\Service\Order\SecondChanceEmail;
Expand Down Expand Up @@ -31,4 +33,33 @@ public function testDoesNotSendReminderWhenAlreadyPaid(): void

$instance->send($pendingPaymentReminder);
}

/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @return void
*/
public function testDoesNotSendReminderWhenProductsNotInStock(): void
{
$order = $this->loadOrder('100000001');
$order->setState(Order::STATE_PENDING_PAYMENT);
$order->save();

foreach ($order->getItems() as $item) {
$item->getProduct()->setStatus(Status::STATUS_DISABLED);
}

/** @var PendingPaymentReminderInterface $pendingPaymentReminder */
$pendingPaymentReminder = $this->objectManager->create(PendingPaymentReminderInterface::class);
$pendingPaymentReminder->setOrderId($order->getId());

$secondChanceMock = $this->createMock(SecondChanceEmail::class);
$secondChanceMock->expects($this->never())->method('send');

/** @var PaymentReminder $instance */
$instance = $this->objectManager->create(PaymentReminder::class, [
'secondChanceEmail' => $secondChanceMock,
]);

$instance->send($pendingPaymentReminder);
}
}

0 comments on commit 5373ffe

Please sign in to comment.