Magento可配置产品工作,但不显示,直到重新保存


Magento Configurable Product working but not showing until re-saved

我目前正在学习如何为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)。

导入时索引存在问题。导出工作表中一定缺少关联项目和存储所需的字段。保存时它能工作的原因是它重建了表的索引填入了缺失的数据