-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from nordcomputer/develop
Develop
- Loading branch information
Showing
7 changed files
with
101 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,37 @@ | ||
<?php | ||
|
||
namespace Nordcomputer\Stockfilter\Cron; | ||
|
||
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; | ||
use Magento\CatalogInventory\Api\StockRegistryInterface; | ||
use Magento\CatalogInventory\Api\Data\StockItemInterface; | ||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Framework\App\ResourceConnection; | ||
|
||
class UpdateStockFilter | ||
{ | ||
protected $CollectionFactory; | ||
|
||
private $stockRegistry; | ||
|
||
/** | ||
* @param ScopeConfigInterface $scopeConfig | ||
* @param ResourceConnection $resourceConnection | ||
*/ | ||
public function __construct( | ||
CollectionFactory $CollectionFactory, | ||
StockRegistryInterface $stockRegistry, | ||
ScopeConfigInterface $scopeConfig | ||
ScopeConfigInterface $scopeConfig, | ||
ResourceConnection $resourceConnection | ||
) { | ||
$this->CollectionFactory = $CollectionFactory; | ||
$this->stockRegistry = $stockRegistry; | ||
$this->scopeConfig = $scopeConfig; | ||
$this->resourceConnection = $resourceConnection; | ||
} | ||
/** | ||
* Executes Cronjob for updating 'stock_filter' parameter | ||
*/ | ||
public function execute() | ||
{ | ||
if ($this->scopeConfig->getValue('cataloginventory/cronjobs/is_enabled')==1) { | ||
$collection = $this->CollectionFactory->create() | ||
->addAttributeToSelect('*') | ||
->addAttributeToFilter('type_id', \Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) | ||
->load(); | ||
foreach ($collection as $product) { | ||
$oldfilterstock=$product->getFilterStock(); | ||
$product->setFilterStock(''); | ||
if ($this->getStockStatus($product->getId())==true) { | ||
$product->setFilterStock(1); | ||
} | ||
if ($oldfilterstock!=$product->getFilterStock()) { | ||
$product->save(); | ||
} | ||
} | ||
$connection = $this->resourceConnection->getConnection(); | ||
$table = $connection->getTableName('catalog_product_entity_int'); | ||
// Update query | ||
$query = "UPDATE " . $table . " t | ||
JOIN cataloginventory_stock_status a ON a.product_id = t.entity_id | ||
JOIN eav_attribute ap ON ap.attribute_id = t.attribute_id | ||
SET value = stock_status WHERE attribute_code = 'filter_stock'"; | ||
$connection->query($query); | ||
} | ||
return $this; | ||
} | ||
|
||
public function getStockStatus($productId) | ||
{ | ||
/** @var StockItemInterface $stockItem */ | ||
$stockItem = $this->stockRegistry->getStockItem($productId); | ||
$isInStock = $stockItem ? $stockItem->getIsInStock() : false; | ||
return $isInStock; | ||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
namespace Nordcomputer\Stockfilter\Setup; | ||
|
||
use Magento\Eav\Setup\EavSetup; | ||
use Magento\Eav\Setup\EavSetupFactory; | ||
use Magento\Framework\Setup\InstallDataInterface; | ||
use Magento\Framework\Setup\ModuleContextInterface; | ||
use Magento\Framework\Setup\ModuleDataSetupInterface; | ||
use Magento\Framework\Setup\SchemaSetupInterface; | ||
use Magento\Framework\Setup\UninstallInterface; | ||
|
||
class Uninstall implements UninstallInterface | ||
{ | ||
private $eavSetupFactory; | ||
|
||
public function __construct(EavSetupFactory $eavSetupFactory) | ||
{ | ||
$this->eavSetupFactory = $eavSetupFactory; | ||
} | ||
|
||
public function uninstall(SchemaSetupInterface $setup, ModuleContextInterface $context) | ||
{ | ||
$setup->startSetup(); | ||
|
||
$eavSetup = $this->eavSetupFactory->create(); | ||
|
||
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'filter_stock'); | ||
|
||
$setup->endSetup(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?xml version="1.0" ?> | ||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> | ||
<module name="Nordcomputer_Stockfilter" setup_version="1.0.3"/> | ||
<module name="Nordcomputer_Stockfilter" setup_version="1.1"/> | ||
</config> |