Skip to content

Commit

Permalink
Handle SetAssociatedProductCategoriesCommand with new shop constraint…
Browse files Browse the repository at this point in the history
… ShopCollection
  • Loading branch information
jolelievre committed Dec 19, 2024
1 parent f9d6cf7 commit 5e73d6e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/Adapter/Category/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
namespace PrestaShop\PrestaShop\Adapter\Category\Repository;

use Category;
use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryException;
use PrestaShop\PrestaShop\Core\Domain\Category\Exception\CategoryNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Category\ValueObject\CategoryId;
use PrestaShop\PrestaShop\Core\Domain\Language\ValueObject\LanguageId;
use PrestaShop\PrestaShop\Core\Domain\Product\ValueObject\ProductId;
use PrestaShop\PrestaShop\Core\Domain\Shop\Exception\ShopNotFoundException;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopCollection;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopId;
use PrestaShop\PrestaShop\Core\Exception\CoreException;
Expand Down Expand Up @@ -280,6 +282,21 @@ public function getProductCategoryIds(ProductId $productId, ShopConstraint $shop
->andWhere('cs.id_shop = :shopId')
->setParameter('shopId', $shopConstraint->getShopId()->getValue())
;
} elseif ($shopConstraint instanceof ShopCollection && $shopConstraint->hasShopIds()) {
$qb
->innerJoin(
'cp',
$this->dbPrefix . 'category_shop',
'cs',
'cp.id_category = cs.id_category'
)
->andWhere('cs.id_shop IN (:shopIds)')
->setParameter(
'shopIds',
array_map(fn (ShopId $shopId) => $shopId->getValue(), $shopConstraint->getShopIds()),
ArrayParameterType::INTEGER
)
;
}

$results = $qb->executeQuery()->fetchAllAssociative();
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/Product/Update/ProductCategoryUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function updateCategories(ProductId $productId, array $newCategoryIds, Ca
$newCategoryIds = $this->formatCategoryIdsList($newCategoryIds, $defaultCategoryId);
$this->assertCategoriesExists($newCategoryIds);

// Get curren categories based on the provided shop constraint
// Get current categories based on the provided shop constraint
$this->deleteCategoriesAssociations($productId, $newCategoryIds, $shopConstraint);
$this->categoryRepository->addProductAssociations($productId, $newCategoryIds);
$this->updateDefaultCategory($productId, $defaultCategoryId, $shopConstraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PrestaShop\PrestaShop\Core\Domain\Product\Command\SetAssociatedProductCategoriesCommand;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\CannotUpdateProductException;
use PrestaShop\PrestaShop\Core\Domain\Product\Exception\ProductException;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopCollection;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopConstraint;
use Tests\Integration\Behaviour\Features\Context\Util\PrimitiveUtils;

Expand Down Expand Up @@ -62,6 +63,18 @@ public function assignToCategoriesForSpecificShop(string $productReference, Tabl
$this->assignToCategories($productReference, $table, ShopConstraint::shop($this->referenceToId($shopReference)));
}

/**
* @When I assign product :productReference to following categories for shops :shopReferences:
*
* @param string $productReference
* @param TableNode $table
* @param string $shopReferences
*/
public function assignToCategoriesForSpecificShopCollection(string $productReference, TableNode $table, string $shopReferences)
{
$this->assignToCategories($productReference, $table, ShopCollection::shops($this->referencesToIds($shopReferences)));
}

/**
* @When I assign product :productReference to following categories for all shops:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,3 +571,43 @@ Feature: Edit product with specific list of shops.
And product otherProductCopy is not associated to shops "shop1,shop4"
And default shop for product otherProductCopy is shop2
And product "otherProductCopy" should be disabled for shops "shop2,shop3"

Scenario: I can edit categories for specific shops
Given category "home" in default language named "Home" exists
And category "home" is the default one
And category "men" in default language named "Men" exists
And category "clothes" in default language named "Clothes" exists
And category "women" in default language named "Women" exists
And category "accessories" in default language named "Accessories" exists
And I edit home category "home" with following details:
| associated shops | shop1,shop2,shop3,shop4 |
And I edit category "clothes" with following details:
| associated shops | shop1,shop2,shop3,shop4 |
And I edit category "accessories" with following details:
| associated shops | shop1,shop2,shop3,shop4 |
# Men is only associated in shop1
And I edit category "men" with following details:
| associated shops | shop1 |
# Women is only associated in shop2 and is the default category
And I edit category "women" with following details:
| associated shops | shop2 |
And I set "women" as default category for shop shop2
Given product "product" should be assigned to following categories for shops "shop1,shop2,shop3,shop4":
| id reference | name | is default |
| home | Home | true |
# Now we assign categories for specific shops
When I assign product product to following categories for shops shop1,shop3:
| categories | [home, men, clothes] |
| default category | men |
# Category association is shared for all shops, the only thing that may vary is if the category is in the shop, the position and the default category
# Shop1 has the new update categories
Then product "product" should be assigned to following categories for shop shop1:
| id reference | name | is default |
| home | Home | false |
| men | Men | true |
| clothes | Clothes | false |
# The other shops have a new category clothes but keeps home as default, they don't have men because it is only on shop1
And product "product" should be assigned to following categories for shops "shop2,shop3,shop4":
| id reference | name | is default |
| home | Home | true |
| clothes | Clothes | false |

0 comments on commit 5e73d6e

Please sign in to comment.