如何从magento 2中的产品ID获取产品信息

时间:2016-01-25 09:42:17

标签: magento2

我想从我们在代码下面使用的所有产品类型中获取价格,特价来自magento 2中的产品ID

    <?php
namespace Namespace\Module\Model;

use Magento\Framework\Model\AbstractModel;
use Magento\Framework\Model\Context;
use Magento\Store\Model\ScopeInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Namespace\Module\Model\ResourceModel\Subscription\CollectionFactory as SubscriptionCollectionFactory;

class Observer extends AbstractModel{

      protected $_storeManager;
      protected $_productCollectionFactory;
      protected $_objectManager;
      protected $_currency;

      public function __construct(
    Context $context,
    \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
    \Magento\Framework\ObjectManagerInterface $objectManager,
    ProductCollectionFactory $productCollectionFactory,
    SubscriptionCollectionFactory $subscriptionCollectionFactory,
    \Magento\Directory\Model\Currency $currency
     ) 
     {  
    $this->_scopeConfig = $scopeConfig;
    $this->_storeManager = $storeManager;   
    $this->_objectManager = $objectManager;
    $this->_productCollectionFactory = $productCollectionFactory;
        $this->subscriptionCollectionFactory  = $subscriptionCollectionFactory;
    $this->_currency = $currency;
     }

     public function sendalert() { 

    $data = $this->subscriptionCollectionFactory->create()
        ->addFieldToSelect('*')
        ->addFieldToFilter('subscription_status', 0);



    $collection = $this->_productCollectionFactory->create()
              ->addAttributeToFilter('status', 1)
              ->addAttributeToFilter('visibility', 4)
              ->addAttributeToSelect('*');
    $prodIds = $collection->getAllIds();



    foreach($prodIds as $productId)
    {
        $om =   \Magento\Framework\App\ObjectManager::getInstance();
        $pdata = $this->_objectManager()->create('Magento\Catalog\Model\Product')->load($productId);
        echo '>>'.$pdata->getPrice();
    }

      }


}

6 个答案:

答案 0 :(得分:3)

其他答案告诉您直接使用ObjectManager,但您不应该这样做,因为您可以使用Dependency Injection模式。 ObejctManager只应在引导期间使用,或者如果您确实有理由使用它。

更好的方法是使用\Magento\Catalog\Model\ProductRepository::getById方法。您可以像对待其他构造函数参数一样注入ProductRepository自动构造函数注入:

$productRepository; 
public function __construct(
    Context $context,
    \Magento\Catalog\Api\ProductRepositoryInterface $pr
    // ...
    ) 
{  
    $this->productRepository = $pr;
    // ....
}

然后使用

$productRepository->getById(1234);

如果您需要有关如何使用存储库的更多信息,也许this tutorial可以为您提供帮助。

答案 1 :(得分:1)

试试这个

<?php  
   $productId = 8;
   $objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
   $currentproduct = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
   echo $currentproduct->getName(); 
?>

答案 2 :(得分:0)

以下代码适用于我

&#13;
&#13;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productObject = $objectManager->create('Magento\Catalog\Model\Product');
$product      = $productObject->load('enter product id');
&#13;
&#13;
&#13;

这里的ObjectManager类就像服务或工厂类,用于创建产品模型的对象。

如果你有双重话,请告诉我。

答案 3 :(得分:0)

尝试以下

use Magento\Catalog\Model\ProductFactory;
/**
 * @var ProductFactory
 */
protected $_modelProductFactory;
public function __construct(
    ...      
    ProductFactory $modelProductFactory,  
    ...
)
{           
    $this->_modelProductFactory = $modelProductFactory; 
    ...
}

public function getProductInformation($productId)
{
    return $this->_modelProductFactory->create()->load($productId);
}

答案 4 :(得分:0)

使用ObjectManager。

[
    Response.Record(userName: Optional("123user"),
                 review: Optional("Ggg"),
                 bookingID: "Booking1",
                 reviewID: Optional("review_38405"),
                 status: Optional("active"),
                 id: Optional("5e0c43ea5bd0377f4cfdfa19"),
                 rating: Optional(5)),
    Response.Record(userName: Optional("123user"),
                 review: Optional("Ggg"),
                 bookingID: "Booking2",
                 reviewID: Optional("review_38405"),
                 status: Optional("active"),
                 id: Optional("5e0c43ea5bd0377f4cfdfa19"),
                 rating: Optional(5))
]

使用工厂方法。

<?php $product_id = 45; // product id
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($product_id);
?>

在phtml文件中使用。

<?php
namespace Test\Module\Block;

class Product extends \Magento\Framework\View\Element\Template
{

  protected $_productloader;  


  public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Model\ProductFactory $_productloader

    ) {


    ) {


        $this->_productloader = $_productloader;
        parent::__construct($context);
    }
    public function getLoadProduct($id)
    {
        return $this->_productloader->create()->load($id);
    }

}
        $this->_productloader = $_productloader;
        parent::__construct($context);
    }
    public function getLoadProduct($id)
    {
        return $this->_productloader->create()->load($id);
    }

}

谢谢。

答案 5 :(得分:-1)

在Magento 2中建议使用服务层。尝试使用\ Magento \ Catalog \ Model \ ProductRepository :: getById方法按ID获取产品

尝试以下代码

$prod_id = "Your product id goes here";
$om =   \Magento\Framework\App\ObjectManager::getInstance();
$pdata = $om->create('Magento\Catalog\Model\Product')->load($prod_id);