我使用此查詢來獲取產品名稱和 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');
uj5u.com熱心網友回復:
我強烈建議利用 Magento 2 產品物件來檢索資訊,而不是自己構建查詢。
在 php 類中,您可以使用工廠方法像這樣檢索它:
<?php
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Catalog\Model\ProductFactory $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 = \Magento\Framework\App\ObjectManager::getInstance();
$product = $objectManager->create('Magento\Catalog\Model\Product')->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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/395479.html
標籤:sql 数据库 magento2 magento-2.3
