Skip to content

Commit

Permalink
Fix double sorting for some attribute, fix configurable products issue
Browse files Browse the repository at this point in the history
  • Loading branch information
tuyennn committed Aug 14, 2023
1 parent 4950d69 commit 2ef6829
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 2 deletions.
38 changes: 38 additions & 0 deletions Model/Indexer/Stock/Configurable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace GhoSter\OutOfStockAtLast\Model\Indexer\Stock;

use Magento\ConfigurableProduct\Model\ResourceModel\Indexer\Stock\Configurable as DefaultIndexer;
use Magento\Framework\DB\Select;

class Configurable extends DefaultIndexer
{
/**
* @inheritdoc
*/
protected function _getStockStatusSelect($entityIds = null, $usePrimaryTable = false)
{
$select = parent::_getStockStatusSelect($entityIds, $usePrimaryTable);
$this->_autoCalculate($select);

return $select;
}

/**
* Calculate depends on simple products
*
* @param Select $select
* @throws \Zend_Db_Select_Exception
*/
private function _autoCalculate($select)
{
$columns = $select->getPart(Select::COLUMNS);
foreach ($columns as &$column) {
if (isset($column[2]) && $column[2] == 'qty') {
$column[1] = new \Zend_Db_Expr('SUM(IF(i.stock_status > 0, i.qty, 0))');
}
}
$select->setPart(Select::COLUMNS, $columns);
}
}
27 changes: 25 additions & 2 deletions Plugin/Model/ResourceModel/Product/CollectionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ public function aroundSetOrder(
*/
private function applyOutOfStockAtLastOrders(Collection $collection)
{
if (!$collection->getFlag('is_sorted_by_oos')) {
$collection->setFlag('is_sorted_by_oos', true);
if (!$collection->getFlag('sorted_by_oos_flag')) {
$collection->setFlag('sorted_by_oos_flag', true);
$collection->setOrder('out_of_stock_at_last', Select::SQL_DESC);
}
}
Expand All @@ -110,4 +110,27 @@ public function beforeAddOrder(

return $result ?? [$attribute, $dir];
}

/**
* Prevent double sorting by some attribute.
*
* @param Collection $collection
* @param callable $proceed
* @param string $attribute
* @param string $dir
* @return Collection
*/
public function aroundAddAttributeToSort(
Collection $collection,
callable $proceed,
string $attribute,
string $dir = Collection::SORT_ORDER_ASC
): Collection {
if (!$collection->getFlag(sprintf('sorted_by_%s_attribute', $attribute))) {
$collection->setFlag(sprintf('sorted_by_%s_attribute', $attribute), true);
$proceed($attribute, $dir);
}

return $collection;
}
}
8 changes: 8 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,12 @@
<plugin sortOrder="10" name="GhoSter_OutOfStockAtLast::mark_apply_flag"
type="GhoSter\OutOfStockAtLast\Plugin\Model\ResourceModel\Fulltext\Collection\SearchResultApplierPlugin"/>
</type>

<type name="Magento\Catalog\Model\ProductTypes\Config\Reader">
<arguments>
<argument name="idAttributes" xsi:type="array">
<item name="/config/type/stockIndexerModel" xsi:type="string"/>
</argument>
</arguments>
</type>
</config>
6 changes: 6 additions & 0 deletions etc/product_types.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/product_types.xsd">
<type name="configurable" label="Configurable Product" modelInstance="Magento\ConfigurableProduct\Model\Product\Type\Configurable">
<stockIndexerModel instance="GhoSter\OutOfStockAtLast\Model\Indexer\Stock\Configurable" />
</type>
</config>

0 comments on commit 2ef6829

Please sign in to comment.