diff --git a/src/elements/Product.php b/src/elements/Product.php index 32d692a46c..aa17886e5b 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 9758491a38..85f1521522 100755 --- a/src/elements/Variant.php +++ b/src/elements/Variant.php @@ -420,16 +420,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..14ec536d8a 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->productFieldLayout(); + } + /** * @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,