質問

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帰属
所属していません magento.stackexchange
scroll top