Skip to content

Commit

Permalink
Fixed #1186
Browse files Browse the repository at this point in the history
-  Save the fieldLayoutId correctly on products and variants.
-  Hard deleting now deletes the variants also
  • Loading branch information
lukeholder committed Apr 9, 2020
1 parent 7513135 commit fb041de
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
- Fixed the unnecessary logging of a deprecation error. ([#1365](https://github.com/craftcms/commerce/issues/1365))
- Fixed and error that prevented payments on orders in the control panel. ([#1362](https://github.com/craftcms/commerce/issues/1362))
- Fixed a bug that caused a redirect back to the Edit Customer page instead of the Edit User page editing an address from the Customer Info tab. ([#1368](https://github.com/craftcms/commerce/issues/1368))

- Hard deleting a product now correctly hard deletes orphaned variants. ([#1186](https://github.com/craftcms/commerce/issues/1186))

## 3.1.1 - 2020-04-03

### Changed
Expand Down
15 changes: 12 additions & 3 deletions src/elements/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -785,14 +785,23 @@ public function beforeValidate()
public function afterDelete()
{
$variants = Variant::find()
->productId($this->id)
->productId([$this->id, ':empty:'])
->all();

$elementsService = Craft::$app->getElements();

foreach ($variants as $variant) {

$hardDelete = false;
$variant->deletedWithProduct = true;
$elementsService->deleteElement($variant);

// The product ID is gone, so it has been hard deleted
if (!$variant->productId) {
$hardDelete = true;
$variant->deletedWithProduct = false;
}

$elementsService->deleteElement($variant, $hardDelete);
}

parent::afterDelete();
Expand Down Expand Up @@ -876,7 +885,7 @@ public function datetimeAttributes(): array
*/
public function getFieldLayout()
{
return $this->getType()->getFieldLayout();
return parent::getFieldLayout() ?? $this->getType()->getFieldLayout();
}

/**
Expand Down
15 changes: 15 additions & 0 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,21 @@ public function beforeValidate()
return parent::beforeValidate();
}

/**
* @param bool $isNew
* @return bool
* @throws InvalidConfigException
*/
public function beforeSave(bool $isNew): bool
{
// Set the field layout
/** @var ProductType $productType */
$productType = $this->getProduct()->getType();
$this->fieldLayoutId = $productType->getFieldLayout()->id;

return parent::beforeSave($isNew);
}

This comment has been minimized.

Copy link
@boboldehampsink

boboldehampsink Apr 22, 2020

Contributor

@lukeholder what is this logic exactly? This is totally breaking saving variants for me as it gets the wrong field layout? Why is it setting the product's field layout on the variant here before saving?

This comment has been minimized.

Copy link
@boboldehampsink

boboldehampsink Apr 22, 2020

Contributor

Shouldn't this be variantFieldLayoutId

This comment has been minimized.

Copy link
@boboldehampsink

boboldehampsink Apr 22, 2020

Contributor

Submitted a fix in #1403

/**
* @inheritdoc
*/
Expand Down

0 comments on commit fb041de

Please sign in to comment.