how to Programmatically create bundle products in Magento

how to Programmatically create bundle products in Magento

In thsi post we will show you, how to Programmatically create bundle products in Magento. hear we are going to show you code how to create bundle products Programmatically in Magento.

Pass you data link name, sku, color, weight, etc information to create Easily a bundle product programmatically in Magento.

$add_bundle_Product = Mage::getModel('catalog/product');
// add data for Programmatically create bundle products in Magento
$add_bundle_Product
		->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID) 
		//you can set data in store scope
		
        ->setWebsiteIds(array(1))  
		// add your website ID the product is assigned to, as an array
		
        ->setAttributeSetId(21)  
		// add your ID of a attribute set named 'default'
		
        ->setTypeId('bundle')  
		// add your product type
		
        ->setCreatedAt(strtotime('now'))  
		// add your product creation time
		
		//  ->setUpdatedAt(strtotime('now'))  
		// add your product update time
		
        ->setSkuType(0)  
		// add your SKU type (0 - dynamic, 1 - fixed)
		
        ->setSku('onlinecode-org') 
		// add your sku
		
        ->setName('test bundle onlinecode')  
		// add your product name
		
        ->setWeightType(0)  
		// add your weight type (0 - dynamic, 1 - fixed)
		
		// ->setWeight(8.0000)
		
        ->setShipmentType(0)  
		// add your shipment type (0 - together, 1 - separately)
		
        ->setStatus(1)  
		// add your product status (1 - enabled, 2 - disabled)
		
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)  
		// add your catalog and search visibility
		
        ->setManufacturer(17)  
		// add your manufacturer id
		
        ->setColor(24)
		
        ->setNewsFromDate('01/01/2017')  
		// add your product set as new from
		
        ->setNewsToDate('01/01/2017')  
		// add your product set as new to
		
        ->setCountryOfManufacture('AF')  
		// add your country of manufacture (2-letter country code)
		
        ->setPriceType(0)  
		// add your price type (0 - dynamic, 1 - fixed)
		
        ->setPriceView(0)  
		// add your price view (0 - price range, 1 - as low as)
		
        ->setSpecialPrice(00.43)  
		// add your special price in form 11.33
		
        ->setSpecialFromDate('01/01/2017') 
		// add your special price from (MM-DD-YYYY)
		
        ->setSpecialToDate('01/01/2017')  
		// add your special price to (MM-DD-YYYY)
		/*only available if price type is 'fixed'*/
			
//        ->setPrice(11.33)  
// add your price, works only if price type is fixed

//        ->setCost(33.33)  
// add your price in form 11.33

//        ->setMsrpEnabled(1)  
// add your enable MAP

//        ->setMsrpDisplayActualPriceType(1)  
// add your display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)

			// ->setMsrp(99.88)  
// add your Manufacturer's Suggested Retail Price

			// ->setTaxClassId(4)  
// add your tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)

        /* foe price type :: only available if "price type" is 'fixed' */
		
        ->setMetaTitle('onlinecode meta title 2') 
		// add your meta title
		
        ->setMetaKeyword('onlinecode meta keyword 2') 
		// add your meta title
		
        ->setMetaDescription('onlinecode meta description 2') 
		// add your meta  meta description
		
        ->setDescription('onlinecode a long description') 
		// add your meta long description
		
        ->setShortDescription('This is a short description') 
		// add your meta short description
		
        ->setMediaGallery(array('images' => array(), 'values' => array())) 
		// add your media gallery initialization
		
        ->setStockData(array(
                'use_config_manage_stock' => 1, 
				// add your 'Use config settings' checkbox (0 = no or 1 =yes )
                'manage_stock' => 1, 
				// add your manage stock (0 = no or 1 =yes )
                'is_in_stock' => 1, 
				// add your Stock Availability (0 = no or 1 =yes )
            )
        )
        ->setCategoryIds(array(10, 4)); 
		// add your assign product to categories
 
    $bundle_options = array();
    $bundle_options = array(
        '0' => array( // add your option id (0, 1, 2, etc)
            'title' => 'onlinecode title1', // add your option title
            'option_id' => '',
            'delete' => '',
            'type' => 'select', // add your option type
            'required' => '1', // add your is option required
            'position' => '1' // add your option position
        ),
        '1' => array(
            'title' => 'onlinecode title2',
            'option_id' => '',
            'delete' => '',
            'type' => 'multi',
            'required' => '1',
            'position' => '1'
        )
    );
 
    $bundle_selections = array();
    $bundle_selections = array(
        '0' => array( // add your option ID
            '0' => array( // add your selection ID of the option (first product under this option (option ID) would have ID of 0, second an ID of 1, etc)
                'product_id' => '119', // add your if of a product in selection
                'delete' => '',
                'selection_price_value' => '17',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            ),
 
            '1' => array(
                'product_id' => '118',
                'delete' => '',
                'selection_price_value' => '17',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            )
        ),
        '1' => array( // add your option ID
            '0' => array(
                'product_id' => '117',
                'delete' => '',
                'selection_price_value' => '17',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            ),
 
            '1' => array(
                'product_id' => '116',
                'delete' => '',
                'selection_price_value' => '17',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            )
        )
    );
	
    // add your flags for saving custom options/selections
    $add_bundle_Product->setCanSaveCustomOptions(true);
    $add_bundle_Product->setCanSaveBundleSelections(true);
    $add_bundle_Product->setAffectBundleProductSelections(true);
 
    // add your registering a product because of Mage_Bundle_Model_Selection::_beforeSave
    Mage::register('product', $add_bundle_Product);
 
    // add your setting the bundle options and selection data
    $add_bundle_Product->setBundleOptionsData($bundle_options);
    $add_bundle_Product->setBundleSelectionsData($bundle_selections);
 
    $add_bundle_Product->save();
    echo 'Programmatically create bundle products added success in Magento.';
} 
catch (Exception $exception) 
{
    Mage::log($exception->getMessage());
    echo $exception->getMessage();
}

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  11  =  14

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US