Programatically added bundle product isn't showing up in frontend

后端 未结 2 1971
说谎
说谎 2020-12-28 23:13

I am trying to insert bundled products to the Magento database from a PHP script. The version in question is Community 1.5.1.0.

I tried the method described in the

相关标签:
2条回答
  • 2020-12-28 23:53

    Please try using the following code & see what happens:-

    <?php
    $magentoPath = '/home/nikola/bin/magento-1.5/';
    require_once($magentoPath . 'includes/config.php');
    require_once($magentoPath . 'app/Mage.php');
    
    $storeID = 1;
    $websiteIDs = array(1);
    $cats = array("210");
    
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    
    $product = Mage::getModel('catalog/product');
    
    $p = array(
      'sku_type' => 0,
      'sku' => '687',
      'name' => "BarProduct",
      'description' => 'Foo',
      'short_description' => 'Bar',
      'type_id' => 'bundle',
      'attribute_set_id' => 4,
      'weight_type' => 0,
      'visibility' => 4,
      'price_type' => 0,
      'price_view' => 0,
      'status' => 1,
      'created_at' => strtotime('now'),
      'category_ids' => $cats,
      'store_id' => $storeID,
      'website_ids' => $websiteIDs
    );
    
    $product->setData($p);
    Mage::register('product', $product);
    Mage::register('current_product', $product);
    
    /**
     * Section of Bundle Options
     * 
     * Required Properties of Bundle Options are:-
     * 1. title
     * 2. option_id
     * 3. delete
     * 4. type
     * 5. required
     * 6. position
     * 7. default_title
     */
    $optionRawData = array();
    $optionRawData[0] = array(
      'required' => 1,
      'option_id' => '',
      'position' => 0,
      'type' => 'select',
      'title' => 'FooOption',
      'default_title' => 'FooOption',
      'delete' => '',
    );
    
    /**
     * Section of Bundle Selections
     * 
     * Required Properties of Bundle Selections
     * 1.   selection_id
     * 2.   option_id
     * 3.   product_id
     * 4.   delete
     * 5.   selection_price_value
     * 6.   selection_price_type
     * 7.   selection_qty
     * 8.   selection_can_change_qty
     * 9.   position
     * 10.  is_default
     */
    $selectionRawData = array();
    $selectionRawData[0] = array();
    $selectionRawData[0][] = array(
      'product_id' => 1810,
      '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' => ''
    );
    
    $product->setCanSaveConfigurableAttributes(false);
    $product->setCanSaveCustomOptions(true);
    
    // Set the Bundle Options & Selection Data
    $product->setBundleOptionsData($optionRawData);
    $product->setBundleSelectionsData($selectionRawData);
    $product->setCanSaveBundleSelections(true);
    $product->setAffectBundleProductSelections(true);
    
    $product->save();
    ?>
    

    Hope it helps.

    0 讨论(0)
  • 2020-12-29 00:00

    I have tried using your code, but it did not seem to work in Magento 1.7.0.2. Apparently the product could not be saved.

    What I did was added the following lines:

     Mage::register('product', $product);
     Mage::register('current_product', $product);
     $product->setCanSaveConfigurableAttributes(false);
     $product->setCanSaveCustomOptions(true);
    

    Just before the lines:

    // Set the Bundle Options & Selection Data
    $product->setBundleOptionsData($optionRawData);
    $product->setBundleSelectionsData($selectionRawData);
    $product->setCanSaveBundleSelections(true);
    $product->setAffectBundleProductSelections(true);
    
    $product->save();
    

    This seemed to fix the issue of not being able to save the file.

    0 讨论(0)
提交回复
热议问题