Skip to content

Commit

Permalink
Merge pull request #11 from tuyennn/feature/issue-10-sort-order-by-price
Browse files Browse the repository at this point in the history
[Issue#10]: Fix order by price it doesn't work due missing flag to co…
  • Loading branch information
tuyennn authored Sep 22, 2021
2 parents 784861b + 906cc21 commit c0d802f
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 29 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ dist: trusty
language: php

php:
- 7.2
- 7.3
- 7.4

Expand Down
12 changes: 9 additions & 3 deletions Model/Elasticsearch/Adapter/DataMapper/Stock.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Magento\Store\Model\StoreManagerInterface;
use GhoSter\OutOfStockAtLast\Model\ResourceModel\Inventory;

/**
* phpcs:ignore Magento2.Legacy.Copyright.FoundCopyrightMissingOrWrongFormat
* Class Stock for mapping
*/
class Stock
{
/**
Expand All @@ -33,12 +37,14 @@ public function __construct(
}

/**
* @param $entityId
* @param $storeId
* Map the attribute
*
* @param mixed $entityId
* @param mixed $storeId
* @return bool[]|int[]
* @throws NoSuchEntityException
*/
public function map($entityId, $storeId)
public function map($entityId, $storeId): array
{
$sku = $this->inventory->getSkuRelation((int)$entityId);

Expand Down
28 changes: 23 additions & 5 deletions Model/ResourceModel/Inventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
use Magento\Framework\Model\ResourceModel\Db\Context;
use Magento\Framework\Module\Manager;

/**
* phpcs:ignore Magento2.Legacy.Copyright.FoundCopyrightMissingOrWrongFormat
* Class Inventory for stock processing and calculation
*/
class Inventory extends AbstractDb
{
/**
Expand Down Expand Up @@ -38,35 +42,41 @@ class Inventory extends AbstractDb

/**
* Inventory constructor.
*
* @param Manager $moduleManager
* @param StockRegistryInterface $stockRegistry
* @param Context $context
* @param null $connectionName
* @param ?string $connectionName
*/
public function __construct(
Manager $moduleManager,
StockRegistryInterface $stockRegistry,
Context $context,
$connectionName = null
string $connectionName = null
) {
parent::__construct($context, $connectionName);
$this->moduleManager = $moduleManager;
$this->stockRegistry = $stockRegistry;
}

/**
* Initialize
*/
protected function _construct()
{
$this->stockIds = [];
$this->skuRelations = [];
}

/**
* @param $productSku
* @param $websiteCode
* Get stock status
*
* @param string $productSku
* @param ?string $websiteCode
* @return int
* @throws NoSuchEntityException
*/
public function getStockStatus($productSku, $websiteCode): int
public function getStockStatus(string $productSku, ?string $websiteCode): int
{
if ($this->moduleManager->isEnabled('Magento_Inventory')) {
$stockStatus = $this->getMsiStock($productSku, $websiteCode);
Expand All @@ -80,6 +90,8 @@ public function getStockStatus($productSku, $websiteCode): int
}

/**
* Get Msi stock
*
* @param string $productSku
* @param string $websiteCode
* @return int
Expand All @@ -98,6 +110,8 @@ protected function getMsiStock(string $productSku, string $websiteCode): int
}

/**
* Get stock id
*
* @param string $websiteCode
* @return int|mixed
*/
Expand All @@ -115,6 +129,8 @@ public function getStockId(string $websiteCode)
}

/**
* Relation saving
*
* @param array $entityIds
* @return Inventory
*/
Expand All @@ -139,6 +155,8 @@ public function clearRelation()
}

/**
* Get sku relation
*
* @param int $entityId
* @return string
*/
Expand Down
14 changes: 10 additions & 4 deletions Plugin/Model/Adapter/BatchDataMapper/ProductDataMapperPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use GhoSter\OutOfStockAtLast\Model\ResourceModel\Inventory;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* phpcs:ignore Magento2.Legacy.Copyright.FoundCopyrightMissingOrWrongFormat
* Class ProductDataMapperPlugin for mapping hook
*/
class ProductDataMapperPlugin
{
/**
Expand All @@ -32,11 +36,13 @@ public function __construct(StockDataMapper $stockDataMapper, Inventory $invento
}

/**
* Map more attributes
*
* @param ProductDataMapper $subject
* @param $documents
* @param $documentData
* @param $storeId
* @param $context
* @param mixed $documents
* @param mixed $documentData
* @param mixed $storeId
* @param mixed $context
* @return mixed
* @throws NoSuchEntityException
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use GhoSter\OutOfStockAtLast\Model\ResourceModel\Inventory;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* phpcs:ignore Magento2.Legacy.Copyright.FoundCopyrightMissingOrWrongFormat
* Class AttributeDataPlugin for fulltext datasource mapping
*/
class AttributeDataPlugin
{
/**
Expand All @@ -31,9 +35,11 @@ public function __construct(StockDataMapper $stockDataMapper, Inventory $invento
}

/**
* Add data for datasource
* phpcs:ignore Magento2.Annotation
* @param $subject
* @param array $result
* @param $storeId
* @param mixed $storeId
* @param array $indexData
* @return array
* @throws NoSuchEntityException
Expand Down
78 changes: 67 additions & 11 deletions Plugin/Model/ResourceModel/Product/CollectionPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,106 @@
namespace GhoSter\OutOfStockAtLast\Plugin\Model\ResourceModel\Product;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\Framework\DB\Select;
use Zend_Db_Select;

/**
* phpcs:ignore Magento2.Legacy.Copyright.FoundCopyrightMissingOrWrongFormat
* Class CollectionPlugin
*/
class CollectionPlugin
{
/**
* @var array
*/
private $skipFlags = [];

/**
* Setting order and determine flags
*
* @param Collection $subject
* @param $attribute
* @param mixed $attribute
* @param string $dir
* @return array
*/
public function beforeSetOrder(
Collection $subject,
$attribute,
string $dir = Select::SQL_DESC
string $dir = Zend_Db_Select::SQL_DESC
): array {
$subject->setFlag('is_processed', true);
$subject->setFlag('is_processing', true);
$this->applyOutOfStockAtLastOrders($subject);
$subject->setFlag('is_processed', false);

$flagName = $this->_getFlag($attribute);

if ($subject->getFlag($flagName)) {
$this->skipFlags[] = $flagName;
}

$subject->setFlag('is_processing', false);
return [$attribute, $dir];
}

/**
* Get flag by attribute
*
* @param string $attribute
* @return string
*/
private function _getFlag(string $attribute): string
{
return 'sorted_by_' . $attribute;
}

/**
* Try to determine applied sorting attribute flags
*
* @param Collection $subject
* @param callable $proceed
* @param mixed $attribute
* @param string $dir
* @return Collection
*/
public function aroundSetOrder(
Collection $subject,
callable $proceed,
$attribute,
string $dir = Zend_Db_Select::SQL_DESC
): Collection {
$flagName = $this->_getFlag($attribute);
if (!in_array($flagName, $this->skipFlags)) {
$proceed($attribute, $dir);
}

return $subject;
}

/**
* Apply sort orders
*
* @param Collection $collection
*/
private function applyOutOfStockAtLastOrders(Collection $collection)
{
if (!$collection->getFlag('is_sorted_by_oos')) {
$collection->setFlag('is_sorted_by_oos', true);
$collection->setOrder('out_of_stock_at_last', Select::SQL_DESC);
$collection->setOrder('out_of_stock_at_last', Zend_Db_Select::SQL_DESC);
}
}

/**
* @param $subject
* @param $attribute
* Determine and set order if necessary
*
* @param Collection $subject
* @param mixed $attribute
* @param string $dir
* @return array
*/
public function beforeAddOrder(
$subject,
Collection $subject,
$attribute,
string $dir = Select::SQL_DESC
string $dir = Zend_Db_Select::SQL_DESC
): array {
if (!$subject->getFlag('is_processed')) {
if (!$subject->getFlag('is_processing')) {
$result = $this->beforeSetOrder($subject, $attribute, $dir);
}

Expand Down
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

composer require ghoster/module-outofstockatlast

[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
[![Latest Stable Version](http://poser.pugx.org/ghoster/module-outofstockatlast/v)](https://packagist.org/packages/ghoster/module-outofstockatlast)
[![Total Downloads](http://poser.pugx.org/ghoster/module-outofstockatlast/downloads)](https://packagist.org/packages/ghoster/module-outofstockatlast)
[![Latest Unstable Version](http://poser.pugx.org/ghoster/module-outofstockatlast/v/unstable)](https://packagist.org/packages/ghoster/module-outofstockatlast)
[![License](http://poser.pugx.org/ghoster/module-outofstockatlast/license)](https://packagist.org/packages/ghoster/module-outofstockatlast)
[![PHP Version Require](http://poser.pugx.org/ghoster/module-outofstockatlast/require/php)](https://packagist.org/packages/ghoster/module-outofstockatlast)
[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.me/thinghost)
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/63baac1389b34c8d8b1005a087f8f2ab)](https://www.codacy.com/gh/tuyennn/magento2-outofstockatlast/dashboard?utm_source=github.com&utm_medium=referral&utm_content=tuyennn/magento2-outofstockatlast&utm_campaign=Badge_Grade)
[![Build Status](https://travis-ci.com/tuyennn/magento2-outofstockatlast.svg?branch=master)](https://travis-ci.com/tuyennn/magento2-outofstockatlast)
![Version 1.0.1](https://img.shields.io/badge/Version-1.0.0-green.svg)

---
- [Extension on GitHub](https://github.com/tuyennn/magento2-outofstockatlast)
- [Direct download link](https://github.com/tuyennn/magento2-outofstockatlast/tarball/master)
Expand Down Expand Up @@ -45,3 +47,8 @@

- Reindexing after you enable the module

## Donation

If this project help you reduce time to develop, you can give me a cup of beer :)

[![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.me/thinghost)
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"magento/module-elasticsearch-7": "*"
},
"type": "magento2-module",
"version": "1.0.1",
"version": "1.0.2",
"authors": [
{
"name": "Tuyen Nguyen",
Expand Down
4 changes: 4 additions & 0 deletions registration.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
<?php
/**
* phpcs:ignore Magento2.Legacy.Copyright.FoundCopyrightMissingOrWrongFormat
* Copyright © GhoSter, Inc. All rights reserved.
*/
declare(strict_types=1);

use Magento\Framework\Component\ComponentRegistrar;
Expand Down

0 comments on commit c0d802f

Please sign in to comment.