Question

I have Magento ver. 1.9.3.0. I have 2 stores, each for different country.
Everyday my shop synchronizes prices with ERP system by cron. If price for one product was changed in ERP, then cron sets new price for that product.

I have noticed that every time after synchronization, Default Value checkbox is unchecked for changed product in General section. It is very annoying to check all checkboxes, even after I have set all them day before. I have no idea why it happens.

There is main cron function, that updates product:

public function prepare($store, $ids, $productId) {
    $updateProduct = false;
    $product = Mage::getModel('catalog/product');
    $minPrice = null;

    if ($store) {
        $product->setStoreFilter($store);
        $product->setStoreId($store->getId());
    }
    $product->load($productId);

    $collection = $this->getSimpleProductsByIds($ids, $store);

    foreach ($collection as $simple) {
        $price = $simple->getPrice();
        if ($price > 0 && (is_null($minPrice) || $price < $minPrice)) {
            $minPrice = $price;
        }
    }

    if ($product->getPrice() != $minPrice && !is_null($minPrice)) {
        $updateProduct = true;
        $product->setPrice($minPrice);
    }


    $configurableArray = $product->getTypeInstance()->getConfigurableAttributesAsArray();
    foreach ($collection as $simple) {
        $price = $simple->getPrice();
        $valueIndex = $simple->getData('size_simple');

        if ($configurableArray && isset($configurableArray[0]) && isset($configurableArray[0]['values']) && $configurableArray[0]['attribute_code'] == 'size_simple') {
            foreach ($configurableArray[0]['values'] as $key => $value) {
                if ($value['value_index'] == $valueIndex) {
                    $diff = (float) ($price - $product->getPrice());
                    if ($diff != 0 || $diff != $value['pricing_value']) {
                        $updateProduct = true;
                        $configurableArray[0]['values'][$key]['pricing_value'] = $diff;
                        $configurableArray[0]['values'][$key]['use_default_value'] = 0;
                        $configurableArray[0]['values'][$key]['is_percent'] = 0;
                    }
                }
            }
        }
    }
    if ($updateProduct) {
        $product->setCanSaveConfigurableAttributes(true);
        $product->setConfigurableAttributesData($configurableArray);
        $product->setData('update_price', 0);
        $product->setData('bold_catalog_update', 1);
        try {
            $product->save();  // there is something wrong
        } catch (Exception $e) {
            return false;
        }
    } else if ($product->getData('update_price') == 1) {
        try {
            $product->setData('update_price', 0);
            $product->getResource()->saveAttribute($product, 'update_price');
        } catch (Exception $e) {
            return false;
        }
    }
    return true;
}

$configurableArray looks like that:

array(1) {
  [0]=>
  array(9) {
    ["id"]=>
    string(4) "2353"
    ["label"]=>
    string(11) "Size Simple"
    ["use_default"]=>
    string(1) "0"
    ["position"]=>
    string(1) "0"
    ["values"]=>
    array(2) {
      [0]=>
      array(9) {
        ["product_super_attribute_id"]=>
        string(4) "2353"
        ["value_index"]=>
        string(1) "3"
        ["label"]=>
        string(5) "S/M/L"
        ["default_label"]=>
        string(5) "S/M/L"
        ["store_label"]=>
        string(5) "S/M/L"
        ["is_percent"]=>
        int(0)
        ["pricing_value"]=>
        NULL
        ["use_default_value"]=>
        bool(true)
        ["order"]=>
        int(6)
      }
      [1]=>
      array(9) {
        ["product_super_attribute_id"]=>
        string(4) "2353"
        ["value_index"]=>
        string(1) "6"
        ["label"]=>
        string(6) "XL/XXL"
        ["default_label"]=>
        string(6) "XL/XXL"
        ["store_label"]=>
        string(6) "XL/XXL"
        ["is_percent"]=>
        int(0)
        ["pricing_value"]=>
        NULL
        ["use_default_value"]=>
        bool(true)
        ["order"]=>
        int(11)
      }
    }
    ["attribute_id"]=>
    string(3) "133"
    ["attribute_code"]=>
    string(11) "size_simple"
    ["frontend_label"]=>
    string(11) "Size Simple"
    ["store_label"]=>
    string(7) "Rozmiar"
  }
}

There is a key: use_default_value, but I have no idea to what it points.

Was it helpful?

Solution

Calling $product->save() implies that all the product data will be saved and as you have loaded the entire product for a specific store :

if ($store) {
        $product->setStoreFilter($store);
        $product->setStoreId($store->getId());
 }
$product->load($productId);

Then all the data is saved for this specific store (that implies that the default values are not use anymore : so the checkbox is unchecked).

If you want to save only specific data of your product, you should use the saveAttribute method:

$product->getResource()->saveAttribute (it is also better for performance). Or, I guess, it will be more work for you, you should not load the entire product ($product could be an item of a collection with only wanted attributes selected).

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top