$storeManager = $objectManager->get('\Magento\Store\Model\StoreManagerInterface');
var_dump($storeManager->getStore()->getData());
\Magento\Framework\App\ObjectManager::getInstance()->get('\Psr\Log\LoggerInterface')->addCritical('notice message', ['abc']);
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
if(!$customerSession->isLoggedIn()) {
//未登陆
}else{
//已登陆
}
$category = $this->categoryFactory->create()->load(51);
//var_dump($category->getId());exit;
$collection = $this->_productCollectionFactory->create();
$collection->addAttributeToSelect('*');
//$collection->addAttributeToFilter('entity_id', 30);
$collection->addCategoryFilter($category);//获取
//$collection->setPageSize(3); // fetching only 3 products
foreach ($collection as $key => $pro) {
var_dump($pro->getId());
}
六、获取配置产品下的所有单品
if ("configurable" == $configProduct->getTypeId()) {
//是配置产品
$_children = $configProduct->getTypeInstance()->getUsedProducts($configProduct);
foreach ($_children as $child){
echo "Single Id: ". $child->getID()."\n";
}
echo "count: ".count($_children);
}
七、产品
1、例子一
private $productRepository;
public function __construct(\Magento\Catalog\Model\ProductRepository $productRepository) {
$this->productRepository = $productRepository;
}
public function execute()
{
$productId = $this->getRequest()->getParam('product_id');
$product = $this->productRepository->getById($productId);
$category_ids = $product->getCategoryIds(); //获取产品类目,array(0=>'2',1=>'10',2=>'20')
}
2、例子二
public function _initProduct()
{
$productId = (int)$this->getRequest()->getParam('product');
if ($productId) {
$storeId = $this->_objectManager->get(
\Magento\Store\Model\StoreManagerInterface::class
)->getStore()->getId();
try {
return $this->productRepository->getById($productId, false, $storeId);//
} catch (NoSuchEntityException $e) {
return false;
}
}
return false;
}
3、例子三
protected $productFactory;
public function __construct(
\Magento\Catalog\Model\ProductFactory $productFactory
) {
$this->productFactory = $productFactory;
}
public function render(\Magento\Framework\DataObject $row)
{
$product = $this->productFactory->create()->load($row->getProductId());
return $product->getSku();
}
例子4:get product by sku or id
$productRepository = $this->_objectManager->get('\Magento\Catalog\Model\ProductRepository');
$product = $productRepository->get($sku);//by sku
$product = $productRepository->getById($productId);//by id