Skip to content

Commit

Permalink
Merge pull request #10 from nordcomputer/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nordcomputer committed Sep 9, 2021
2 parents b75ab0c + 269b28e commit 5b517ec
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 40 deletions.
56 changes: 20 additions & 36 deletions Cron/UpdateStockFilter.php
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;
}
}
2 changes: 1 addition & 1 deletion Model/Config/Source/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
public function getAllOptions()
{
$this->_options = [
['label' => __('No'), 'value'=> '' ],
['label' => __('No'), 'value'=> ''],
['label' => __('Yes'), 'value'=> 1]
];
return $this->_options;
Expand Down
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,35 @@ Enabels filtering by stock status
- apply database updates by running `php bin/magento setup:upgrade`\*
- Flush the cache by running `php bin/magento cache:flush`

## Steps after installation (IMPORTANT)

- Since this extension does not set the attribute for existing products (the value stays empty after installation), you need to set the attribute to `Yes` for every simple product (you can do so by mass-action).
- Also every new created product needs to be set to `Yes` (Default value) for at least one time, otherwise, the attribute for that specific product does not get created.

Once the attribute was set to `Yes` at least one time, the cronjob does his thing and sets the value automatically with every run.

## Configuration

You can find the configuration for this extension in `Stores -> Configuration -> Catalog -> Inventory -> Stock Filter Cronjob Configuration`

The Cronjob iterates over all simple products and sets the newly created attribute "filter-stock" according to the stock status of the product.


## Uninstalling

As this extension creates an attribute, the attribute needs to be removed when the extension get uninstalled.

# Method 1 (installed via composer)
You can uninstall this extension by running 'bin/magento module:uninstall Nordcomputer_Stockfilter --remove-data'

# Method 2 (installed via Download)
- delete the `Stockfilter` directory in `/app/code/Nordcomputer`
- delete `filter_stock` from the `eav_attribute` table in your database
- run following commands:

```bin/magento setup:upgrade
bin/magento setup:di:compile
bin/magento setup:static-content:deploy -f
bin/magento indexer:reindex
bin/magento c:f
```
12 changes: 11 additions & 1 deletion Setup/InstallData.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,22 @@
class InstallData implements InstallDataInterface
{
private $eavSetupFactory;
/**
*
* @param Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
*/

public function __construct(EavSetupFactory $eavSetupFactory)
{
$this->eavSetupFactory = $eavSetupFactory;
}

/**
* Installs filter_stock attribute
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
Expand All @@ -35,7 +45,7 @@ public function install(ModuleDataSetupInterface $setup, ModuleContextInterface
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'default' => '',
'default' => 1,
'searchable' => false,
'filterable' => true,
'comparable' => false,
Expand Down
31 changes: 31 additions & 0 deletions Setup/Uninstall.php
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();
}
}
12 changes: 11 additions & 1 deletion Setup/UpgradeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,22 @@

class UpgradeData implements UpgradeDataInterface
{
/**
*
* @param Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
*/
public function __construct(
EavSetup $eavSetupFactory
) {
$this->eavSetupFactory = $eavSetupFactory;
}

/**
* Upgrades filter_stock attribute
*
* @param ModuleDataSetupInterface $setup
* @param ModuleContextInterface $context
*/
public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$setup->startSetup();
Expand All @@ -33,7 +43,7 @@ public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
'default' => '',
'default' => 1,
'searchable' => false,
'filterable' => true,
'comparable' => false,
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
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>

0 comments on commit 5b517ec

Please sign in to comment.