diff --git a/Helper/Data.php b/Helper/Data.php index 2edf0ce..bef5380 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -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 @@ -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, @@ -80,7 +86,8 @@ 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; @@ -88,6 +95,7 @@ public function __construct( $this->storeManager = $storeManager; $this->configWriter = $configWriter; $this->cacheTypeList = $cacheTypeList; + $this->attributeRepository = $attributeRepository; parent::__construct($context); } @@ -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 @@ -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 .= '
' . $value . '
'; + } + } + + return $html; + } } diff --git a/Model/Config/Source/DescriptionAttributes.php b/Model/Config/Source/DescriptionAttributes.php new file mode 100644 index 0000000..4de9069 --- /dev/null +++ b/Model/Config/Source/DescriptionAttributes.php @@ -0,0 +1,85 @@ + + * @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; + } +} diff --git a/Model/ProductService.php b/Model/ProductService.php index 4eee983..abe6ae4 100755 --- a/Model/ProductService.php +++ b/Model/ProductService.php @@ -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); diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 2e23572..affa139 100755 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -60,6 +60,10 @@