Skip to content

Commit

Permalink
Merge pull request #2 from nordcomputer/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
nordcomputer committed Jul 23, 2021
2 parents 2a8d994 + 0f47904 commit 260fea0
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 84 deletions.
56 changes: 56 additions & 0 deletions Cron/UpdateStockFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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;

class UpdateStockFilter
{
protected $CollectionFactory;

private $stockRegistry;

public function __construct(
CollectionFactory $CollectionFactory,
StockRegistryInterface $stockRegistry,
ScopeConfigInterface $scopeConfig
) {
$this->CollectionFactory = $CollectionFactory;
$this->stockRegistry = $stockRegistry;
$this->scopeConfig = $scopeConfig;
}
public function execute()
{
if ($this->scopeConfig->getValue('cataloginventory/cronjobs/is_enabled')==1) {
$collection = $this->CollectionFactory->create()
->addAttributeToSelect('*')
->load();
foreach ($collection as $product) {
if ($this->getStockStatus($product->getId())==true) {
if ($product->getFilterStock()=='' || $product->getFilterStock()==null) {
$product->setFilterStock(1);
$product->save();
}
} else {
if ($product->getFilterStock()==1) {
$product->setFilterStock('');
$product->save();
}
}

}
}
return $this;
}

public function getStockStatus($productId)
{
/** @var StockItemInterface $stockItem */
$stockItem = $this->stockRegistry->getStockItem($productId);
$isInStock = $stockItem ? $stockItem->getIsInStock() : false;
return $isInStock;
}
}
20 changes: 20 additions & 0 deletions Model/Config/Source/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Nordcomputer\Stockfilter\Model\Config\Source;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{
/**
* Get all options
*
* @return array
*/
public function getAllOptions()
{
$this->_options = [
['label' => __('No'), 'value'=> '' ],
['label' => __('Yes'), 'value'=> 1]
];
return $this->_options;
}
}
54 changes: 0 additions & 54 deletions Observer/SetStockFilter.php

This file was deleted.

23 changes: 4 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
# Mage2 Module Nordcomputer Stockfilter
# Magento 2 Module Nordcomputer Stockfilter

``nordcomputer/module-stockfilter``

- [Main Functionalities](#markdown-header-main-functionalities)
- [Installation](#markdown-header-installation)
- [Configuration](#markdown-header-configuration)
- [Specifications](#markdown-header-specifications)
- [Attributes](#markdown-header-attributes)


## Main Functionalities
Expand All @@ -24,11 +22,7 @@ Enabels filtering by stock status

### Type 2: Composer

- Make the module available in a composer repository for example:
- private repository `repo.magento.com`
- public repository `packagist.org`
- public github repository as vcs
- Add the composer repository to the configuration by running `composer config repositories.repo.magento.com composer https://repo.magento.com/`
- Add the composer repository to the configuration by running `composer config repositories.nordcomputer/module-stockfilter git "[email protected]:nordcomputer/magento2-stockfilter.git"`
- Install the module composer by running `composer require nordcomputer/module-stockfilter`
- enable the module by running `php bin/magento module:enable Nordcomputer_Stockfilter`
- apply database updates by running `php bin/magento setup:upgrade`\*
Expand All @@ -37,15 +31,6 @@ Enabels filtering by stock status

## Configuration

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



## Specifications




## Attributes



The Cronjob iterates over all simple products and sets the newly created attribute "filter-stock" according to the stock status of the product.
10 changes: 6 additions & 4 deletions Setup/InstallData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,19 @@ public function __construct(EavSetupFactory $eavSetupFactory)
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
$eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY, 'filter_stock');

$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'filter_stock',
[
'type' => 'text',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Stock Filter',
'input' => 'boolean',
'label' => 'In Stock',
'input' => 'select',
'class' => '',
'source' => \Magento\Eav\Model\Entity\Attribute\Source\Boolean::class,
'source' => \Nordcomputer\Stockfilter\Model\Config\Source\Options::class,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
'visible' => true,
'required' => false,
Expand Down
9 changes: 9 additions & 0 deletions etc/acl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Acl/etc/acl.xsd">
<acl>
<resources>
<resource id="Magento_Backend::admin">
<resource id="Nordcomputer_Stockfilter::main" translate="title" title="Stock Filter" sortOrder="50"/>
</resource>
</resources>
</acl>
</config>
20 changes: 20 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
<section id="cataloginventory" translate="label tooltip comment" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
<group id="cronjobs" translate="label tooltip comment" type="text" sortOrder="999999" showInDefault="1" showInWebsite="1" showInStore="1">
<resource>Nordcomputer_Stockfilter::main</resource>
<label>Stock Filter Cronjob Configuration</label>
<field id="is_enabled" translate="label" type="select" sortOrder="2" showInDefault="1" canRestore="1">
<label>Enable Cronjob for stock-filter</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
</field>
<field id="cronjobupdate" translate="label tooltip comment" type="text" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1" canRestore="1">
<label>Cronjob for updating stock-filter</label>
<comment>Set time for cronjob (for example: * * * * * to run every minute)</comment>
</field>
</group>
</section>

</system>
</config>
11 changes: 11 additions & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
<default>
<cataloginventory>
<cronjobs>
<is_enabled>1</is_enabled>
<cronjobupdate>/30 * * * *</cronjobupdate>
</cronjobs>
</cataloginventory>
</default>
</config>
12 changes: 12 additions & 0 deletions etc/cron_groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/cron_groups.xsd">
<group id="filterstock">
<schedule_generate_every>1</schedule_generate_every>
<schedule_ahead_for>4</schedule_ahead_for>
<schedule_lifetime>2</schedule_lifetime>
<history_cleanup_every>10</history_cleanup_every>
<history_success_lifetime>60</history_success_lifetime>
<history_failure_lifetime>600</history_failure_lifetime>
<use_separate_process>1</use_separate_process>
</group>
</config>
8 changes: 8 additions & 0 deletions etc/crontab.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="filterstock">
<job name="updatestockfilter" instance="Nordcomputer\Stockfilter\Cron\UpdateStockFilter" method="execute">
<config_path>cataloginventory/cronjobs/cronjobupdate</config_path>
</job>
</group>
</config>
6 changes: 0 additions & 6 deletions etc/events.xml

This file was deleted.

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="0.0.1"/>
<module name="Nordcomputer_Stockfilter" setup_version="0.0.2"/>
</config>
9 changes: 9 additions & 0 deletions i18n/de_DE.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"Nordcomputer Stock-Filter","Nordcomputer Lagerbestandsfilter"
"Stockfilter","Lagerbestandsfilter"
"Stock Filter Cronjob Configuration","Lagerbestandsfilter Cronjob Konfiguration"
"Cronjob for updating stock-filter","Cronjob, um den Lagerbestandsfilter zu updaten"
"stock","Lagerbestände"
"Set time for cronjob (for example: * * * * * to run every minute)","Zeit(en) setzen, wann der Cronjob ausgeführt werden soll. Zum Beispiel * * * * * um jede Minute zu laufen"
"In stock (Filternavigation)","Auf Lager (Filternavigation)"
"In stock","Auf Lager"
"Enable Cronjob for stock-filter","Cronjob für Lagerbestandsfilter aktivieren"

0 comments on commit 260fea0

Please sign in to comment.