Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
44 changes: 41 additions & 3 deletions Observer/CatalogProductSaveAfter/UpdateSubscriptionProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ public function execute(Observer $observer)

private function subscriptionTableHasPriceUpdate(ProductInterface $product): bool
{
$old = $this->serializer->unserialize($product->getOrigData('mollie_subscription_table'));
$new = $this->serializer->unserialize($product->getData('mollie_subscription_table'));
$oldRaw = $product->getOrigData('mollie_subscription_table');
$newRaw = $product->getData('mollie_subscription_table');

$old = $this->safeUnserializeToArray($oldRaw);
$new = $this->safeUnserializeToArray($newRaw);

$oldMapping = $this->getIdentifierToPriceMapping($old);
$newMapping = $this->getIdentifierToPriceMapping($new);
Expand All @@ -63,11 +66,46 @@ private function subscriptionTableHasPriceUpdate(ProductInterface $product): boo
return false;
}

/**
* Safely convert a serialized value or array into a normalized array.
* - Null/empty/false values are treated as empty arrays
* - Already-array values are returned as-is
* - Any unserialize errors result in an empty array
*
* @param mixed $value
*/
private function safeUnserializeToArray($value): array
{
if (is_array($value)) {
return $value;
}

if ($value === null || $value === '' || $value === false) {
return [];
}

try {
$result = $this->serializer->unserialize($value);
return is_array($result) ? $result : [];
} catch (\InvalidArgumentException $e) {
return [];
} catch (\Throwable $e) {
return [];
}
}

private function getIdentifierToPriceMapping(array $table): array
{
$output = [];
foreach ($table as $row) {
$output[$row['identifier']] = $row['price'];
if (!is_array($row)) {
continue;
}
if (!isset($row['identifier']) || $row['identifier'] === '' || $row['identifier'] === null) {
continue;
}
$price = $row['price'] ?? null; // Price is optional
$output[$row['identifier']] = $price;
}

return $output;
Expand Down
13 changes: 9 additions & 4 deletions Service/Email/SubscriptionToProductEmailVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,22 @@ public function get(SubscriptionToProductInterface $subscriptionToProduct): arra
$subscriptionToProduct->getStoreId()
);

$date = $this->formatDate($subscription->nextPaymentDate, $subscriptionToProduct->getStoreId());

return [
$variables = [
'subscription_id' => $subscriptionToProduct->getSubscriptionId(),
'subscription_description' => $subscription->description,
'subscription_nextPaymentDate' => $date,
'subscription_nextPaymentDate' => null,
'subscription_amount' => $amount,
'customer_name' => $customer->name,
'customer_email' => $customer->email,
'product' => $product,
];

if ($subscription->nextPaymentDate !== null) {
$date = $this->formatDate($subscription->nextPaymentDate, $subscriptionToProduct->getStoreId());
$variables['subscription_nextPaymentDate'] = $date;
}

return $variables;
}

private function getApiForStore($storeId): MollieApiClient
Expand Down
3 changes: 2 additions & 1 deletion Service/Magento/SubscriptionAddProductToCart.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ private function setSubscriptionPrice(CartInterface $cart, Item $item, Subscript
$request->setProductClassId($item->getTaxClassId());
$taxRate = $this->taxCalculation->getRate($request);

$priceIncl = $subscription->amount->value;
$quantity = (float)($subscription->metadata->quantity ?? 1);
$priceIncl = $subscription->amount->value / $quantity;
$newPrice = $priceIncl;

if ($taxRate !== 0.0) {
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "mollie/magento2-subscriptions",
"description": "Mollie subscriptions extension for Magento 2",
"type": "magento2-module",
"version": "1.17.0",
"version": "1.17.1",
"license": [
"OSL-3.0",
"AFL-3.0"
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<default>
<mollie_subscriptions>
<general>
<version>v1.17.0</version>
<version>v1.17.1</version>
<enable>0</enable>
<debug>1</debug>
<update_subscription_when_price_changes>0</update_subscription_when_price_changes>
Expand Down
Loading