i am tyring to add categories programmatically for german and english languages.

website-ID: 1
website store-ID: 1

view-ID english, code "en" => 1
view-ID german, code "de" => 2

    $category['general']['path'] = $v['cat_name_de'];
    $category['general']['name'] = $v['cat_name_de'];
    $category['general']['meta_title'] = "";
    $category['general']['meta_description'] = "";
    $category['general']['is_active'] = 1;
    $category['general']['url_key'] = urlencode($v['cat_name_de']);
    $category['general']['display_mode'] = "PRODUCTS";
    $category['general']['is_anchor'] = 0;
    $category['category']['parent'] = $v['magento_cat_id'];
    $storeId = 0; // default
    $magento_subcat_id = createCategory($category, $storeId);

...

function createCategory($data, $storeId) {
    echo "Erzeuge '{$data['general']['name']}' - Elternkategorie: 
    [{$data['category']['parent']}] ...";

    $category = Mage::getModel('catalog/category');
    $category->setStoreId($storeId);

    if (is_array($data)) {
        $category->addData($data['general']);

    if (!$category->getId()) {

        $parentId = $data['category']['parent'];
            if (!$parentId) {
                if ($storeId) {
                    $parentId = Mage::app()->getStore($storeId)->getRootCategoryId();
                } else {
                    $parentId = Mage_Catalog_Model_Category::TREE_ROOT_ID;
                }
            }
            $parentCategory = Mage::getModel('catalog/category')->load($parentId);
            $category->setPath($parentCategory->getPath());
        }

    if ($useDefaults = $data['use_default']) {
        foreach ($useDefaults as $attributeCode) {
            $category->setData($attributeCode, null);
        }
    }

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

    if (isset($data['category_products']) &&
            !$category->getProductsReadonly()) {
        $products = array();
        parse_str($data['category_products'], $products);
        $category->setPostedProducts($products);
    }
        try {
            $category->save();
            return $category->getId();
        } catch (Exception $e) {
            return FALSE;
        }
    }
}

What i am puzzling with at the moment is setting view-ID individually for passing the $category['general']['name'] for each language.

I tried to set each language string with a foreach setting english/german texts. But this did not work out, because i somehow mixed up setting the view correctly.

To make it short: Creating categories and subcategories like this does work very good, but i do not get it to work to pass english/german categoty titles.

Thanks guys

有帮助吗?

解决方案

For all of you who are struggeling with this situation i made a workaound like this:

I added all those values to the affected database tables directly. This is for sure a quick and dirty way, but it does the job.

For all in need of some ideas here is the (undocumented) snipped belonging to my initial post.

        // Get values for next language insert
        $entity_type_id = $category
            ->getResource()
            ->getTypeId();
        $entity_id = $category->getEntityId();

        $read_entity = Mage::getSingleton('core/resource')
            ->getConnection('core_write');
        $sql_entity = "SELECT * FROM eav_attribute WHERE 
            `attribute_code` = 'name' AND 
            `entity_type_id` = " . $entity_type_id . ";";

        $row_entity = $read_entity
            ->fetchRow($sql_entity);
        $attribute_id = $row_entity['attribute_id'];

        // prepare value for name field in different langauge
        $val = Mage::getSingleton('core/resource')
            ->getConnection('default_write')
            ->quote($v['en']);

        $sql_insert = "INSERT INTO `catalog_category_entity_varchar` 
            (`entity_type_id`,`attribute_id`,`store_id`,`entity_id`,`value`) 
            VALUES ($entity_type_id,$attribute_id,$storeId,$entity_id,$val);";

        $read_entity->query($sql_insert);

I hope this helps someone out who is in need of this.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top