문제

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