Skip to content

Commit

Permalink
Merge pull request #334 from akeneo/release/v101.2.0
Browse files Browse the repository at this point in the history
Release/v101.2.0
  • Loading branch information
Dnd-Gimix authored Jan 18, 2021
2 parents 6c8c98e + 31da0ea commit 708bc59
Show file tree
Hide file tree
Showing 9 changed files with 914 additions and 122 deletions.
165 changes: 165 additions & 0 deletions Block/Adminhtml/System/Config/Form/Field/Grouped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

declare(strict_types=1);

namespace Akeneo\Connector\Block\Adminhtml\System\Config\Form\Field;

use Akeneo\Connector\Helper\Authenticator;
use Akeneo\Connector\Helper\Config as ConfigHelper;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Akeneo\Pim\ApiClient\Pagination\ResourceCursorInterface;
use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Data\Form\Element\Factory as ElementFactory;
use Psr\Log\LoggerInterface as Logger;

/**
* Class Grouped
*
* @package Akeneo\Connector\Block\Adminhtml\System\Config\Form\Field
* @author Agence Dn'D <[email protected]>
* @copyright 2004-present Agence Dn'D
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* @link https://www.dnd.fr/
*/
class Grouped extends AbstractFieldArray
{
/**
* Description AKENEO_GROUPED_FAMILY_CODE constant
*
* @var string AKENEO_GROUPED_FAMILY_CODE
*/
const AKENEO_GROUPED_FAMILY_CODE = 'akeneo_grouped_family_code';
/**
* This variable contains a mixed value
*
* @var Authenticator $akeneoAuthenticator
*/
protected $akeneoAuthenticator;
/**
* This variable contains a Logger
*
* @var Logger $logger
*/
protected $logger;
/**
* This variable contains a ConfigHelper
*
* @var ConfigHelper $configHelper
*/
protected $configHelper;
/**
* This variable contains an ElementFactory
*
* @var ElementFactory $elementFactory
*/
protected $elementFactory;

/**
* Grouped constructor
*
* @param Authenticator $akeneoAuthenticator
* @param Logger $logger
* @param ConfigHelper $configHelper
* @param Context $context
* @param ElementFactory $elementFactory
* @param array $data
*/
public function __construct(
Authenticator $akeneoAuthenticator,
Logger $logger,
ConfigHelper $configHelper,
Context $context,
ElementFactory $elementFactory,
array $data = []
) {
parent::__construct($context, $data);

$this->akeneoAuthenticator = $akeneoAuthenticator;
$this->logger = $logger;
$this->configHelper = $configHelper;
$this->elementFactory = $elementFactory;
}

/**
* Initialise form fields
*
* @return void
*/
protected function _construct()
{
$this->addColumn(self::AKENEO_GROUPED_FAMILY_CODE, ['label' => __('Grouped product family code')]);
$this->addColumn('akeneo_quantity_association', ['label' => __('Quantity association code')]);
$this->_addAfter = false;
$this->_addButtonLabel = __('Add');

parent::_construct();
}

/**
* Render array cell for prototypeJS template
*
* @param string $columnName
*
* @return string
*/
public function renderCellTemplate($columnName)
{
if ($columnName != self::AKENEO_GROUPED_FAMILY_CODE || !isset($this->_columns[$columnName])) {
return parent::renderCellTemplate($columnName);
}

/** @var array $options */
$options = $this->getFamilies();
/** @var AbstractElement $element */
$element = $this->elementFactory->create('select');
$element->setForm(
$this->getForm()
)->setName(
$this->_getCellInputElementName($columnName)
)->setHtmlId(
$this->_getCellInputElementId('<%- _id %>', $columnName)
)->setValues(
$options
);

return str_replace("\n", '', $element->getElementHtml());
}

/**
* Get Families
*
* @return ResourceCursorInterface|array
*/
public function getFamilies()
{
/** @var array $families */
$families = [];

try {
/** @var AkeneoPimClientInterface $client */
$client = $this->akeneoAuthenticator->getAkeneoApiClient();

if (empty($client)) {
return $families;
}

/** @var string|int $paginationSize */
$paginationSize = $this->configHelper->getPaginationSize();
/** @var ResourceCursorInterface $akeneoFamilies */
$akeneoFamilies = $client->getFamilyApi()->all($paginationSize);
/** @var mixed[] $family */
foreach ($akeneoFamilies as $family) {
if (!isset($family['code'])) {
continue;
}
$families[$family['code']] = $family['code'];
}
} catch (\Exception $exception) {
$this->logger->warning($exception->getMessage());
}

return $families;
}
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,7 @@

### Version 101.1.2 :
* Fix image attribute import to fill the catalog_product_entity_media_gallery_value table

### Version 101.2.0 :
* Add grouped product management with quantity association (https://help.akeneo.com/magento2-connector/v100/articles/12-configure-grouped-products.md)
* Add product association mapping in connector configuration (https://help.akeneo.com/magento2-connector/v100/articles/05-configure-products.html#configure-related-upsell-and-cross-sell-products)
134 changes: 122 additions & 12 deletions Helper/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Akeneo\Connector\Helper;

use Magento\Catalog\Model\Product\Link;
use Akeneo\Connector\Model\Source\Edition;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\App\Helper\Context;
Expand Down Expand Up @@ -313,27 +314,39 @@ class Config
*/
const ATTRIBUTE_FILTERS_BY_CODE = 'akeneo_connector/filter_attribute/filter_attribute_code';
/**
* Product activation flag config path
* Akeneo master of staging content flag config path
*
* @var string PRODUCT_ACTIVATION
* @var string PRODUCT_ASSOCIATION_RELATED
*/
const PRODUCT_ACTIVATION = 'akeneo_connector/product/activation';
const PRODUCT_ASSOCIATION_RELATED = 'akeneo_connector/product/association_related';
/**
* @var int PAGINATION_SIZE_DEFAULT_VALUE
* Akeneo master of staging content flag config path
*
* @var string PRODUCT_ASSOCIATION_UPSELL
*/
const PAGINATION_SIZE_DEFAULT_VALUE = 10;
const PRODUCT_ASSOCIATION_UPSELL = 'akeneo_connector/product/association_upsell';
/**
* @var int PRODUCT_PRODUCT_MODEL_BATCH_SIZE_DEFAULT_VALUE
* Akeneo master of staging content flag config path
*
* @var string PRODUCT_AKENEO_MASTER
*/
const PRODUCT_PRODUCT_MODEL_BATCH_SIZE_DEFAULT_VALUE = 500;
const PRODUCT_ASSOCIATION_CROSSELL = 'akeneo_connector/product/association_crossell';
/**
* @var int PRODUCT_PRODUCT_MODEL_LENGTH_DEFAULT_VALUE
* Product activation flag config path
*
* @var string PRODUCT_ACTIVATION
*/
const PRODUCT_PRODUCT_MODEL_UPDATE_LENGTH_DEFAULT_VALUE = 5000;
const PRODUCT_ACTIVATION = 'akeneo_connector/product/activation';
/**
* @var int PRODUCT_PRODUCT_MODEL_UPDATE_LENGTH_MINIMUM
* Grouped product families mapping path
*
* @var string GROUPED_PRODUCTS_FAMILIES_MAPPING
*/
const PRODUCT_PRODUCT_MODEL_UPDATE_LENGTH_MINIMUM = 1000;
const GROUPED_PRODUCTS_FAMILIES_MAPPING = 'akeneo_connector/grouped_products/families_mapping';
/**
* @var int PAGINATION_SIZE_DEFAULT_VALUE
*/
const PAGINATION_SIZE_DEFAULT_VALUE = 10;
/**
* Families filters updated mode config path
*
Expand Down Expand Up @@ -523,7 +536,7 @@ public function isSerenityEdition()
*/
public function getEdition()
{
return $this->scopeConfig->getValue(self::AKENEO_API_EDITION);
return $this->scopeConfig->getValue(self::AKENEO_API_EDITION);
}

/**
Expand Down Expand Up @@ -1319,6 +1332,66 @@ public function getAttributeMapping()
return $loweredMatches;
}

/**
* Get all grouped families from the mapping
*
* @return string[]
*/
public function getGroupedFamiliesToImport()
{
/** @var string $familiesSerialized */
$familiesSerialized = $this->scopeConfig->getValue(self::GROUPED_PRODUCTS_FAMILIES_MAPPING);
/** @var mixed[] $associations */
$associations = $this->serializer->unserialize($familiesSerialized);
/** @var string[] $families */
$families = [];
/** @var mixed[] $association */
foreach ($associations as $association) {
$families[] = $association['akeneo_grouped_family_code'];
}

return $families;
}

/**
* Get all families and their associations to import
*
* @return string[]
*/
public function getGroupedAssociationsToImport()
{
/** @var string $associationsSerialized */
$associationsSerialized = $this->scopeConfig->getValue(self::GROUPED_PRODUCTS_FAMILIES_MAPPING);
/** @var string[] $associations */
$associations = $this->serializer->unserialize($associationsSerialized);

return $associations;
}

/**
* Description getGroupedAssociationsForFamily function
*
* @param string $family
*
* @return mixed[]
*/
public function getGroupedAssociationsForFamily(string $family)
{
/** @var string[] $allAssociations */
$allAssociations = $this->getGroupedAssociationsToImport();
/** @var mixed[] $associations */
$associations = [];

/** @var string[] $association */
foreach ($allAssociations as $association) {
if ($association['akeneo_grouped_family_code'] === $family) {
$associations[] = $association;
}
}

return $associations;
}

/**
* Returns default attribute-set id for given entity
*
Expand Down Expand Up @@ -1351,4 +1424,41 @@ public function getFamiliesUpdatedGreater()
{
return $this->scopeConfig->getValue(self::FAMILIES_FILTERS_UPDATED_GREATER);
}

/**
* Get association types configuration array for product import
*
* @return string[]
*/
public function getAssociationTypes()
{
/** @var string $relatedCode */
$relatedCode = $this->scopeConfig->getValue(self::PRODUCT_ASSOCIATION_RELATED);
/** @var string $upsellCode */
$upsellCode = $this->scopeConfig->getValue(self::PRODUCT_ASSOCIATION_UPSELL);
/** @var string $crossellCode */
$crossellCode = $this->scopeConfig->getValue(self::PRODUCT_ASSOCIATION_CROSSELL);
/** @var string[] $associationTypes */
$associationTypes = [];
if ($relatedCode) {
$associationTypes[Link::LINK_TYPE_RELATED] = [
$relatedCode . '-products',
$relatedCode . '-product_models',
];
}
if ($upsellCode) {
$associationTypes[Link::LINK_TYPE_UPSELL] = [
$upsellCode . '-products',
$upsellCode . '-product_models',
];
}
if ($crossellCode) {
$associationTypes[Link::LINK_TYPE_CROSSSELL] = [
$crossellCode . '-products',
$crossellCode . '-product_models',
];
}

return $associationTypes;
}
}
Loading

0 comments on commit 708bc59

Please sign in to comment.