Magento2 – How to Get ‘All Store Views’ Price Scope from a Product

priceproductproduct-pricesstores

I have 3 websites, each with one store and one store view.

I have all products' collections based on store in hand, i'm doing a price change for each product using the magento' API within a script, but to do that i need to use the price stored at "all store views" scope to a calculation.

the price that I need

I get all products collection from each store with below function:

protected function getProductCollectionByStoreId($storeId)
    {
        $productCollection = $this->objectManager
            ->create('Magento\Catalog\Model\ResourceModel\Product\CollectionFactory');
        $collection = $productCollection->create()
            ->addAttributeToSelect('*')
            ->addStoreFilter($storeId)
            ->load();        
        return $collection;
    }

And then I update the prices for each collection of products based on store:

protected function setProductPricesBasedOnStoreWithoutRest($stores){
        foreach($stores as $storeData){
            $storeId = $storeData[0];
            $storeCode = $storeData[1];
            $multiplier = $storeData[3];
            $currencyRate = $storeData[4];
            $productCollection = $this->getProductCollectionByStoreId($storeId);
            foreach($productCollection as $product){
                // ...
                $price = $product->getPrice();
                $newPrice = round(ceil(($price * $multiplier) / $currencyRate), 2);
                // ...
            }
        }
    }

$price needs to get the value stored in "all store views" price (showed in the image) and not the price in the product' storeview scope. What's the easyest way to get that value?

Best Answer

You are able to get the price of a store or base store (Store 0 [Store_id] Usually) with the resource:

$resource = $product->getResource();
            $priceBaseStore    = $resource->getAttributeRawValue($product->getId(),'price', 0 );
            $SpecialPriceBaseStore   = $resource->getAttributeRawValue($product->getId(),'special_price', 0 );
            $priceStore1  = $resource->getAttributeRawValue($product->getId(),'price', 1 );
            $specialPriceStore1 = $resource->getAttributeRawValue($product->getId(),'special_price', 1 );