diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 3da54487c5..01c2b43223 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -8,6 +8,7 @@ - Logged-in users who own the order or have appropriate permissions bypass the email verification flow and can download PDFs directly. - Added a new system email message for sending PDF download links to customers. - It is now possible to select multiple products in variant conditions. ([#4166](https://github.com/craftcms/commerce/pull/4166)) +- Variants now include their owner products' title when being displayed in the control panel. ([#4155](https://github.com/craftcms/commerce/issues/4155)) - It is now possible to select multiple specific variants in pricing rule’s “Match Variant” condition. ([#4167](https://github.com/craftcms/commerce/issues/4167)) - It is now possible to select multiple specific users in pricing rule’s “Match Customer” condition. ([#4167](https://github.com/craftcms/commerce/issues/4167)) diff --git a/src/elements/Variant.php b/src/elements/Variant.php index 5cd2a09b50..2875650dd7 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -256,6 +256,31 @@ public function init(): void $this->ownerType = Product::class; } + /** + * @inheritdoc + */ + public function getUiLabel(): string + { + $request = Craft::$app->getRequest(); + $referrer = $request->getReferrer() ?? ''; + $pathInfo = $request->getPathInfo(); + + $isCommerceProductContext = ( + str_contains($pathInfo, 'commerce/products') || + ($request->getIsAjax() && str_contains($referrer, 'commerce/products')) + ); + + if ($isCommerceProductContext || !$this->owner) { + return parent::getUiLabel(); + } + + $labelParts = Craft::$app->getLocale()->getOrientation() === 'rtl' + ? [parent::getUiLabel(), $this->owner->getUiLabel()] + : [$this->owner->getUiLabel(), parent::getUiLabel()]; + + return implode(' : ', $labelParts); + } + /** * @inheritdoc */ @@ -1181,7 +1206,7 @@ public function beforeSave(bool $isNew): bool // Validate shipping category ID is available for this product type $availableShippingCategories = $this->availableShippingCategories(); $availableShippingCategoryIds = ArrayHelper::getColumn($availableShippingCategories, 'id'); - + // If the current shipping category ID is not in the available categories, set it to the default one $currentShippingCategoryId = $this->getShippingCategoryId(); if (!in_array($currentShippingCategoryId, $availableShippingCategoryIds)) {