Magento中以编程的方式创建捆绑产品

redmaomail 2024-07-24 18:12 阅读数 68 #Magento

红帽云邮外贸主机

如果说这是一个系列文章,那么这就是第三个了。如果你之前看过了Magento中简单产品和可配置产品的创建,那你已经了解了基础。

这篇文章中我们将扩展知识,学习如何用编程的方式创建Magento的捆绑产品。

捆绑产品就是你想要批量销售的产品。从图片中可能会让你有更好的了解。我们以Magento示例数据中的一个产品为例:

magento在首页显示登陆注册框,创建magento下拉登陆框

我们可以看到这个产品的每个自定义选项值都会对应一个捆绑相关的简单产品。例如,Camera选项有 Madison LX2200和Madison RX3400,它们对应不同的简单产品。现在我们跳过基础部分,我们将创建一个捆绑产品必要的两个选项,每一个选项有两个值来表示不同的产品。

$bundleProduct = Mage::getModel('catalog/product');
    $bundleProduct
    ->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID) //you can set data in store scope
        ->setWebsiteIds(array(1)) //website ID the product is assigned to, as an array
        ->setAttributeSetId(20) //ID of a attribute set named 'default'
        ->setTypeId('bundle') //product type
        ->setCreatedAt(strtotime('now')) //product creation time
//    ->setUpdatedAt(strtotime('now')) //product update time
        ->setSkuType(0) //SKU type (0 - dynamic, 1 - fixed)
        ->setSku('bundlexx1') //SKU
        ->setName('test bundle product96') //product name
        ->setWeightType(0) //weight type (0 - dynamic, 1 - fixed)
//        ->setWeight(4.0000)
        ->setShipmentType(0) //shipment type (0 - together, 1 - separately)
        ->setStatus(1) //product status (1 - enabled, 2 - disabled)
        ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH) //catalog and search visibility
        ->setManufacturer(28) //manufacturer id
        ->setColor(24)
        ->setNewsFromDate('06/26/2014') //product set as new from
        ->setNewsToDate('06/30/2014') //product set as new to
        ->setCountryOfManufacture('AF') //country of manufacture (2-letter country code)
        ->setPriceType(0) //price type (0 - dynamic, 1 - fixed)
        ->setPriceView(0) //price view (0 - price range, 1 - as low as)
        ->setSpecialPrice(00.44) //special price in form 11.22
        ->setSpecialFromDate('06/1/2014') //special price from (MM-DD-YYYY)
        ->setSpecialToDate('06/30/2014') //special price to (MM-DD-YYYY)
        /*only available if price type is 'fixed'*/
//        ->setPrice(11.22) //price, works only if price type is fixed
//        ->setCost(22.33) //price in form 11.22
//        ->setMsrpEnabled(1) //enable MAP
//        ->setMsrpDisplayActualPriceType(1) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
//        ->setMsrp(99.99) //Manufacturer's Suggested Retail Price
//        ->setTaxClassId(4) //tax class (0 - none, 1 - default, 2 - taxable, 4 - shipping)
        /*only available if price type is 'fixed'*/
        ->setMetaTitle('test meta title 2')
        ->setMetaKeyword('test meta keyword 2')
        ->setMetaDescription('test meta description 2')
        ->setDescription('This is a long description')
        ->setShortDescription('This is a short description')
        ->setMediaGallery(array('images' => array(), 'values' => array())) //media gallery initialization
        ->setStockData(array(
                'use_config_manage_stock' => 1, //'Use config settings' checkbox
                'manage_stock' => 1, //manage stock
                'is_in_stock' => 1, //Stock Availability
            )
        )
        ->setCategoryIds(array(4, 10)); //assign product to categories
 
    $bundleOptions = array();
    $bundleOptions = array(
        '0' => array( //option id (0, 1, 2, etc)
            'title' => 'item01', //option title
            'option_id' => '',
            'delete' => '',
            'type' => 'select', //option type
            'required' => '1', //is option required
            'position' => '1' //option position
        ),
        '1' => array(
            'title' => 'item02',
            'option_id' => '',
            'delete' => '',
            'type' => 'multi',
            'required' => '1',
            'position' => '1'
        )
    );
 
    $bundleSelections = array();
    $bundleSelections = array(
        '0' => array( //option ID
            '0' => array( //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' => '554', //if of a product in selection
                'delete' => '',
                'selection_price_value' => '10',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            ),
 
            '1' => array(
                'product_id' => '553',
                'delete' => '',
                'selection_price_value' => '10',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            )
        ),
        '1' => array( //option ID
            '0' => array(
                'product_id' => '552',
                'delete' => '',
                'selection_price_value' => '10',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            ),
 
            '1' => array(
                'product_id' => '551',
                'delete' => '',
                'selection_price_value' => '10',
                'selection_price_type' => 0,
                'selection_qty' => 1,
                'selection_can_change_qty' => 0,
                'position' => 0,
                'is_default' => 1
            )
        )
    );
    //flags for saving custom options/selections
    $bundleProduct->setCanSaveCustomOptions(true);
    $bundleProduct->setCanSaveBundleSelections(true);
    $bundleProduct->setAffectBundleProductSelections(true);
 
    //registering a product because of Mage_Bundle_Model_Selection::_beforeSave
    Mage::register('product', $bundleProduct);
 
    //setting the bundle options and selection data
    $bundleProduct->setBundleOptionsData($bundleOptions);
    $bundleProduct->setBundleSelectionsData($bundleSelections);
 
    $bundleProduct->save();
    echo 'success';
} catch (Exception $e) {
    Mage::log($e->getMessage());
    echo $e->getMessage();
}

你可以在每行代码找到注释。我尽可能多地注释,这样有利于你们去理解。

电商网站开发服务


红帽云邮外贸主机

分享到:
版权声明:本站内容源自互联网,如有内容侵犯了你的权益,请联系删除相关内容。
    红帽云邮外贸主机
热门
    红帽云邮外贸主机
    红帽云邮外贸主机