Skip to content

Commit

Permalink
Merge branch '5.x' into bugfix/product-variant-revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Nov 4, 2024
2 parents 8f0b679 + 3ca96db commit a22491f
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 38 deletions.
17 changes: 9 additions & 8 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/DownloadsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
16 changes: 5 additions & 11 deletions src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
15 changes: 8 additions & 7 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
20 changes: 16 additions & 4 deletions src/models/ProductType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +34,6 @@
/**
* Product type model.
* @method null setFieldLayout(FieldLayout $fieldLayout)
* @method FieldLayout getFieldLayout()
*
* @property string $cpEditUrl
* @property string $cpEditVariantUrl
Expand All @@ -47,7 +47,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 2.0
*/
class ProductType extends Model
class ProductType extends Model implements FieldLayoutProviderInterface
{
/** @since 5.2.0 */
public const DEFAULT_PLACEMENT_BEGINNING = 'beginning';
Expand Down Expand Up @@ -224,6 +224,14 @@ public function __toString()
return (string)$this->handle;
}

/**
* @inerhitdoc
*/
public function getHandle(): ?string
{
return $this->handle;
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -388,6 +396,11 @@ public function setTaxCategories(array $taxCategories): void
$this->_taxCategories = $categories;
}

public function getFieldLayout(): FieldLayout
{
return $this->getProductFieldLayout();
}

/**
* @throws InvalidConfigException
*/
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/queue/jobs/SendEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/LineItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/OrderStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/services/Pdfs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
Expand All @@ -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);

Expand Down

0 comments on commit a22491f

Please sign in to comment.