1

On catalog_product_save_before event I've got handler that should update prices for product. On save I need to update prices for every store, not one price for Default.

$sellerslist = Mage::getModel('mpassignproduct/mpassignproduct')->getCollection();
$sellerslist = $this->getSellersList();
$data = $sellerslist->getData();
if (!empty($data) && count($data) > 0 && !empty($data[0]['mpassignproduct_id']) && $data[0]['mpassignproduct_id'] + 0 > 0) {
    $stores = Mage::helper('advox_core')->getStoresByWebsiteId($websiteId);
    foreach ($stores as $storeId) {
        // Previously I tried to load this product to new variable.
        //$product = Mage::getModel('catalog/product')->setStoreId($storeId + 0)->load($product->getId());
        $product->setStoreId($storeId + 0);
        $product->setMinPrice($data[0]['minprice']);
        $price = $data[0]['minprice'];
        if ($price === null) {
            $price = 0;
        }
        $product->setPrice($price);
    }
}

I can't load product again for specific store because on save there is once again called catalog_product_save_before so infinite event observer is called. How to change product prices for specific store.


Update:

I changed to:

$websiteIds = $product->getWebsiteIds();
foreach ($websiteIds as $websiteId) {
    $sellerslist = $this->getSellersList();
    $data = $sellerslist->getData();
    if (!empty($data) && count($data) > 0 && !empty($data[0]['mpassignproduct_id']) && $data[0]['mpassignproduct_id'] + 0 > 0) {
        $product->setWebsiteId(array($websiteId + 0));
        $product->setMinPrice($data[0]['minprice']);
        $price = $data[0]['minprice'];
        if ($price === null) {
            $price = 0;
        }
        $product->setPrice($price);
    }
}
$product->setWebsiteIds($websiteIds);

as you said that prices should be changed only for websites. I tried with catalog_product_prepare_save event but still not updating.


Update 2:

While saving product in admin panel I've got only global product model. Is there any simple way to load website specific data to this product model and then change this details?

2

You usually would do something like that with an observer on catalog_product_prepare_save, this event is triggered in Mage_Adminhtml_Catalog_ProductController::_initProductSave().

But you cannot set prices per store, prices can only be set per website. Changing the allowed scope of the price attributes would require significant changes in the core, especially the price index. So if you need different prices on different sites, create separate websites, not only separate stores.

0

After few days of trying almost everything I stayed with catalog_product_save_before but also write method that get saved product (that is GLOBAL) and change prices with hard coded SQL:

<?php
public function setProductMinPrice(Mage_Catalog_Model_Product $product)
{
    $websiteIds = $product->getWebsiteIds();
    foreach ($websiteIds as $websiteId) {
        $sellerslist = Mage::getModel('mpassignproduct/mpassignproduct')->getCollection();
        $sellerslist->getSelect()
            ->columns('MIN(if (special_price IS NOT NULL, special_price, price)) as minprice')
            ->where('qty > ?', 0)
            ->where('active = ?', true)
            ->where('website = (?)', $websiteId)
            ->where('product_id = ?', $product->getId());
        $data = $sellerslist->getData();
        if (!empty($data) && count($data) > 0 && !empty($data[0]['mpassignproduct_id']) && $data[0]['mpassignproduct_id'] + 0 > 0) {
            $stores = Mage::helper('fkg_core')->getStoresByWebsiteId($websiteId);
            foreach ($stores as $storeId) {
                $product->setMinPrice($data[0]['minprice']);
                $price = $data[0]['minprice'];
                if ($price === null) {
                    $price = 0;
                }
                $product->setPrice($price);
                $write = Mage::getSingleton('core/resource')->getConnection('core_write');
                $write->query("
                    UPDATE catalog_product_entity_decimal val
                    SET val.value = {$price}
                    WHERE val.attribute_id = (
                        SELECT attribute_id FROM eav_attribute eav
                        WHERE eav.entity_type_id = 4
                            AND eav.attribute_code = 'price'
                        )
                        AND val.store_id = {$storeId}
                ");
            }
        }
    }
    return $this;
}

That save product price from another table and assign to product in everyt store.

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.