Skip to content

Commit

Permalink
Fixed bug where shipping categories were displaying incorrectly
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Sep 25, 2024
1 parent 8710f1c commit 292ff1b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

## Unreleased

- Fixed a PHP error that could occur when creating a new product.
- Fixed a PHP error that could occur when creating a new product.
- Fixed a bug where variants weren’t respecting their owner’s propagation method.
- Fixed a PHP error that could occur when creating a new product.
- Fixed a bug where shipping categories for other stores were showing up for selection on the Edit Product screen. ([#3690](https://github.com/craftcms/commerce/issues/3690))

## 5.1.2 - 2024-09-19

Expand Down
20 changes: 17 additions & 3 deletions src/elements/Variant.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use craft\commerce\events\CustomizeVariantSnapshotFieldsEvent;
use craft\commerce\helpers\Purchasable as PurchasableHelper;
use craft\commerce\models\ProductType;
use craft\commerce\models\ShippingCategory;
use craft\commerce\Plugin;
use craft\commerce\records\Variant as VariantRecord;
use craft\db\Query;
Expand Down Expand Up @@ -1182,12 +1183,25 @@ public function defineRules(): array
*/
protected function availableShippingCategories(): array
{
$allAvailableShippingCategories = parent::availableShippingCategories();

$productTypeId = $this->getPrimaryOwner()?->getType()->id;
if ($productTypeId) {
return Plugin::getInstance()->getShippingCategories()->getShippingCategoriesByProductTypeId($productTypeId);

if (!$productTypeId) {
return [Plugin::getInstance()->getShippingCategories()->getDefaultShippingCategory($this->storeId)];
}

// Limit to only those for this product type
$categoryIds = collect(Plugin::getInstance()->getShippingCategories()->getShippingCategoriesByProductTypeId($productTypeId))->pluck('id')->toArray();
$available = collect($allAvailableShippingCategories)->filter(function(ShippingCategory $category) use ($categoryIds) {
return in_array($category->id, $categoryIds);
});

if ($available->isEmpty()) {
return [Plugin::getInstance()->getShippingCategories()->getDefaultShippingCategory($this->storeId)];
}

return parent::availableShippingCategories();
return $available->toArray();
}

/**
Expand Down

0 comments on commit 292ff1b

Please sign in to comment.