Skip to content

Commit

Permalink
Generate product mapping by attribute data from magento instead of dy…
Browse files Browse the repository at this point in the history
…namic generating DivanteLtd#7
  • Loading branch information
VladimirPlastovets committed Aug 28, 2018
1 parent 68e6763 commit 4d89f04
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 178 deletions.
Original file line number Diff line number Diff line change
@@ -1,38 +1,128 @@
<?php
require_once('AbstractController.php');

class Divante_VueStorefrontBridge_AttributesController extends Divante_VueStorefrontBridge_AbstractController
{
const ES_DATA_TYPE_STRING = 'text';
const ES_DATA_TYPE_FLOAT = 'float';
const ES_DATA_TYPE_INT = 'integer';
const ES_DATA_TYPE_DATE = 'date';
const ES_DATA_TYPE_OBJECT = 'object';
const ES_DATA_TYPE_KEYWORD = 'keyword';
const ES_DATA_TYPE_BOOLEAN = 'boolean';

/**
* elasticsearch <=> magento type mappings
* @var array
*/
protected $_mapAttributeType = array(
'varchar' => self::ES_DATA_TYPE_STRING,
'text' => self::ES_DATA_TYPE_STRING,
'decimal' => self::ES_DATA_TYPE_FLOAT,
'int' => self::ES_DATA_TYPE_INT,
'smallint' => self::ES_DATA_TYPE_INT,
'timestamp' => self::ES_DATA_TYPE_DATE,
'datetime' => self::ES_DATA_TYPE_DATE,
'static' => self::ES_DATA_TYPE_STRING,
);

public function indexAction()
{
if($this->_authorize($this->getRequest())) {

$params = $this->_processParams($this->getRequest());
if ($this->_authorize($this->getRequest())) {
$attrList = array();
$params = $this->_processParams($this->getRequest());
$productAttrs = Mage::getResourceModel('catalog/product_attribute_collection');

$attrList = array();
foreach ($productAttrs as $productAttr) {
$options = array();
$productAttrDTO = $productAttr->getData();
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */
$attribute = Mage::getSingleton('eav/config')
->getAttribute(Mage_Catalog_Model_Product::ENTITY, $productAttr->getAttributeCode());
->getAttribute(
Mage_Catalog_Model_Product::ENTITY,
$productAttr->getAttributeCode()
);

$options = array();
if ($attribute->usesSource()) {
$options = $attribute->getSource()->getAllOptions(false);
}

$productAttrDTO = $productAttr->getData();
if (in_array($productAttrDTO['source_model'], array('core/design_source_design'))) {
continue;
} // exception - this attribute has string typed values; this is not acceptable by VS

if (in_array($productAttrDTO['source_model'], array('core/design_source_design'))) continue; // exception - this attribute has string typed values; this is not acceptable by VS
$productAttrDTO['id'] = intval($productAttr->getAttributeId());
$productAttrDTO['options'] = $options;
$productAttrDTO['default_value'] = (string)$productAttrDTO['default_value'];
$productAttrDTO = $this->_filterDTO($productAttrDTO);
$attrList[] = $productAttrDTO;
}
$this->_result(200, $attrList);
}
}

$productAttrDTO['id'] = intval($productAttr->attribute_id);
$productAttrDTO['options'] = $options;
/**
* Prepare attribute data for mappings
*/
public function attributeInfoAction()
{
if ($this->_authorize($this->getRequest())) {
$result = array();
$attributeMapping = array();
$productAttributes = Mage::getResourceModel('catalog/product_attribute_collection');
foreach ($productAttributes as $productAttribute) {
/** @var Mage_Catalog_Model_Resource_Eav_Attribute $productAttr */
$attribute = Mage::getSingleton('eav/config')
->getAttribute(
Mage_Catalog_Model_Product::ENTITY,
$productAttribute->getAttributeCode()
);

$productAttrDTO = $this->_filterDTO($productAttrDTO);
$backendType = $attribute->getBackendType();
$attributeCode = $productAttribute->getAttributeCode();
if (strstr($attributeCode, 'is_') || strstr($attributeCode, 'has_')) {
$fieldType = self::ES_DATA_TYPE_BOOLEAN;
} else {
$fieldType = $this->_getElasticsearchTypeByMagentoType($backendType);
}

$attributeMapping[$attributeCode]['type'] = $fieldType;
if ($backendType == 'timestamp' || $backendType == 'datetime') {
$attributeMapping[$attributeCode]['format'] = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis";
}

$attrList[] = $productAttrDTO;
if (($attribute->getUsedForSortBy()
|| $attribute->getIsFilterableInSearch()
|| $attribute->getIsFilterable())
&& $fieldType == self::ES_DATA_TYPE_STRING
) {
$attributeMapping[$attributeCode]['fielddata'] = true;
}
}
$this->_result(200, $attrList);
$attributeMapping['final_price']['type'] = self::ES_DATA_TYPE_FLOAT;
$attributeMapping['configurable_children']['properties'] = $attributeMapping;

unset($attributeMapping['created_at']);
unset($attributeMapping['updated_at']);

$result['properties'] = $attributeMapping;
$this->_result(200, $result);
}
}

/**
* Elasticsearch <=> magento type mapping
*
* @param $magentoType
*
* @return mixed
*/
protected function _getElasticsearchTypeByMagentoType($magentoType)
{
if (!isset($this->_mapAttributeType[$magentoType])) {
return $magentoType;
}

return $this->_mapAttributeType[$magentoType];
}
}
?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class Divante_VueStorefrontBridge_ProductsController extends Divante_VueStorefro
public function indexAction()
{
if($this->_authorize($this->getRequest())) {

$params = $this->_processParams($this->getRequest());
$confChildBlacklist = array('entity_id', 'id', 'type_id', 'updated_at', 'created_at', 'stock_item', 'short_description', 'page_layout', 'news_from_date', 'news_to_date', 'meta_description', 'meta_keyword', 'meta_title', 'description', 'attribute_set_id', 'entity_type_id', 'has_options', 'required_options');

Expand All @@ -29,9 +28,7 @@ public function indexAction()
unset($productDTO['stock_item']);

$productDTO['stock'] = $stock->getData();
if (isset($productDTO['stock']['is_in_stock']) && $productDTO['stock']['is_in_stock'] == 1) {
$productDTO['stock']['is_in_stock'] = true;
}
$productDTO['final_price'] = $product->getFinalPrice();
$productDTO['media_gallery'] = $product->getMediaGalleryImages();
if ($productDTO['type_id'] !== 'simple') {
$configurable = Mage::getModel('catalog/product_type_configurable')->setProduct($product);
Expand Down Expand Up @@ -80,4 +77,3 @@ public function indexAction()
}
}
}
?>
3 changes: 2 additions & 1 deletion node-app/config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"product_endpoint": "http://dockerized-magento.local/vsbridge/products/index",
"category_endpoint": "http://dockerized-magento.local/vsbridge/categories/index",
"taxrule_endpoint": "http://dockerized-magento.local/vsbridge/taxrules/index",
"attribute_endpoint": "http://dockerized-magento.local/vsbridge/attributes/index"
"attribute_endpoint": "http://dockerized-magento.local/vsbridge/attributes/index",
"attribute_data_endpoint": "http://dockerized-magento.local/vsbridge/attributes/attributeInfo"
}
}
Loading

0 comments on commit 4d89f04

Please sign in to comment.