Skip to content

Commit

Permalink
Fixed #3444 deleting shipping methods from footer action
Browse files Browse the repository at this point in the history
  • Loading branch information
nfourtythree committed Apr 30, 2024
1 parent eb41a7a commit 0ae2209
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft Commerce

## Unreleased

- Fixed a bug where the footer delete action wasn’t deleting shipping methods. ([#3444](https://github.com/craftcms/commerce/issues/3444))

## 4.5.4 - 2024-04-24

- Fixed a bug where date ranges weren’t displayed correctly on the Discounts index page. ([#3457](https://github.com/craftcms/commerce/issues/3457))
Expand Down
27 changes: 23 additions & 4 deletions src/controllers/ShippingCategoriesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,36 @@ public function actionSave(): ?Response
/**
* @throws HttpException
*/
public function actionDelete(): Response
public function actionDelete(): ?Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();

$id = $this->request->getRequiredBodyParam('id');
$id = $this->request->getBodyParam('id');
$ids = $this->request->getBodyParam('ids');

if (!Plugin::getInstance()->getShippingCategories()->deleteShippingCategoryById($id)) {
if ((!$id && empty($ids)) || ($id && !empty($ids))) {
throw new BadRequestHttpException('id or ids must be specified.');
}

if ($id) {
// If it is just the one id we know it has come from an ajax request on the table
$this->requireAcceptsJson();
$ids = [$id];
}

$failedIds = [];
foreach ($ids as $id) {
if (!Plugin::getInstance()->getShippingCategories()->deleteShippingCategoryById($id)) {
$failedIds[] = $id;
}
}

if (!empty($failedIds)) {
// @TODO: re-word this message at next translations update
return $this->asFailure(Craft::t('commerce', 'Could not delete shipping category'));
}

// @TODO: re-word add better message in next translation update
return $this->asSuccess();
}

Expand Down
25 changes: 22 additions & 3 deletions src/controllers/ShippingMethodsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,33 @@ public function actionSave(): ?Response
public function actionDelete(): ?Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();

$id = $this->request->getRequiredBodyParam('id');
$id = $this->request->getBodyParam('id');
$ids = $this->request->getBodyParam('ids');

if (!Plugin::getInstance()->getShippingMethods()->deleteShippingMethodById($id)) {
if ((!$id && empty($ids)) || ($id && !empty($ids))) {
throw new BadRequestHttpException('id or ids must be specified.');
}

if ($id) {
// If it is just the one id we know it has come from an ajax request on the table
$this->requireAcceptsJson();
$ids = [$id];
}

$failedIds = [];
foreach ($ids as $id) {
if (!Plugin::getInstance()->getShippingMethods()->deleteShippingMethodById($id)) {
$failedIds[] = $id;
}
}

if (!empty($failedIds)) {
// @TODO: re-word this message at next translations update
return $this->asFailure(Craft::t('commerce', 'Could not delete shipping method and its rules.'));
}

// @TODO: re-word add better message in next translation update
return $this->asSuccess();
}

Expand Down
10 changes: 10 additions & 0 deletions src/services/ShippingCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,16 @@ public function deleteShippingCategoryById(int $id): bool
return false;
}

// Find the shipping category and check it isn't the default
/** @var ShippingCategory $shippingCategory */
$shippingCategory = ArrayHelper::firstWhere($all, function(ShippingCategory $s) use ($id) {
return $s->id == $id;
});

if ($shippingCategory->default) {
return false;
}

$affectedRows = Craft::$app->getDb()->createCommand()
->softDelete(\craft\commerce\db\Table::SHIPPINGCATEGORIES, ['id' => $id])
->execute();
Expand Down

0 comments on commit 0ae2209

Please sign in to comment.