I want to know how to get the amount of products per website? I don't know if this exists, it should.

I was searching for a plugin or observer, but I cannot find it, under magento-catalog and magento-catalog-inventory.

Greetings!

有帮助吗?

解决方案

public function __construct(
    ...
    \Magento\Catalog\Model\ProductFactory $productFactory,
    ...
) {
    ...
    $this->productFactory = $productFactory;
    ...
}

public function getProduct($storeId, $productId) {
    $product = $this->productFactory->create()
               ->setStoreId($storeId)
               ->load($productId);
    return $product;
}

public function getProductCollection($storeId) {
    $productCollection = $this->productFactory->create()
                          ->setStoreId($storeId)
                          ->getCollection()
                          ->addAttributeToSelect('*');
    return $productCollection;
}

Here the getProduct function takes the store id and the product id as parameter and it runs the product model for that specific store.

And the getProductCollection function takes store id as parameter and returns the product collection for that specific store view.

其他提示

You can get product price by website using below code

public function __construct(
    .....
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Model\ResourceModel\Product $productResourceModel
    ..... ) {
    .....
    $this->productFactory = $productFactory;
    $this->productResourceModel = $productResourceModel;
    ..... }



public function getProductPrice($productId, $storeId) {
    $productFactory = $this->productFactory->create();
    $product = $productFactory->load($productId);
    $product->setStoreId($storeId);
    return $product->getPrice(); 
 }
许可以下: CC-BY-SA归因
scroll top