Skip to content

Commit

Permalink
Option to enable all products from import (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmeijer97 authored Jul 21, 2021
1 parent 552bc13 commit f5e57ee
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 0 deletions.
108 changes: 108 additions & 0 deletions Plugin/SetProductsActive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php

namespace JustBetter\AkeneoBundle\Plugin;

use Akeneo\Connector\Job\Product;
use Akeneo\Connector\Helper\Import\Entities;
use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Store\Model\ScopeInterface as Scope;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Catalog\Model\Product\Attribute\Source\Status;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\Catalog\Model\Product as CatalogProduct;

class SetProductsActive
{
protected $config;
protected $entitiesHelper;

/**
* @param ScopeConfigInterface $config
* @param Entities $entitiesHelper
*/
public function __construct(
ScopeConfigInterface $config,
Entities $entitiesHelper,
ProductRepositoryInterface $productRepository
) {
$this->config = $config;
$this->entitiesHelper = $entitiesHelper;
$this->productRepository = $productRepository;
$this->connection = $this->entitiesHelper->getConnection();
}

/**
* afterInsertData function
* @param Product $subject
* @param bool $result
* @return bool $result
*/
public function afterInsertData(Product $subject, $result)
{
$extensionEnabled = $this->config->getValue('akeneo_connector/justbetter/setproductsactive', Scope::SCOPE_WEBSITE);
if (!$extensionEnabled) {
return $result;
}

$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());
$products = $this->getProducts($subject);

foreach ($products as $product) {
try {
$this->update($product);
} catch (CouldNotSaveException $e) {
// TODO: possible slack message?
} catch (NoSuchEntityException $e) {
}
}

return $result;
}

/**
* Get the products from the temp table with a join to get the _entity_id
*
* @param Product $subject
* @return array
*/
protected function getProducts(Product $subject): array
{
$tmpTableName = $this->entitiesHelper->getTableName($subject->getCode());
$query = $this->connection->select()->from(['t' => $tmpTableName])->joinInner(
['c' => 'catalog_product_entity'],
't.identifier = c.sku'
);
return $this->connection->fetchAll($query);
}

/**
* @param $identifier
* @return CatalogProduct
* @throws NoSuchEntityException
*/
protected function getProduct(string $identifier): CatalogProduct
{
return $this->productRepository->get($identifier);
}

/**
* update the stock information
*
* @param $product
* @return void
* @throws CouldNotSaveException|NoSuchEntityException
*/
protected function update(array $product): void
{
$product = $this->getProduct($product['identifier']);

if (!$product) {
return;
}

$product->setStatus(Status::STATUS_ENABLED);
$product = $this->productRepository->save($product);
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The following features are included in the JustBetter Akeneo Bundle extension:
| ----------------------------------------------------- | --------------------------------------------------------------------------------------------- |
| FixProductUrls | Replaces variant product urls with the original url + sku for unique URL’s. |
| InsertNewProducts | Adds the possibility to disable insertion of new products from akeneo. |
| SetProductsActive | Adds the possibility to enable all products from akeneo. |
| CategoryExist | Adds the possibility to skip inserting url paths when the category already exist. |
| SlackNotificationCommand | Adds the possibility to receive slack notifications about akeneo imports. |
| MailNotificationCommand | Adds the possibility to receive e-mail notifications about akeneo imports. |
Expand Down
5 changes: 5 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Map Akeneo tax class codes to Magento tax class (Default: no)</comment>
</field>
<field id="setproductsactive" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Set products active</label>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<comment>Set all products from import to active. (Default: no)</comment>
</field>
<group id="slack" translate="label" type="text" sortOrder="7" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Slack Akeneo import notifications</label>
<field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<categoryexist>0</categoryexist>
<akeneomanager>0</akeneomanager>
<insertnewproducts>1</insertnewproducts>
<setproductsactive>0</setproductsactive>
<settaxclass>0</settaxclass>
<slack>
<enable>0</enable>
Expand Down
1 change: 1 addition & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<plugin name="JustBetter_TierPrices" type="JustBetter\AkeneoBundle\Plugin\SetTierPrices" />
<plugin name="JustBetter_InsertNewProducts" type="JustBetter\AkeneoBundle\Plugin\InsertNewProducts" />
<plugin name="JustBetter_setTaxClass" type="JustBetter\AkeneoBundle\Plugin\SetTaxClassId" />
<plugin name="JustBetter_setProductsActive" type="JustBetter\AkeneoBundle\Plugin\SetProductsActive" />
</type>

<type name="Akeneo\Connector\Job\Category">
Expand Down

0 comments on commit f5e57ee

Please sign in to comment.