From 0291ca5397a34575617ec8c9c05e04be65f86d24 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 12 Nov 2025 21:46:11 +0800 Subject: [PATCH 1/5] Show product title on variant chip Potential fix for #4155 --- CHANGELOG-WIP.md | 1 + src/elements/Variant.php | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 2d101d2596..f50baadd95 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)) ### Administration - Added billing and shipping address conditions to gateways. ([#4100](https://github.com/craftcms/commerce/pull/4100)) diff --git a/src/elements/Variant.php b/src/elements/Variant.php index 5cd2a09b50..c2bc971934 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -256,6 +256,33 @@ public function init(): void $this->ownerType = Product::class; } + /** + * @inheritdoc + */ + public function getUiLabel(): string + { + $referrer = Craft::$app->getRequest()->getReferrer(); + $isAjax = Craft::$app->getRequest()->getIsAjax(); + $pathInfo = Craft::$app->getRequest()->getPathInfo(); + if ( + ($isAjax && str_contains($referrer, 'commerce') && str_contains($referrer, 'products')) + || + (str_contains($pathInfo, 'commerce') && str_contains($pathInfo, 'products')) + ) { + return parent::getUiLabel(); + } + + if (!$this->owner) { + return parent::getUiLabel(); + } + + if (Craft::$app->getLocale()->getOrientation() == 'rtl') { + return Html::encode(parent::getUiLabel() . ' : ' . $this->owner->getUiLabel()); + } + + return Html::encode($this->owner->getUiLabel() . ' : ' . parent::getUiLabel()); + } + /** * @inheritdoc */ @@ -1181,7 +1208,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)) { From 0c450a2b8407f3fc13e3cd30fe085170786ac06f Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 12 Nov 2025 21:50:18 +0800 Subject: [PATCH 2/5] Cleanup --- src/elements/Variant.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/elements/Variant.php b/src/elements/Variant.php index c2bc971934..98d6aa03cc 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -261,9 +261,10 @@ public function init(): void */ public function getUiLabel(): string { - $referrer = Craft::$app->getRequest()->getReferrer(); - $isAjax = Craft::$app->getRequest()->getIsAjax(); - $pathInfo = Craft::$app->getRequest()->getPathInfo(); + $request = Craft::$app->getRequest(); + $referrer = $request->getReferrer(); + $isAjax = $request->getIsAjax(); + $pathInfo = $request->getPathInfo(); if ( ($isAjax && str_contains($referrer, 'commerce') && str_contains($referrer, 'products')) || From c5961c114a9e64d641b28da8bcf1ba5285633bc8 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 12 Nov 2025 21:51:16 +0800 Subject: [PATCH 3/5] Not needed --- src/elements/Variant.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/elements/Variant.php b/src/elements/Variant.php index 98d6aa03cc..812e4bf2db 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -278,10 +278,10 @@ public function getUiLabel(): string } if (Craft::$app->getLocale()->getOrientation() == 'rtl') { - return Html::encode(parent::getUiLabel() . ' : ' . $this->owner->getUiLabel()); + return parent::getUiLabel() . ' : ' . $this->owner->getUiLabel(); } - return Html::encode($this->owner->getUiLabel() . ' : ' . parent::getUiLabel()); + return $this->owner->getUiLabel() . ' : ' . parent::getUiLabel(); } /** From 48ccfdce593ada7aa352aab448611e1ca4980406 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 12 Nov 2025 21:53:13 +0800 Subject: [PATCH 4/5] Cleanup --- src/elements/Variant.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/elements/Variant.php b/src/elements/Variant.php index 812e4bf2db..3876ec3529 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -262,26 +262,23 @@ public function init(): void public function getUiLabel(): string { $request = Craft::$app->getRequest(); - $referrer = $request->getReferrer(); - $isAjax = $request->getIsAjax(); - $pathInfo = $request->getPathInfo(); - if ( - ($isAjax && str_contains($referrer, 'commerce') && str_contains($referrer, 'products')) - || - (str_contains($pathInfo, 'commerce') && str_contains($pathInfo, 'products')) - ) { - return parent::getUiLabel(); - } + $referrer = $request->getReferrer() ?? ''; + $pathInfo = $request->getPathInfo() ?? ''; - if (!$this->owner) { + $isCommerceProductContext = ( + str_contains($pathInfo, 'commerce/products') || + ($request->getIsAjax() && str_contains($referrer, 'commerce/products')) + ); + + if ($isCommerceProductContext || !$this->owner) { return parent::getUiLabel(); } - if (Craft::$app->getLocale()->getOrientation() == 'rtl') { - return parent::getUiLabel() . ' : ' . $this->owner->getUiLabel(); - } + $labelParts = Craft::$app->getLocale()->getOrientation() === 'rtl' + ? [parent::getUiLabel(), $this->owner->getUiLabel()] + : [$this->owner->getUiLabel(), parent::getUiLabel()]; - return $this->owner->getUiLabel() . ' : ' . parent::getUiLabel(); + return implode(' : ', $labelParts); } /** From 7b366472a07bec573a75069972b77567ca0c7839 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Wed, 12 Nov 2025 21:59:22 +0800 Subject: [PATCH 5/5] Not nullable anyway --- src/elements/Variant.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/elements/Variant.php b/src/elements/Variant.php index 3876ec3529..2875650dd7 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -263,7 +263,7 @@ public function getUiLabel(): string { $request = Craft::$app->getRequest(); $referrer = $request->getReferrer() ?? ''; - $pathInfo = $request->getPathInfo() ?? ''; + $pathInfo = $request->getPathInfo(); $isCommerceProductContext = ( str_contains($pathInfo, 'commerce/products') ||