1

属性オプション ラベルを更新する方法に関する情報がウェブ上にあります (例: http://www.webspeaks.in/2012/05/addupdate-attribute-option-values.html ) が、並べ替え順序のみを更新するにはどうすればよいですか?

これを実行した理由は、製造元を製品ビュー数でソートしたかったからです。したがって、最もグローバルなビューを持つメーカーが一番上にソートされます。

4

2 に答える 2

1

本当に面倒です。更新時に何も上書きしないように、ストアごとにラベルの配列を作成する必要があります。次に、並べ替え順序をその配列にアタッチできます。これを機能させるコードは次のとおりです。

//build array of labels
        $attribute_model        = Mage::getModel('eav/entity_attribute');
        $attribute_code         = $attribute_model->getIdByCode('catalog_product', 'manufacturer');

        //get default label values for option
        $optionCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setAttributeFilter($attribute_code)
                ->setPositionOrder('desc', true)
                //->setStoreFilter($_eachStoreId)
                ->load();

        //build the data required to reset the labels to what they were (what a pain)       
        foreach ($optionCollection as $option) 
            $data['value'][$option->getData('option_id')][0] = $option->getData('value');


        //go through the stores one by one
        foreach (Mage::app()->getStores() as $_eachStoreId => $val) 
        {

            //get the labels for this attribute for that store
            $optionCollection = Mage::getResourceModel('eav/entity_attribute_option_collection')
                    ->setAttributeFilter($attribute_code)
                    ->setPositionOrder('desc', true)
                    ->setStoreFilter($_eachStoreId)
                    ->load();

            //build the data required to reset the labels to what they were (what a pain)       
            foreach ($optionCollection as $option) 
                if( $data['value'][$option->getData('option_id')][0] != $option->getData('value') )
                    $data['value'][$option->getData('option_id')][$_eachStoreId] = $option->getData('value');
                else
                    $data['value'][$option->getData('option_id')][$_eachStoreId] = '';

        }

        //echo "<pre>"; print_r($data); die; 

        //just load all products with view counts and build manufacturerValueId => totalViews array
        $products = Mage::getResourceModel('reports/product_collection')
                    ->addViewsCount()
                    ->addAttributeToSelect('manufacturer')
                    ;

        foreach($products as $product){
            if ($product->getManufacturer()!='') $data['order'][$product->getManufacturer()] += $product->getViews();
        }

        //now we've gotta invert this array
        //put largest value at top
        arsort($data['order']);
        foreach($data['order'] as $key => $value)
        {
            if(!isset($highest)) $highest = $value; 
            $newData['order'][$key] = $highest - $value;
        }
        $data['order'] = $newData['order']; unset($newData);
        //echo "<pre>"; print_r($data); die;

        $data = array('option' => $data );

        //Get the eav attribute model
        $attr_model = Mage::getModel('catalog/resource_eav_attribute');

        //Load the particular attribute by id
        $attr_model->load($attribute_code);

        //Add data to our attribute model
        $attr_model->addData($data);

        //echo "<pre>"; print_r($attr_model->getData()); die; 

        //Save the updated model
        try {
            $attr_model->save();
            /**
             * Clear translation cache because attribute labels are stored in translation
             */
            $session = Mage::getSingleton('adminhtml/session');
            Mage::app()->cleanCache(array(Mage_Core_Model_Translate::CACHE_TAG));
            $session->setAttributeData(false);
            return;
        } catch (Exception $e) {
            echo "<pre>"; print_r($e->getMessage());
            echo "<pre>"; print_r($data);
            return;
        }
于 2013-07-19T03:12:46.393 に答える