How to Manage Inactive Products in Shopware
When integrating orders from another e-commerce platform into your Shopware shop, you may need to include items that are inactive in Shopware within these orders. This article explains the issue and provides a solution to store these deactivated items in Shopware orders.
Issue Faced
When integrating orders from another e-commerce platform into the Shopware system, there is no need to display these items on the Shopware frontend, so we have deactivated them. However, deactivated items cannot be retrieved using the SalesChannel product repository when inserting ERP orders into Shopware.
The default restriction within Shopware limits the visibility of products based on their active status, preventing inactive products from being fetched.
Default Product Availability Filter
By default, Shopware uses the ProductAvailableFilter
class to determine which products are available for a given sales channel. The relevant code snippet is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | ```php <?php declare(strict_types=1); namespace Shopware\Core\Content\Product\SalesChannel; use Shopware\Core\Content\Product\Aggregate\ProductVisibility\ProductVisibilityDefinition; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter; use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter; use Shopware\Core\Framework\Log\Package; #[Package('inventory')] class ProductAvailableFilter extends MultiFilter { public function __construct(string $salesChannelId, int $visibility = ProductVisibilityDefinition::VISIBILITY_ALL) { parent::__construct( self::CONNECTION_AND, [ new RangeFilter('product.visibilities.visibility', [RangeFilter::GTE => $visibility]), new EqualsFilter('product.visibilities.salesChannelId', $salesChannelId), new EqualsFilter('product.active', true), ] ); } } ``` |
In this filter, the line new EqualsFilter('product.active', true)
ensures that only active products are returned.
Solution: Customizing Product Filters
To retrieve inactive products in Shopware, you need to extend the ProductAvailableFilter
to modify the behavior of the product retrieval process. Below is a step-by-step solution:
Step 1: Create a Custom Filter
You can create a custom filter class that extends the existing ProductAvailableFilter
. Here’s how:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | ```php <?php namespace AldiOrderIntegration\Services; use Shopware\Core\Content\Product\SalesChannel\ProductAvailableFilter; class CustomProductAvailableFilter extends ProductAvailableFilter { public function __construct(string $salesChannelId, int $visibility = 0) { parent::__construct($salesChannelId, $visibility); $this->queries = []; } } ``` |
In this custom filter, you override the visibility setting to allow fetching inactive products.
Step 2: Implement the Custom Filter in Your Code
Use your CustomProductAvailableFilter
in place of the default ProductAvailableFilter
when you are querying the product repository:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $filter = new CustomProductAvailableFilter($salesChannelId); $criteria = new Criteria(); $criteria->addFilter($filter); $criteria->addFilter(new EqualsFilter('manufacturerNumber', $productData['productCode'])); $criteria->addAssociation('options.group'); $product = $this->productRepository->search($criteria, $salesChannelContext)->first(); |
Conclusion
These steps allow you to successfully modify the default behavior of Shopware’s product filtering system. This allows you to retrieve inactive products when integrating orders from another e-commerce platform. By carrying out this customization, you will have greater control over which products are visible and accessible in your Shopware shop. If you’re looking for a reliable Shopware agency to help with your e-commerce needs, feel free to reach out to us.
Recent help desk articles
Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.