php - Magento Configurable Product 工作但在重新保存之前不显示

标签 php magento

我目前正在学习如何为 Magento 创建可配置的产品。一切正常,产品已使用我的代码成功导入,包括其相关产品。问题是产品没有出现在前端。我必须手动转到后端,编辑产品并保存。请注意我不需要更改任何内容,我只需要打开它并保存。只有这样它才会出现在前端。知道这是为什么吗?

define('MAGENTO', dirname(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
require_once 'FeedMag.php';
$myFeed = new FeedMag();
Mage::app();

// test data
$sku = "TESTSKU2";
$inventory = "10";

$stockData['qty'] = $inventory;
$stockData['is_in_stock'] = 1;

$simple['Description'] = 'Configurable Product 1';
$simple['ShortDescription'] = 'Short Description';
$simple['LongDescription'] = 'Long Description';
$simple['BrandCode'] = 'Nike';

$attr['color'] = 'Blue';
$attr['size'] = 1;

$price = 11;

// get attribute id
foreach($attr AS $key=>$value) {
    $attr_ids[] = $myFeed->attributeValueExists($key, $value);
}
$new = false;
echo "<pre>";
try {
    // get product id from SKU
    $id = Mage::getModel('catalog/product')->getIdBySku($sku);

    // load product if id exist or create a new one 
    if($id && $id > 0) {
        $product = Mage::getModel('catalog/product')->load($id);
    }
    else {
        $product = Mage::getModel('catalog/product')->setSku($sku);
        $new = true;
    }

    // set it to configurable
    $product->setTypeId('configurable');

    // get attributes' id
    $usingAttributeIds = $new_array = array();
    foreach( $attr as $key=>$value ) {
        $attribute = $product -> getResource() -> getAttribute( $key );
        if ( $product -> getTypeInstance() -> canUseAttribute( $attribute ) ) {
            if ( $new ) { // fix for duplicating attributes error
                $usingAttributeIds[] = $attribute -> getAttributeId();
            } 
        } 
    } 

    // if we have attributes' ID, set it to the product
    if ( !empty( $usingAttributeIds ) ) {
        $product -> getTypeInstance() -> setUsedProductAttributeIds( $usingAttributeIds );
        $attributes_array = $product->getTypeInstance()->getConfigurableAttributesAsArray();
        foreach($attributes_array as $key => $attribute_value) {
            $attributes_array[$key]['label'] = $attribute_value['frontend_label'];
        }
        $product -> setConfigurableAttributesData($attributes_array);
        $product -> setCanSaveConfigurableAttributes( true );
        $product -> setCanSaveCustomOptions( true );
    }

    // set product data
    $product->setStoreId(0)
             ->setAttributeSetId(4)
             ->setStockData($stockData)
             ->setPrice($price)
             ->setName($simple['Description'])
             ->setShortDescription($simple['ShortDescription'])
             ->setDescription($simple['LongDescription'])
             ->setCategoryIds(array(3))
             ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
             ->setBrand($simple['BrandCode'])
             ->setStatus(1)
             ->setTaxClassId(2) //Taxable goods
             ->save();

    // get previous children if any
    $associated = Mage::getModel('catalog/product_type_configurable')
                ->getChildrenIds($product->getId());

    // add new simple product to configurable product
    $associated[0][] = Mage::getModel('catalog/product')->getIdBySku('SIMPLE1');

    // add all simple product to configurable product
    Mage::getResourceModel('catalog/product_type_configurable')
        ->saveProducts($product->getId(), array_unique($associated[0]));
}
catch (Mage_Core_Exception $e) {
    echo $e->getMessage();
}
catch (Exception $e) {
    echo $e;
}
echo "</pre>";

FeedMag 是我同事制作的自定义类。那里有很多方法,但为此我将只使用一个; attributeValueExists 检查所述属性是否存在,如果存在,将返回其 ID。

简单的产品已经存在,所以我只需要使用它 (SIMPLE1)。

最佳答案

这是导入时索引的问题。您必须在导出表中缺少关联项目和商店所需的字段。它在您保存时起作用的原因是因为它重建了填充缺失数据的表索引。

关于php - Magento Configurable Product 工作但在重新保存之前不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7469043/

相关文章:

php - 使用 exec( ) 并行化 php

php - *仅*在数据库表更改(添加行)时刷新 html 页面部分

magento - 在 Magento 中将所有核心文件覆盖到本地

玛根托 : Backend event handle for customer save

magento 日期时间属性以错误的格式插入

magento - 在magento 1.7.0.2中将多个产品添加到购物车

php - 连接两个表以进行全文搜索

javascript - 将数组从 URL 传递到 javascript

php - 运行 phpunit 时未找到 fatal error 'File/Iterator/Autoload.php'

javascript - 如何在 Magento 扩展中添加自定义 JavaScript