0

I have created a script to update a custom attribute for all of my products and for some reason it isn't running. I have it located in the base folder of magento and I'm calling it using (base siteurl)/fitment.php and it's not executing. I've tested another script using echo and it works. Are there restrictions on reading a file from the base url or something? The file in question is below.

     require_once('app/Mage.php');
     umask(0);
    Mage::app('default');
    Mage::app ()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $path =  Mage::getBaseDir('var') . '/import/YMM.csv';
    $myfile = fopen($path, "r") or die(Mage::log("failed to open" . $path));

    if ($myfile) {
    $sku = array();
    while (($line = fgets($myfile)) !== false) {
        try {
            //split file by comma
            $array = explode(",", $line);
            $count = 0;
            $yearMakeModel = array();
            $make = "";
            $model = "";
            foreach ($array as $ymm) {
                if ($count == 1) {
                    $make = $ymm;
                } elseif ($count == 2) {
                    $model = $ymm;
                } elseif ($count > 1) {
                    array_push($yearMakeModel, $ymm);
                }

                $count++;
            }
            sort($yearMakeModel);
            $fullText = $make . " " . $model . " ";

            $count = 1;
            $yearsText = "";
            $multiple = false;
            $set = false;
            $previous = 0;
            $lastElement = end($yearMakeModel);
            foreach ($yearMakeModel as $years) {

                if (!empty(trim($years))) {
                    if (!$set) {

                        $yearsText = $years;
                        $previous = $years;
                        $set = true;
                    } elseif ($previous + 1 == $years) {
                        if($years == $lastElement){
                            $yearsText = $yearsText . " - " . $years;
                        }
                        $previous = $years;
                        $multiple = true;
                    } else {
                        if ($multiple) {

                            $yearsText = $yearsText . " - " . $previous . ", " . $years;
                            $previous = $years;
                            $multiple = false;
                        } else {
                            $yearsText = $yearsText . ", " . $years;
                            $previous = $years;
                            $multiple = false;
                        }
                    }
                }
                $count++;
            }


            $fullText = $fullText . $yearsText;


            if (array_key_exists($array[0], $sku)) {
                $sku[$array[0]] = $fullText . "<br />" . $sku[$array[0]];
            } else {
                $sku[$array[0]] = $fullText;
            }


        } catch (Exception $e) {
            Mage::log("error updating fitment attribute field");
            Mage::log($e->getMessage());
        }
    }

    /**
     * insert fitment info into the fitment attribute
     */
    try {
        while ($year = current($sku)) {
            echo key($sku) . "<br />";

            $product = Mage::getModel('catalog/product');
            $product = $product->load($product->getIdBySku(key($sku)));


            $product = Mage::getModel('catalog/product')->load($product->getEntityId());

            if($product->getId()){
                $product->setData('fitment', $year)->getResource()->saveAttribute($product, 'fitment');
            }





            next($sku);
        }
    }catch(Exception $e){
        Mage::log($e->getMessage());
    }

}
2
  • there is no restriction. Try to log something right below Mage::app('default'); and see what happens.
    – Marius
    Aug 19 '15 at 14:51
  • can you provide sample csv file?
    – Sukeshini
    Aug 19 '15 at 17:14
0

Your script contains multiple syntax errors: A missing closing brace, and trying to use empty() on the return value of trim(). http://phpcodechecker.com/

Your server probably has error displaying disabled (as it should). You can override that in your custom script by adding this at the beginning:

error_reporting(E_ALL);
ini_set('display_errors',1);
0

check this out, It may help you out to install script.

http://www.appseconnect.com/magento-install-script-and-upgrade-scripts-for-your-extension/

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.