1

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?

0

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 );
1
  • Thanks a lot! This solution fits like a glove, I almost used the value right from the db. Aug 23 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.