2

I'm in the process of cleaning up my existing magento install by moving the DB and using a new install so I can do away with all the unnecessary extensions taking up space.

Everything seems to have been setup fine, however some product attributes are not showing up in the admin, but doing a save brings the values back. However I have over 7000 products and cant save them individually. All the products are simple products.

So I've used this script from this article.

<?php
require 'app/Mage.php';
Mage::app();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
    $product->load();
    echo 'Product: ' . $product->getId() . ' - ' . $product->getName() . "\xA";

    $product->setShortDescription('N/A');
    $product->save();
}
?>

But I need to run this script via the browser and if the internet cuts out, I have to start again going over the ones that were already saved.

  1. Instead, how can I convert this to a shell script and run if via SSH?

  2. Also I have one store that doesn't have the same values as the default (all store views) how/where do I specify that the script run for only that store?

Thanks in advance.

0

2 Answers 2

0

Working shell script here: run it from shell directory php productupdate.php --stores <id,...>

shell/productupdate.php

<?php
require_once 'abstract.php';

class Your_Shell_Productupdate extends Mage_Shell_Abstract
{
    protected $_stores = array();

    public function __construct() {
        parent::__construct();
        set_time_limit(0);

        if ($this->getArg('stores')) {
            $this->_stores = array_merge(
                $this->_stores,
                array_map(
                    'trim',
                    explode(',', $this->getArg('stores'))
                )
            );
        } else {
            $this->_stores = array(Mage_Core_Model_App::ADMIN_STORE_ID);
        }
    }

    public function run()
    {
        try {
            printf(
                'Selected stores: %s'."\n",
                '"'.implode('", "', $this->_stores).'"'
            );

            foreach ($this->_stores as $storeId) {
                $products = Mage::getModel('catalog/product')->getCollection();
                Mage::getSingleton('core/resource_iterator')->walk(
                    $products->getSelect(),
                    array(array($this, '_saveShortDescription')),
                    array('store_id' => $storeId)
                );

                printf(
                    'Done processing.'."\n"
                        .'Total processed products count for store %d: %d'."\n",
                    $storeId, $products->getSize()
                );
            }
        } catch (Exception $e) {
            echo $e->getMessage().'@'.time();
        }
    }

    public function _saveShortDescription($args)
    {
        Mage::getSingleton('catalog/product_action')->updateAttributes(
            array($args['row']['entity_id']),
            array('short_description' => 'N/A'),
            $args['store_id']
        );
    }

    public function usageHelp()
    {
        return <<<USAGE
Usage:  php -f productupdate.php

  --stores <ids>       Process only these stores (comma-separated)

  help                 This help

USAGE;
    }
}

$shell = new Your_Shell_Productupdate();
$shell->run();
0
 <?php 
use Magento\Framework\App\Bootstrap;

include("./app/bootstrap.php");
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('frontend');
 // add logging capability
$writer = new \Zend\Log\Writer\Stream(BP . '/var/log/import-Products.log');
$logger = new \Zend\Log\Logger();
$logger->addWriter($writer);
$eavConfig = $objectManager->get('\Magento\Eav\Model\Config');
// $attribute = $eavConfig->getAttribute('catalog_product', 'test_enable');
// $options = $attribute->getSource()->getAllOptions('1');
// print_r($options);
// die();
$attribute = $eavConfig->getAttribute('catalog_product', 'es_flavour');
$options = $attribute->getSource()->getAllOptions();

foreach($options as $option) {
    if($option['label'] == "braambes"){
        $value = $option['value'];
    }
}
$attribute2 = $eavConfig->getAttribute('catalog_product', 'es_pet');
$options2 = $attribute2->getSource()->getAllOptions();
// print_r($options2);
// die();
foreach($options2 as $option1) {
    if($option1['label'] == "Schaap"){
        $value2 = $option1['value'];
    }
}
   // try{
            $rand=rand(1,50);
            $simpleProduct = $objectManager->create('\Magento\Catalog\Model\Product');
            $simpleProduct->setSku('Testing'.$rand);
            $simpleProduct->setName('Testing11115'.$rand);
            // $attributeSetId = $simpleProduct->getDefaultAttributeSetId();
            $simpleProduct->setAttributeSetId(4);
            $simpleProduct->setCategoryIds(array(6));
            $simpleProduct->setDescription('This is for testing');
            $simpleProduct->setStatus(1);
            $simpleProduct->setTypeId('simple');
            $simpleProduct->setTaxClassId(2); // (0 - none, 1 - default, 2 - taxable, 4 - shipping)
            $simpleProduct->setPrice(500);
            $simpleProduct->setWebsiteIds([1]);
            $simpleProduct->setVisibility(4);
            // $simpleProduct->setUrlKey('Testing445');
            $simpleProduct->setStoreId(1);
            $simpleProduct->setStockData(array(
                'is_in_stock' => 1, //Stock Availability
                'qty' => 100//qty
                    )
            );

            $simpleProduct->setCustomAttribute("es_flavour",$value);
            $simpleProduct->setCustomAttribute("es_pet",$value2);

            // set the select value
            $attr = $simpleProduct->getResource()->getAttribute('es_foot_size');
            $attributeOptionId = $attr->getSource()->getOptionId('10'); //name in Default Store View
            // print_r($attributeOptionId);
            // die();
            $simpleProduct->setData('es_foot_size', $attributeOptionId);

            //get the yes or no attribute
            $attr = $simpleProduct->getResource()->getAttribute('test_enable');
            $attributeOptionId = $attr->getSource()->getOptionId('1'); //name in Default Store View
            // print_r($attributeOptionId);
            // die();
            $simpleProduct->setData('test_enable', $attributeOptionId);

            $simpleProduct->save();
            $simpleProductId = $simpleProduct->getId();
            $simpleProductSku = $simpleProduct->getSku();
            echo "Simple Product ID Store 1: " . $simpleProductId . "\n";
            
            // $simpleProduct2 = $objectManager->create('\Magento\Catalog\Model\Product')->load($simpleProduct->getId());
            // $simpleProduct2->setStoreId(2);
            // $simpleProduct->setWebsiteIds(array(2));
            // $simpleProduct2->setDescription('Description store view 2');
            // $simpleProduct2->save();
            // $simpleProductId = $simpleProduct2->getId();
            // echo "Simple Product ID Store 2: " . $simpleProductId . "\n";


//     }catch(Exception $e) {
//             //print_r($e->getMessage());
//             $logger->info('Error importing product sku: '.$simpleProductId.'-'.$e->getMessage());
//             // continue;
// }
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.