以编程方式创建捆绑产品:选项和产品不显示在前端,除非从管理员保存

时间:2013-09-10 13:53:22

标签: magento

我已经以编程方式创建了捆绑产品,并且还能够向其添加选择和选项数据,但我的问题是产品在前端不可见。当我保存该产品时,即使不更改管理员的任何字段,它也会开始显示。

我被困住了,如果你有任何想法,我不知道该怎么办请帮助我。下面是使用的代码

    $storeID = 0;
$websiteIDs = array(1);
$cats = array(19);

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

/** @var $productCheck Mage_Catalog_Model_Product */
$productCheck = Mage::getModel('catalog/product');

$p = array(
        'sku_type' => 1, //0 = dynamic, 1 = fixed
        'sku' => '687',
        'name' => "BarProduct",
        'description' => 'Foo',
        'short_description' => 'Bar',
        'type_id' => 'bundle',
        'attribute_set_id' => 4,
        'weight_type' => 0, //0 = dynamic, 1 = fixed
        'visibility' => 4,
        'price_type' => 0, //0 = dynamic, 1 = fixed
        'price_view' => 0, //0 = as low as, 1 = price range <---- DOES NOT SEEM TO HAVE ANY EFFECT
        'status' => 1,
        'category_ids' => $cats,
        'store_id' => $storeID,
        'website_ids' => $websiteIDs
);

$productCheck->setData($p);
Mage::register('product', $productCheck);

$selectionRawData = array();
$selectionRawData[0] = array();

$optionRawData = array();
$ChildProduct = array(1,2);
$i = 0;
foreach ($ChildProduct as $child){
    $optionRawData[$i] = array(
            'required' => 1,
            'option_id' => '',
            'position' => 0,
            'type' => 'select',
            'title' => 'FooOption',
            'default_title' => 'FooOption',
            'delete' => '',
    );

    $selectionRawData[$i][0] = array(
            'product_id' => $child,
            'selection_qty' => 1,
            'selection_can_change_qty' => 0,  // 1-> yes, 0 -> no
            'position' => 0,
            'is_default' => 1,
            'selection_id' => '',
            'selection_price_type' => 0,
            'selection_price_value' => 0.0,
            'option_id' => '',
            'delete' => ''
    );

$i++;
}

Mage::register('productCheck', $productCheck);
Mage::register('current_product', $productCheck);
$productCheck->setCanSaveConfigurableAttributes(false);
$productCheck->setCanSaveCustomOptions(true);
$productCheck->setCanSaveBundleSelections(true);
// Set the Bundle Options
$productCheck->setBundleOptionsData($optionRawData);
//set option data
$productCheck->setBundleSelectionsData($selectionRawData);

$productCheck->setAffectBundleProductSelections(true);

$productCheck->save();

2 个答案:

答案 0 :(得分:0)

产品是否“有库存”并且管理面板中的数量是否至少为“1”?您似乎没有在代码中设置这些值。将此代码段添加到您的代码中:

$product->setStockData(array(
    'is_in_stock' => 1,
    'qty' => 1
));

答案 1 :(得分:0)

试试这个......

        $storeID = 1;
        $websiteIDs = array(1);
        $cats = array(13);

        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

        $productCheck = Mage::getModel('catalog/product');
        $p = array(
            'sku_type' => 0,
            'sku' => 'test-sku',
            'name' => 'test product',
            'description' => 'test product',
            'short_description' => 'test product',
            'type_id' => 'bundle',
            'attribute_set_id' => 4,
            'weight_type' => 0,
            'visibility' => 4, //visibility : catalog, search
            'price_type' => 0,
            'price_view' => 0,
            'status' => 1,
            'created_at' => strtotime('now'),
            'category_ids' => $cats,
            'store_id' => $storeID,
            'website_ids' => $websiteIDs
        );
        $productCheck->setData($p);
        Mage::register('product', $productCheck);

        $in_featured_products = array('0'=>'4','1'=>'7'); // these are child products ids ... obviously existing products but not bundle type

        $optionRawData = array();
        $selectionRawData = array();
        $count = 0;
        foreach($in_featured_products as $_child){

            $optionRawData[$count] = array(
                'required' => 1,
                'option_id' => '',
                'position' => 0,
                'type' => 'select',
                'title' => 'test product option',
                'default_title' => 'test product option',
                'delete' => '',
            );

            $selectionRawData[$count][] = array(
                'product_id' => $_child,
                'selection_qty' => 1,
                'selection_can_change_qty' => 1,
                'position' => 0,
                'is_default' => 1,
                'selection_id' => '',
                'selection_price_type' => 0,
                'selection_price_value' => 0.0,
                'option_id' => '',
                'delete' => ''
            );
            $count++;
        }
        Mage::register('productCheck', $productCheck);
        Mage::register('current_product', $productCheck);
        $productCheck->setCanSaveConfigurableAttributes(false);
        $productCheck->setCanSaveCustomOptions(true);
        // Set the Bundle Options & Selection Data
        $productCheck->setBundleOptionsData($optionRawData);
        $productCheck->setBundleSelectionsData($selectionRawData);
        $productCheck->setCanSaveBundleSelections(true);
        $productCheck->setAffectBundleProductSelections(true);
        $productCheck->save();