Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How do I write a (clean) batch…
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

I’d like to write a specific batch launched by a cron…

I already write one to init my shop with some categories, here it is…

<?php
include_once(dirname(__FILE__) . '/../magento/app/Mage.php');
ini_set('memory_limit''32M');

function 
createCategory($c$parent_id 3)
{
    $data 
= array(
    
'name'              => $c['name'],
    
'parent_id'         => $parent_id,
    
'description'       => '',
    
'meta_title'        => '',
    
'meta_keywords'     => '',
    
'meta_description'  => '',
    
'landing_page'      => '',
    
'display_mode'      => 'PRODUCTS',
    
'is_active'         => 1,
    
'is_anchor'         => isset($c['is_anchor']) ? $c['is_anchor'0,
    
'url_key'           => '',
    
'page_layout'       => '',
    );

    
$category Mage::getModel('catalog/category')
    ->
setStoreId(0)
    ->
load(null)
    ->
addData($data);

    
$category->setAttributeSetId($category->getDefaultAttributeSetId());
    
$category->save();

    return 
$category->getId();
}

$yaml 
= <<< YAML

-
    name: My First Category
    is_anchor: true
    categories:
    - { name: Test }
    - { name: whatever }

-
    name: Another Category
    is_anchor: true
    categories:
    - { name: Tasks }
    - { name: Questions }

YAML;

// these 3 lines looks like a little hack, I wish I could write something cleaner...
ob_start();
Mage::run('base');
ob_clean();

$categories syck_load($yaml);

foreach (
$categories as $category{
    $parent_id 
createCategory($category);

    foreach (
$category['categories'as $subcategory{
        createCategory
($subcategory$parent_id);
    
}
}

Is there a way to write this batch in a cleaner way ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

If you’re asking only about these 3 lines, just use

Mage::app('base');
instead, it won’t dispatch any controller action.
 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

Thank you Moshe, I’ll give a try… smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

I’ve just given a try… :(

<pre>
<b>Notice:</strong> <i>Undefined index:  HTTP_HOST</i> in <strong>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/Config.php</strong> on line <b>205</b><br>[0] in <strong>Mage_Core_Model_Config->getDistroServerVars</b>() in <b>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/Config.php</b> on line <b>179</b><br>[1] in <strong>Mage_Core_Model_Config->loadDistroConfig</b>() in <b>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/Config.php</b> on line <b>103</b><br>[2] in <strong>Mage_Core_Model_Config->init</b>("") in <b>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/App.php</b> on line <b>126</b><br>[3] in <strong>Mage_Core_Model_App->init</b>("base", “") in <b>/var/www/magento-test/magento/app/Mage.php</b> on line <b>357</b><br>[4] in <strong>Mage::app</b>("base") in <b>/var/www/magento-test/init/categories.php</b> on line <b>57</b><br>
</pre><pre>
<b>Warning:</strong> <i>file_get_contents(/distro.xml): failed to open stream: No such file or directory</i> in <strong>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/Config.php</strong> on line <b>180</b><br>[0] in <strong>file_get_contents</b>("/distro.xml") in <b>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/Config.php</b> on line <b>180</b><br>[1] in <strong>Mage_Core_Model_Config->loadDistroConfig</b>() in <b>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/Config.php</b> on line <b>103</b><br>[2] in <strong>Mage_Core_Model_Config->init</b>("") in <b>/var/www/magento-test/magento/app/code/core/Mage/Core/Model/App.php</b> on line <b>126</b><br>[3] in <strong>Mage_Core_Model_App->init</b>("base", “") in <b>/var/www/magento-test/magento/app/Mage.php</b> on line <b>357</b><br>[4] in <strong>Mage::app</b>("base") in <b>/var/www/magento-test/init/categories.php</b> on line <b>57</b><br>
</pre>
Fatal error: Uncaught exception ‘Exception’ with message ‘“$string" parameter for simplexml_load_string is empty’ in /var/www/magento-test/magento/lib/Varien/Simplexml/Config.php:317
Stack trace:
#0 /var/www/magento-test/magento/app/code/core/Mage/Core/Model/Config.php(103): Varien_Simplexml_Config->loadString(’’)
#1 /var/www/magento-test/magento/app/code/core/Mage/Core/Model/App.php(126): Mage_Core_Model_Config->init(’’)
#2 /var/www/magento-test/magento/app/Mage.php(357): Mage_Core_Model_App->init(’base’, ‘’)
#3 /var/www/magento-test/init/categories.php(57): Mage::app(’base’)
#4 {main}
thrown in /var/www/magento-test/magento/lib/Varien/Simplexml/Config.php on line 317

too bad, any idea Moshe, or should I keep on using my hack?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Moshe
Magento Team
 
Avatar
Total Posts:  1770
Joined:  2007-08-07
Los Angeles
 

Upload attached file to app/code/core/Mage/Core/Model/Config.php and try again please.

File Attachments
Config.php  (File Size: 20KB - Downloads: 288)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

Ok, your patch works fine. everything is ok with php cli. I just need to add the ‘etc’ path to the function:

Mage::app('base'MAGENTO_PATH '/app/etc');

Thanks again…

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mark_Kimsal
Sr. Member
 
Total Posts:  186
Joined:  2007-09-12
Michigan, USA
 

This script is awesome, it’s exactly what I need.  I wrote a category import that did raw SQL and I have to keep rewriting it.  I always knew i should go back and rewrite it to load up models.

Here is a way to set the URL key automatically from the name.

function getLinkText($lt$sep='_'{
        $lt 
str_replace('&'' and '$lt);
        
$lt str_replace(' '$sep$lt);

        
$pattern '/[\x{21}-\x{2C}]|[\x{2F}]|[\x{5B}-\x{5E}]|[\x{7E}]/';
        
$lt preg_replace($pattern$sep$lt);
        
$lt str_replace($sep.$sep.$sep$sep$lt);
        
$lt str_replace($sep.$sep$sep$lt);
        
$lt str_replace($sep.$sep$sep$lt);
return 
$lt;
}

Those str_replaces can probably be replaced with 1 regex, but whatever.  You can use underscore or dash as the separator.  The first preg_replace removes all non-valid URL characters.  It might blow up accented characters too, i don’t know about that.

 
Magento Community Magento Community
Magento Community
Magento Community
 
webfxmasta
Jr. Member
 
Total Posts:  15
Joined:  2008-02-29
 
Brikou Carré - 23 October 2007 12:52 PM

I’d like to write a specific batch launched by a cron…

I already write one to init my shop with some categories, here it is…

<?php
include_once(dirname(__FILE__) . '/../magento/app/Mage.php');
ini_set('memory_limit''32M');

function 
createCategory($c$parent_id 3)
{
    $data 
= array(
    
'name'              => $c['name'],
    
'parent_id'         => $parent_id,
    
'description'       => '',
    
'meta_title'        => '',
    
'meta_keywords'     => '',
    
'meta_description'  => '',
    
'landing_page'      => '',
    
'display_mode'      => 'PRODUCTS',
    
'is_active'         => 1,
    
'is_anchor'         => isset($c['is_anchor']) ? $c['is_anchor'0,
    
'url_key'           => '',
    
'page_layout'       => '',
    );

    
$category Mage::getModel('catalog/category')
    ->
setStoreId(0)
    ->
load(null)
    ->
addData($data);

    
$category->setAttributeSetId($category->getDefaultAttributeSetId());
    
$category->save();

    return 
$category->getId();
}

$yaml 
= <<< YAML

-
    name: My First Category
    is_anchor: true
    categories:
    - { name: Test }
    - { name: whatever }

-
    name: Another Category
    is_anchor: true
    categories:
    - { name: Tasks }
    - { name: Questions }

YAML;

// these 3 lines looks like a little hack, I wish I could write something cleaner...
ob_start();
Mage::run('base');
ob_clean();

$categories syck_load($yaml);

foreach (
$categories as $category{
    $parent_id 
createCategory($category);

    foreach (
$category['categories'as $subcategory{
        createCategory
($subcategory$parent_id);
    
}
}

Is there a way to write this batch in a cleaner way ?

I get an error in the new 0.8.172 version when trying the above code with

ob_start(); Mage::run('base'); ob_clean();
replaced with
Mage::app('base');

The error is below:

Fatal errorUncaught exception 'Mage_Core_Exception' with message 'Invalid store requested: "base".' in C:\wamp\www\app\Mage.php:356 Stack trace#0 C:\wamp\www\app\code\core\Mage\Core\Model\App.php(494): Mage::throwException('Invalid store r...') #1 C:\wamp\www\app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Collection\Abstract.php(48): Mage_Core_Model_App->getStore() #2 C:\wamp\www\app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Collection\Abstract.php(66): Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract->getStoreId() #3 C:\wamp\www\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(848): Mage_Catalog_Model_Resource_Eav_Mysql4_Collection_Abstract->_getLoadAttributesSelect('catalog_categor...') #4 C:\wamp\www\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(689): Mage_Eav_Model_Entity_Collection_Abstract->_loadAttributes(false, false) #5 C:\wamp\www\app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Category\Collection.php(138): Mage_Eav_Model_Entity_Collection_Abstract->load(f in C:\wamp\www\app\Mage.php on line 356

Any help in fixing this error would be greatly appreciated.  Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jsperri
Sr. Member
 
Total Posts:  126
Joined:  2007-08-31
Fistufle
 

Looks like there has been some internal changes.
You may try to call

Mage::app('default');
 
Magento Community Magento Community
Magento Community
Magento Community
 
webfxmasta
Jr. Member
 
Total Posts:  15
Joined:  2008-02-29
 

Thanks, I will try that.

 
Magento Community Magento Community
Magento Community
Magento Community
 
webfxmasta
Jr. Member
 
Total Posts:  15
Joined:  2008-02-29
 

Does anyone know how to add a product to a category in this same fashion? (Using Mage::getModel or something similar?)

 
Magento Community Magento Community
Magento Community
Magento Community
 
jsperri
Sr. Member
 
Total Posts:  126
Joined:  2007-08-31
Fistufle
 

getModel on ‘catalog/product’ then, you can use setCategoryIds($category_id);
$category_id being a string

It’s probably simpler to use the dataflow module on products as it now permits to specify a category_id for a product.
(I read it’s supported, but did not try it myself)

 
Magento Community Magento Community
Magento Community
Magento Community
 
webfxmasta
Jr. Member
 
Total Posts:  15
Joined:  2008-02-29
 

is the setCategoryIds() function available in 0.8.16100??

 
Magento Community Magento Community
Magento Community
Magento Community
 
jsperri
Sr. Member
 
Total Posts:  126
Joined:  2007-08-31
Fistufle
 

No idea, I am running 0.8.17240.
You can look for the method in Mage_Catalog_Model_Product class. See file app/code/core/Mage/Catalog/Model/Product.php

 
Magento Community Magento Community
Magento Community
Magento Community
 
Brikou Carré
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-04
 

I just wrote another way (updated way i guess) to do some import stuff here is the link http://www.magentocommerce.com/boards/viewthread/4694/ ... If you like it just leave your opinion.. wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
desiboyz
Jr. Member
 
Total Posts:  4
Joined:  2008-03-30
 

Hello,

I tried using your code to batch add categories. It adds the categories to the database but it does not add the correct “path” and as a result, they do not show up in the admin. If I open the table “catalog_category_entity” and modify the path with value “1/2”, then the categories are shown on the page. Will you please help?

Thanks,
Ken

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top