diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f50ed36e1..69a8dfe735 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,9 @@ # Release Notes for Craft Commerce -## Unreleased +## 5.2.1 - 2024-10-23 -- Fixed an SQL error that occurred when duplicating variants. ([#3727](https://github.com/craftcms/commerce/issues/3727)) +- Fixed a bug where the Commerce subnav could be missing the “Product” nav item. ([#3735](https://github.com/craftcms/commerce/issues/3735)) +- Fixed PHP errors that could occur when completing an order. ([#3733](https://github.com/craftcms/commerce/issues/3733), [#3736](https://github.com/craftcms/commerce/issues/3736)) ## 5.2.0 - 2024-10-16 @@ -25,12 +26,12 @@ - Added `craft\commerce\elements\db\Purchasable::onPromotion()`. - Added `craft\commerce\events\UpgradeEvent`. - Added `craft\commerce\models\Discount::$requireCouponCode`. -- Added `craft\commerce\models\ProductType::$isStructure` -- Added `craft\commerce\models\ProductType::$maxLevels` -- Added `craft\commerce\models\ProductType::$structureId` -- Added `craft\commerce\models\ProductType::DEFAULT_PLACEMENT_BEGINNING` -- Added `craft\commerce\models\ProductType::DEFAULT_PLACEMENT_END` -- Added `craft\commerce\models\ProductType::getConfig()` +- Added `craft\commerce\models\ProductType::$isStructure`. +- Added `craft\commerce\models\ProductType::$maxLevels`. +- Added `craft\commerce\models\ProductType::$structureId`. +- Added `craft\commerce\models\ProductType::DEFAULT_PLACEMENT_BEGINNING`. +- Added `craft\commerce\models\ProductType::DEFAULT_PLACEMENT_END`. +- Added `craft\commerce\models\ProductType::getConfig()`. ### System - Improved the performance of adding items to the cart. diff --git a/src/controllers/DownloadsController.php b/src/controllers/DownloadsController.php index 92011c410d..be5685e834 100644 --- a/src/controllers/DownloadsController.php +++ b/src/controllers/DownloadsController.php @@ -72,7 +72,7 @@ public function actionPdf(): Response $renderedPdf = Plugin::getInstance()->getPdfs()->renderPdfForOrder($order, $option, null, [], $pdf); // Set previous language back - Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale); + Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale->id); $fileName = $this->getView()->renderObjectTemplate((string)$pdf->fileNameFormat, $order); if (!$fileName) { diff --git a/src/controllers/OrdersController.php b/src/controllers/OrdersController.php index f0dc5d5a42..507f6cb17b 100644 --- a/src/controllers/OrdersController.php +++ b/src/controllers/OrdersController.php @@ -932,7 +932,7 @@ public function actionSendEmail(): Response } // Set previous language back - Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale); + Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale->id); if (!$success) { $error = $error ?: Craft::t('commerce', 'Could not send email'); diff --git a/src/elements/Product.php b/src/elements/Product.php index deb74ec546..98eea0d891 100644 --- a/src/elements/Product.php +++ b/src/elements/Product.php @@ -1655,18 +1655,12 @@ public function setAttributes($values, $safeOnly = true): void */ public function getFieldLayout(): ?FieldLayout { - $fieldLayout = parent::getFieldLayout(); - if ($fieldLayout) { - return $fieldLayout; - } - - $fieldLayout = $this->getType()->getFieldLayout(); - if ($fieldLayout->id) { - $this->fieldLayoutId = $fieldLayout->id; - return $fieldLayout; + try { + return $this->getType()->getProductFieldLayout(); + } catch (InvalidConfigException) { + // The product type was probably deleted + return null; } - - return null; } /** diff --git a/src/elements/Variant.php b/src/elements/Variant.php index c6701ff8fa..54380372c1 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -428,16 +428,17 @@ public function attributes(): array * @throws InvalidConfigException * @throws InvalidConfigException */ + /** + * @inheritdoc + */ public function getFieldLayout(): ?FieldLayout { - $fieldLayout = parent::getFieldLayout(); - - if (!$fieldLayout && $this->getOwnerId()) { - $fieldLayout = $this->getOwner()->getType()->getVariantFieldLayout(); - $this->fieldLayoutId = $fieldLayout->id; + try { + return $this->getOwner()->getType()->getVariantFieldLayout(); + } catch (InvalidConfigException) { + // The product type was probably deleted + return null; } - - return $fieldLayout; } /** diff --git a/src/models/ProductType.php b/src/models/ProductType.php index 15745ea3fa..8cccbe2c55 100644 --- a/src/models/ProductType.php +++ b/src/models/ProductType.php @@ -9,6 +9,7 @@ use Craft; use craft\base\Field; +use craft\base\FieldLayoutProviderInterface; use craft\behaviors\FieldLayoutBehavior; use craft\commerce\base\Model; use craft\commerce\elements\Product; @@ -33,7 +34,6 @@ /** * Product type model. * @method null setFieldLayout(FieldLayout $fieldLayout) - * @method FieldLayout getFieldLayout() * * @property string $cpEditUrl * @property string $cpEditVariantUrl @@ -47,7 +47,7 @@ * @author Pixel & Tonic, Inc. * @since 2.0 */ -class ProductType extends Model +class ProductType extends Model implements FieldLayoutProviderInterface { /** @since 5.2.0 */ public const DEFAULT_PLACEMENT_BEGINNING = 'beginning'; @@ -224,6 +224,14 @@ public function __toString() return (string)$this->handle; } + /** + * @inerhitdoc + */ + public function getHandle(): ?string + { + return $this->handle; + } + /** * @inheritdoc */ @@ -388,6 +396,11 @@ public function setTaxCategories(array $taxCategories): void $this->_taxCategories = $categories; } + public function getFieldLayout(): FieldLayout + { + return $this->getProductFieldLayout(); + } + /** * @throws InvalidConfigException */ @@ -517,9 +530,8 @@ public function getHasVariants(): bool /** * @inheritdoc */ - public function behaviors(): array + protected function defineBehaviors(): array { - $behaviors = parent::behaviors(); $behaviors['productFieldLayout'] = [ 'class' => FieldLayoutBehavior::class, 'elementType' => Product::class, diff --git a/src/queue/jobs/SendEmail.php b/src/queue/jobs/SendEmail.php index 06750134b3..60ae119a6d 100644 --- a/src/queue/jobs/SendEmail.php +++ b/src/queue/jobs/SendEmail.php @@ -76,7 +76,7 @@ public function execute($queue): void } // Set previous language back - Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale); + Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale->id); $this->setProgress($queue, 1); } diff --git a/src/services/LineItems.php b/src/services/LineItems.php index c8a65c7142..e7627fde24 100644 --- a/src/services/LineItems.php +++ b/src/services/LineItems.php @@ -520,7 +520,7 @@ public function eagerLoadLineItemsForOrders(array $orders): array public function orderCompleteHandler(LineItem $lineItem, Order $order): void { // Called the after order complete method for the purchasable if there is one - if ($lineItem->getPurchasable()) { + if ($lineItem->type === LineItemType::Purchasable && $lineItem->getPurchasable()) { $lineItem->getPurchasable()->afterOrderComplete($order, $lineItem); } diff --git a/src/services/OrderStatuses.php b/src/services/OrderStatuses.php index 03a8a7ecec..80c8c864ca 100644 --- a/src/services/OrderStatuses.php +++ b/src/services/OrderStatuses.php @@ -502,7 +502,7 @@ public function statusChangeHandler(Order $order, OrderHistory $orderHistory): v } // Set previous language back - Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale); + Locale::switchAppLanguage($originalLanguage, $originalFormattingLocale->id); } /** diff --git a/src/services/Pdfs.php b/src/services/Pdfs.php index 86875b8ab6..444e054dc3 100644 --- a/src/services/Pdfs.php +++ b/src/services/Pdfs.php @@ -509,7 +509,7 @@ public function renderPdfForOrder(Order $order, string $option = '', string $tem if (!$event->template || !$view->doesTemplateExist($event->template)) { // Restore the original template mode $view->setTemplateMode($oldTemplateMode); - Locale::switchAppLanguage($originalLanguage, $originalFormattingLanguage); + Locale::switchAppLanguage($originalLanguage, $originalFormattingLanguage->id); throw new Exception('PDF template file does not exist.'); } @@ -518,14 +518,14 @@ public function renderPdfForOrder(Order $order, string $option = '', string $tem // TODO Add event $html = $view->renderTemplate($event->template, $variables); } catch (\Exception $e) { - Locale::switchAppLanguage($originalLanguage, $originalFormattingLanguage); + Locale::switchAppLanguage($originalLanguage, $originalFormattingLanguage->id); // Set the pdf html to the render error. Craft::error('Order PDF render error. Order number: ' . $order->getShortNumber() . '. ' . $e->getMessage()); Craft::$app->getErrorHandler()->logException($e); $html = Craft::t('commerce', 'An error occurred while generating this PDF.'); } - Locale::switchAppLanguage($originalLanguage, $originalFormattingLanguage); + Locale::switchAppLanguage($originalLanguage, $originalFormattingLanguage->id); // Restore the original template mode $view->setTemplateMode($oldTemplateMode);