phpsqlmagentomagento-1.9magmi

Set product price to be equal to the special price for all simple products


I know that this is a framework-specific question and not really a general programming one, but I already asked this question on the Magento StackExchange forums and got no response from there.

I want to replace the normal price with the special price for all my simple products (the special price will no longer exist). I am pretty new with Magento and I don't know what's the best approach to doing this.

I was thinking about a SQL Query that replaces the values, then find a way to disable the special prices on the no-longer-discounted products.

I was also thinking that I could use the Import/Export tool to export a list with all the discounted simple products, change their attributes in Excel and then import the new csv with Magmi, but the default Magento Import/Export tool does not work that well for me (multiple lines for the same SKU).

Is there any way I could do this safely? Is Magmi capable of disabling all the special prices from my products?

My Magento version is Magento 1.9

Thank you!


Solution

  • My personal preference is to use Magento shell scripts. You can find few examples under shell dir.

    Once you create one, you can simply call it with:

    php shell/<script-name>.php

    This way, you have access to Magento's ORM, and you can do accomplish this in much safer way.

    It should be quite easy to implement such cli script, but if you need help here is untested version: :)

    <?php
    
    require_once 'abstract.php';
    
    class Mage_Shell_SpecialPrice extends Mage_Shell_Abstract
    {
        public function run()
        {
            if ($this->getArg('help')) {
                echo $this->usageHelp();
                return;
            }
    
            $collection = $this->_getProductCollection();
    
            /** @var Mage_Catalog_Model_Product $product */
            foreach ($collection as $product) {
                $product->setPrice($product->getSpecialPrice())
                    ->setSpecialPrice(null);
    
                $product->getResource()->saveAttribute($product, 'price');
                $product->getResource()->saveAttribute($product, 'special_price');
            }
        }
    
        /**
         * @return Mage_Catalog_Model_Resource_Product_Collection
         */
        protected function _getProductCollection()
        {
            return Mage::getResourceModel('catalog/product_collection')
                ->addAttributeToFilter('special_price', array('notnull' => true))
                ->setOrder($this->getArg('limit') ?: 'created_at', $this->getArg('dir') ?: 'desc')
                ->setPageSize($this->getArg('limit') ?: 20);
        }
    
        /**
         * @return string
         */
        public function usageHelp()
        {
            return <<<USAGE
    Usage:  php -f special-price.php
            php -f special-price.php --limit 20 --sort created_at --dir desc
    USAGE;
        }
    }
    
    $shell = new Mage_Shell_SpecialPrice();
    $shell->run();
    

    I tossed in some command line argument examples for good measure, but that's generally that.

    Cheers. :)