我使用此查询来获取产品名称和Sku。我想加上";品牌名称";存储在制造商属性中。是否可以扩展此查询以按产品获取制造商名称?
SELECT nametable.value,
nametable.store_id,
m2_catalog_product_entity.sku
FROM `m2_catalog_product_entity_varchar` AS nametable
LEFT JOIN m2_catalog_product_entity
ON nametable.entity_id = m2_catalog_product_entity.entity_id
WHERE nametable.attribute_id = (SELECT attribute_id
FROM `m2_eav_attribute`
WHERE `entity_type_id` = 4 and store_id = 0
AND `attribute_code` LIKE 'name');
我强烈建议利用Magento 2产品对象来检索信息,而不是自己构建查询。
在php类中,您可以使用以下工厂方法检索它:
<?php
public function __construct(
MagentoFrameworkViewElementTemplateContext $context,
MagentoCatalogModelProductFactory $product
) {
$this->product = $product;
parent::__construct($context);
}
public function getProduct($id)
{
$product = $this->product->create()->load($entity_id);
$sku = $product->getSku(); // get SKU
$name = $product->getName(); // get name
$manufacturer = $product->getManufacturer(); // get manufacturer
}
}
或通过对象管理器
$entity_id = "your product id";
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$product = $objectManager->create('MagentoCatalogModelProduct')->load($entity_id);
$sku = $product->getSku(); // get SKU
$name = $product->getName(); // get name
$manufacturer = $product->getManufacturer(); // get manufacturer
要检索您想要的任何属性,您可以使用
$attribute = $product->getData("attribute_code"); // get any attribute