Skip to content

Commit d15463c

Browse files
committed
PB-229: Story Bug for PB-107: Incorrect Number of Products
- Fix counts
1 parent 345624e commit d15463c

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

app/code/Magento/PageBuilder/Controller/Adminhtml/Form/Element/ProductTotals.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ class ProductTotals extends \Magento\Backend\App\Action implements HttpPostActio
5050
*/
5151
private $jsonFactory;
5252

53+
/**
54+
* @var \Magento\CatalogInventory\Helper\Stock
55+
*/
56+
private $stockFilter;
57+
5358
/**
5459
* Constructor.
5560
*
@@ -68,14 +73,16 @@ public function __construct(
6873
\Magento\CatalogWidget\Model\Rule $rule,
6974
\Magento\Widget\Helper\Conditions $conditionsHelper,
7075
\Magento\Catalog\Api\CategoryRepositoryInterface $categoryRepository,
71-
\Magento\Framework\Controller\Result\JsonFactory $jsonFactory
76+
\Magento\Framework\Controller\Result\JsonFactory $jsonFactory,
77+
\Magento\CatalogInventory\Helper\Stock $stockFilter
7278
) {
7379
$this->productCollectionFactory = $productCollectionFactory;
7480
$this->sqlBuilder = $sqlBuilder;
7581
$this->rule = $rule;
7682
$this->conditionsHelper = $conditionsHelper;
7783
$this->categoryRepository = $categoryRepository;
7884
$this->jsonFactory = $jsonFactory;
85+
$this->stockFilter = $stockFilter;
7986
parent::__construct($context);
8087
}
8188

@@ -135,26 +142,30 @@ public function execute()
135142
{
136143
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
137144
$collection = $this->createCollection();
145+
$this->stockFilter->addInStockFilterToCollection($collection);
138146
$collection->getSelect()->joinLeft(
139147
['super_link_table' => $collection->getTable('catalog_product_super_link')],
140148
'super_link_table.product_id = e.entity_id',
141149
['product_id']
142150
)->joinLeft(
143151
['link_table' => $collection->getTable('catalog_product_link')],
144-
'link_table.linked_product_id = e.entity_id',
152+
'link_table.product_id = e.entity_id',
145153
['product_id']
146-
)->where('link_table.linked_product_id IS NULL AND super_link_table.product_id IS NULL');
154+
)->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL');
155+
156+
// Clone the collection before we add enabled status filter
157+
$disabledProductsCollection = clone $collection;
158+
$disabledProductsCollection->addAttributeToFilter('status', Status::STATUS_DISABLED);
147159

148-
$totalProducts = $collection->getSize();
149-
$disabledProducts = $collection
150-
->addAttributeToFilter('status', Status::STATUS_DISABLED)
151-
->getSize();
160+
// Only display enabled products in totals count
161+
$collection->addAttributeToFilter('status', Status::STATUS_ENABLED);
162+
$collection->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH);
152163

153164
return $this->jsonFactory->create()
154165
->setData(
155166
[
156-
'total' => $totalProducts,
157-
'disabled' => $disabledProducts
167+
'total' => $collection->getSize(),
168+
'disabled' => $disabledProductsCollection->getSize()
158169
]
159170
);
160171
}

0 commit comments

Comments
 (0)