Php 如何在Magento 2中通过特定属性获取产品 我有一个疑问:

Php 如何在Magento 2中通过特定属性获取产品 我有一个疑问:,php,magento,product,magento2,Php,Magento,Product,Magento2,我在这里寻找传递$product\u id变量的产品 $product_id = 2047; $attr_color = 54; $attr_size = 170; $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface'); $prod

我在这里寻找传递$product\u id变量的产品

$product_id = 2047;
$attr_color = 54;
$attr_size = 170;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
我想得到与attr_颜色和attr_大小变量匹配的特定产品变量。我怎么能做到

使用attr\u颜色获得特定的产品变化attr\u size您需要首先获取该属性,然后将您的属性id传递给该属性

请尝试以下代码:

            $product_id = 2047;
            $attr_color = 54;

          $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
          $_product = $objectManager->get('Magento\Catalog\Model\Product')->load($product_id);
          $_product = $_product->getResource()->getAttribute('color');    

         if ($_product->usesSource()) {
           $_product = $_product->getSource()->getOptionText($attr_color);
        }
           return $_product;

同样,你也可以为属性大小做这件事。

@AndresGonzales很乐意继续帮助:)