Skip to content

JWK-60 Make product update more detailed #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
* @var \Magento\Framework\App\Cache\TypeListInterface
*/
protected $cacheTypeList;

/**
* @var \Magento\Catalog\Api\ProductAttributeRepositoryInterface
*/
protected $attributeRepository;

/**
* Constructor
Expand All @@ -72,6 +77,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\Config\Storage\WriterInterface $configWriter
* @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
* @param \Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
*/
public function __construct(
\Magento\Framework\App\Helper\Context $context,
Expand All @@ -80,14 +86,16 @@ public function __construct(
\Magento\Directory\Model\RegionFactory $regionFactory,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Config\Storage\WriterInterface $configWriter,
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
\Magento\Catalog\Api\ProductAttributeRepositoryInterface $attributeRepository
) {
$this->scopeConfig = $scopeConfig;
$this->urlBackendBuilder = $urlBackendBuilder;
$this->regionFactory = $regionFactory;
$this->storeManager = $storeManager;
$this->configWriter = $configWriter;
$this->cacheTypeList = $cacheTypeList;
$this->attributeRepository = $attributeRepository;

parent::__construct($context);
}
Expand Down Expand Up @@ -157,6 +165,16 @@ public function getAppKey()
{
return $this->scopeConfig->getValue('walkthechat_settings/general/app_key');
}

/**
* Return additional description attributes from configuration
*
* @return array
*/
public function getAdditionalDescriptionAttributes()
{
return explode(',', $this->scopeConfig->getValue('walkthechat_settings/sync/description'));
}

/**
* Check if integration connected
Expand Down Expand Up @@ -496,4 +514,29 @@ public function setInventorySyncStatus($status)
$this->configWriter->save('walkthechat_settings/sync/inventory', $status);
$this->cacheTypeList->cleanType(\Magento\Framework\App\Cache\Type\Config::TYPE_IDENTIFIER);
}

/**
* Get product additional description
*
* @param $product
*
* @return string
*/
public function getAdditionalDescription($product)
{
$html = '';

foreach ($this->getAdditionalDescriptionAttributes() as $id) {
$attribute = $this->attributeRepository->get($id);

$value = $product->getData($attribute->getAttributeCode());

if ($value) {
$html .= '<h2>' . $attribute->getFrontendLabel() . '</h2>';
$html .= '<p>' . $value . '</p>';
}
}

return $html;
}
}
85 changes: 85 additions & 0 deletions Model/Config/Source/DescriptionAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
/**
* @package Walkthechat\Walkthechat
* @author WalktheChat <info@walkthechat.com>
* @copyright 2021 Walkthechat
* @license See LICENSE_WALKTHECHAT.txt for license details.
*/

namespace Walkthechat\Walkthechat\Model\Config\Source;

/**
* Class DescriptionAttributes
*
* @package Walkthechat\Walkthechat\Model\Config\Source
*/
class DescriptionAttributes implements \Magento\Framework\Option\ArrayInterface
{
/**
* @var \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory
*/
protected $attributeFactory;

/**
* @var \Magento\Eav\Model\Entity\TypeFactory
*/
protected $eavTypeFactory;

/**
* @param \Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory
* @param TypeFactory $typeFactory
*/
public function __construct(
\Magento\Catalog\Model\ResourceModel\Eav\AttributeFactory $attributeFactory,
\Magento\Eav\Model\Entity\TypeFactory $typeFactory
)
{
$this->attributeFactory = $attributeFactory;
$this->eavTypeFactory = $typeFactory;
}

protected function _getAttributes()
{
$entityType = $this->eavTypeFactory->create()->loadByCode('catalog_product');

$collection = $this->attributeFactory->create()->getCollection();
$collection->addFieldToFilter('entity_type_id', $entityType->getId());
$collection->addFieldToFilter('frontend_input', ['in' => ['text', 'textarea']]);
$collection->setOrder('frontend_label', 'ASC');

return $collection;
}

/**
* Options getter
*
* @return array
*/
public function toOptionArray()
{
$attributes = [];

foreach ($this->_getAttributes() as $attribute) {
$attributes[] = [
'value' => $attribute->getAttributeId(),
'label' => $attribute->getFrontendLabel(),
];
}

return $attributes;
}

/**
* @return array
*/
public function toArray()
{
$attributes = [];

foreach ($this->_getAttributes() as $attribute) {
$attributes[$attribute->getAttributeId()] = $attribute->getFrontendLabel();
}

return $attributes;
}
}
10 changes: 6 additions & 4 deletions Model/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,13 @@ public function prepareProductData($product, $isNew = true, array $imagesData =
$data['title'] = [
'en' => $product->getName(),
];

$data['bodyHtml'] = [
'en' => isset($mediaContentData['content']) && $mediaContentData['content'] ? $mediaContentData['content'] : $product->getDescription(),
];
}

$description = isset($mediaContentData['content']) && $mediaContentData['content'] ? $mediaContentData['content'] : $product->getDescription();

$data['bodyHtml'] = [
'en' => $description . $this->helper->getAdditionalDescription($product)
];

if ($product->getTypeId() === \Magento\ConfigurableProduct\Model\Product\Type\Configurable::TYPE_CODE) {
$configurableOptions = $product->getTypeInstance()->getConfigurableOptions($product);
Expand Down
4 changes: 4 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@
<label>Inventory Batch</label>
<source_model>Walkthechat\Walkthechat\Model\Config\Source\QueueBatch</source_model>
</field>
<field id="description" translate="label" type="multiselect" sortOrder="60" showInDefault="1">
<label>Additional Description Attributes</label>
<source_model>Walkthechat\Walkthechat\Model\Config\Source\DescriptionAttributes</source_model>
</field>
</group>
<group id="currency" translate="label" sortOrder="30" showInDefault="1">
<label>Currency</label>
Expand Down