|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\PageBuilder\Model\Catalog; |
| 10 | + |
| 11 | +use Magento\Catalog\Model\Product\Attribute\Source\Status; |
| 12 | +use Magento\Catalog\Model\Product\Visibility; |
| 13 | +use Magento\Catalog\Model\ResourceModel\Product\Collection; |
| 14 | +use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; |
| 15 | +use Magento\CatalogInventory\Helper\Stock; |
| 16 | +use Magento\CatalogWidget\Model\Rule; |
| 17 | +use Magento\Framework\Exception\LocalizedException; |
| 18 | +use Magento\Rule\Model\Condition\Combine; |
| 19 | +use Magento\Rule\Model\Condition\Sql\Builder; |
| 20 | +use Magento\Widget\Helper\Conditions; |
| 21 | +use Zend_Db_Select_Exception; |
| 22 | + |
| 23 | +/** |
| 24 | + * Product totals for Products content type |
| 25 | + */ |
| 26 | +class ProductTotals |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var CollectionFactory |
| 30 | + */ |
| 31 | + private $productCollectionFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var Builder |
| 35 | + */ |
| 36 | + private $sqlBuilder; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var Rule |
| 40 | + */ |
| 41 | + private $rule; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var Conditions |
| 45 | + */ |
| 46 | + private $conditionsHelper; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var Stock |
| 50 | + */ |
| 51 | + private $stockFilter; |
| 52 | + |
| 53 | + /** |
| 54 | + * @param CollectionFactory $productCollectionFactory |
| 55 | + * @param Builder $sqlBuilder |
| 56 | + * @param Rule $rule |
| 57 | + * @param Conditions $conditionsHelper |
| 58 | + * @param Stock $stockFilter |
| 59 | + */ |
| 60 | + public function __construct( |
| 61 | + CollectionFactory $productCollectionFactory, |
| 62 | + Builder $sqlBuilder, |
| 63 | + Rule $rule, |
| 64 | + Conditions $conditionsHelper, |
| 65 | + Stock $stockFilter |
| 66 | + ) { |
| 67 | + $this->productCollectionFactory = $productCollectionFactory; |
| 68 | + $this->sqlBuilder = $sqlBuilder; |
| 69 | + $this->rule = $rule; |
| 70 | + $this->conditionsHelper = $conditionsHelper; |
| 71 | + $this->stockFilter = $stockFilter; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Decode the provided conditions |
| 76 | + * |
| 77 | + * @param string $conditions |
| 78 | + * @return Combine |
| 79 | + */ |
| 80 | + private function decodeConditions(string $conditions): Combine |
| 81 | + { |
| 82 | + if ($conditions) { |
| 83 | + $conditions = $this->conditionsHelper->decode($conditions); |
| 84 | + } |
| 85 | + |
| 86 | + foreach ($conditions as $key => $condition) { |
| 87 | + if (!empty($condition['attribute']) |
| 88 | + && in_array($condition['attribute'], ['special_from_date', 'special_to_date']) |
| 89 | + ) { |
| 90 | + $conditions[$key]['value'] = date('Y-m-d H:i:s', strtotime($condition['value'])); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + $this->rule->loadPost(['conditions' => $conditions]); |
| 95 | + return $this->rule->getConditions(); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Retrieve product collection based on provided conditions |
| 100 | + * |
| 101 | + * @param string $conditions |
| 102 | + * @return Collection |
| 103 | + * @throws LocalizedException |
| 104 | + */ |
| 105 | + private function getProductCollection(string $conditions): Collection |
| 106 | + { |
| 107 | + /** @var $collection Collection */ |
| 108 | + $collection = $this->productCollectionFactory->create(); |
| 109 | + |
| 110 | + /** @var Combine $collectionConditions */ |
| 111 | + $collectionConditions = $this->decodeConditions($conditions); |
| 112 | + $collectionConditions->collectValidatedAttributes($collection); |
| 113 | + $this->sqlBuilder->attachConditionToCollection($collection, $collectionConditions); |
| 114 | + |
| 115 | + /** |
| 116 | + * Prevent retrieval of duplicate records. This may occur when multiselect product attribute matches |
| 117 | + * several allowed values from condition simultaneously |
| 118 | + */ |
| 119 | + $collection->distinct(true); |
| 120 | + |
| 121 | + return $collection; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Retrieve count of all disabled products |
| 126 | + * |
| 127 | + * @param Collection $baseCollection |
| 128 | + * @return int number of disabled products |
| 129 | + */ |
| 130 | + private function getDisabledCount(Collection $baseCollection): int |
| 131 | + { |
| 132 | + /** @var Collection $disabledCollection */ |
| 133 | + $disabledCollection = clone $baseCollection; |
| 134 | + $disabledCollection->addAttributeToFilter('status', Status::STATUS_DISABLED); |
| 135 | + return $disabledCollection->getSize(); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Retrieve count of all not visible individually products |
| 140 | + * |
| 141 | + * @param Collection $baseCollection |
| 142 | + * @return int number of products not visible individually |
| 143 | + */ |
| 144 | + private function getNotVisibleCount(Collection $baseCollection): int |
| 145 | + { |
| 146 | + $notVisibleCollection = clone $baseCollection; |
| 147 | + $notVisibleCollection->addAttributeToFilter('status', Status::STATUS_ENABLED); |
| 148 | + $notVisibleCollection->addAttributeToFilter( |
| 149 | + 'visibility', |
| 150 | + [ |
| 151 | + Visibility::VISIBILITY_NOT_VISIBLE, |
| 152 | + Visibility::VISIBILITY_IN_SEARCH |
| 153 | + ] |
| 154 | + ); |
| 155 | + return $notVisibleCollection->getSize(); |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Retrieve count of all out of stock products |
| 160 | + * |
| 161 | + * @param Collection $baseCollection |
| 162 | + * @return int number of out of stock products |
| 163 | + * @throws Zend_Db_Select_Exception |
| 164 | + */ |
| 165 | + private function getOutOfStockCount(Collection $baseCollection): int |
| 166 | + { |
| 167 | + // Retrieve in stock products, then subtract them from the total |
| 168 | + $outOfStockCollection = clone $baseCollection; |
| 169 | + $this->stockFilter->addIsInStockFilterToCollection($outOfStockCollection); |
| 170 | + // Remove existing stock_status where condition from query |
| 171 | + $outOfStockWhere = $outOfStockCollection->getSelect()->getPart('where'); |
| 172 | + $outOfStockWhere = array_filter( |
| 173 | + $outOfStockWhere, |
| 174 | + function ($whereCondition) { |
| 175 | + return !stristr($whereCondition, 'stock_status'); |
| 176 | + } |
| 177 | + ); |
| 178 | + $outOfStockCollection->getSelect()->setPart('where', $outOfStockWhere); |
| 179 | + $outOfStockCollection->getSelect()->where( |
| 180 | + 'stock_status_index.stock_status = ?', |
| 181 | + \Magento\CatalogInventory\Model\Stock\Status::STATUS_OUT_OF_STOCK |
| 182 | + ); |
| 183 | + return $outOfStockCollection->getSize(); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Retrieve product totals for collection |
| 188 | + * |
| 189 | + * @param string $conditions |
| 190 | + * @return array |
| 191 | + * @throws LocalizedException |
| 192 | + * @throws Zend_Db_Select_Exception |
| 193 | + */ |
| 194 | + public function getProductTotals(string $conditions): array |
| 195 | + { |
| 196 | + /** @var Collection $collection */ |
| 197 | + $collection = $this->getProductCollection($conditions); |
| 198 | + |
| 199 | + // Exclude any linked products, e.g. simple products assigned to a configurable, bundle or group |
| 200 | + $collection->getSelect()->joinLeft( |
| 201 | + ['super_link_table' => $collection->getTable('catalog_product_super_link')], |
| 202 | + 'super_link_table.product_id = e.entity_id', |
| 203 | + ['product_id'] |
| 204 | + )->joinLeft( |
| 205 | + ['link_table' => $collection->getTable('catalog_product_link')], |
| 206 | + 'link_table.product_id = e.entity_id', |
| 207 | + ['product_id'] |
| 208 | + )->where('link_table.product_id IS NULL OR super_link_table.product_id IS NULL'); |
| 209 | + |
| 210 | + $disabledCount = $this->getDisabledCount($collection); |
| 211 | + $notVisibleCount = $this->getNotVisibleCount($collection); |
| 212 | + $outOfStockCount = $this->getOutOfStockCount($collection); |
| 213 | + |
| 214 | + return [ |
| 215 | + 'total' => $collection->getSize(), |
| 216 | + 'disabled' => $disabledCount, |
| 217 | + 'notVisible' => $notVisibleCount, |
| 218 | + 'outOfStock' => $outOfStockCount, |
| 219 | + ]; |
| 220 | + } |
| 221 | +} |
0 commit comments