在Magento中以多种语言以编程方式创建类别

时间:2014-04-28 08:52:08

标签: magento categories multilingual

我正在以编程方式为德语和英语添加类别。

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;
        }
    }
}

目前我感到困惑的是单独设置视图ID以传递每种语言的$ category ['general'] ['name']。

我尝试使用foreach设置英语/德语文本来设置每个语言字符串。但这没有用,因为我以某种方式混淆了正确设置视图。

简而言之: 像这样创建类别和子类别确实很有效,但是我没有通过英语/德语类别标题来实现它。

谢谢你们

1 个答案:

答案 0 :(得分:1)

对于那些与这种情况有关的人,我做了一个像这样的工作:

我直接将所有这些值添加到受影响的数据库表中。这肯定是一种快速而肮脏的方式,但它确实起到了作用。

对于所有需要一些想法的人来说,这是(未记录的)剪辑属于我的初始帖子。

        // 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);

我希望这可以帮助那些需要这个的人。