【问题标题】:Magento : Programmatically create order bundled products and configurable productsMagento:以编程方式创建订单捆绑产品和可配置产品
【发布时间】:2012-05-18 16:57:43
【问题描述】:

我尝试使用Vinai 中的以下代码创建订单,但它仅适用于简单产品。我已经尝试过使用 $buyInfo 中的键和值,但似乎订单不会继续。我可能遗漏了什么?

$quote = Mage::getModel('sales/quote')
        ->setStoreId(Mage::app()->getStore('default')->getId());

if ('do customer orders') {
        // for customer orders:
        $customer = Mage::getModel('customer/customer')
                ->setWebsiteId(1)
                ->loadByEmail('customer@example.com');
        $quote->assignCustomer($customer);
} else {
        // for guesr orders only:
        $quote->setCustomerEmail('customer@example.com');
}

// add product(s)
$product = Mage::getModel('catalog/product')->load(8);
$buyInfo = array(
        'qty' => 1,
        // custom option id => value id
        // or
        // configurable attribute id => value id
);
$quote->addProduct($product, new Varien_Object($buyInfo));

$addressData = array(
        'firstname' => 'Test',
        'lastname' => 'Test',
        'street' => 'Sample Street 10',
        'city' => 'Somewhere',
        'postcode' => '123456',
        'telephone' => '123456',
        'country_id' => 'US',
        'region_id' => 12, // id from directory_country_region table
);

$billingAddress = $quote->getBillingAddress()->addData($addressData);
$shippingAddress = $quote->getShippingAddress()->addData($addressData);

$shippingAddress->setCollectShippingRates(true)->collectShippingRates()
                ->setShippingMethod('flatrate_flatrate')
                ->setPaymentMethod('checkmo');

$quote->getPayment()->importData(array('method' => 'checkmo'));

$quote->collectTotals()->save();

$service = Mage::getModel('sales/service_quote', $quote);
$service->submitAll();
$order = $service->getOrder();

知道如何让它在捆绑和可配置的产品中也能正常工作吗?谢谢!

【问题讨论】:

    标签: magento


    【解决方案1】:

    要制作捆绑产品,您必须将 prodtypeID 设置为“捆绑”

    这是我正在使用的代码:

        $__magproduct = new Mage_Catalog_Model_Product();
    
    
    
        // Build the product 
        $__magproduct->setSku(trim($sku_of_product)); 
        $__magproduct->setAttributeSetId('4');# 4 is for default 
        $__magproduct->setTypeId($prodtype); 
        $__magproduct->setName($getprodId->ProductName); 
        //$__magproduct->setCategoryIds(array(42)); # some cat id's, 
        $__magproduct->setWebsiteIDs(array(1)); # Website id, 1 is default 
        //$__magproduct->setDescription($getprodId->Description); 
        //$__magproduct->setShortDescription($getprodId->Description); 
        $__magproduct->setPrice($prodprice); # Set some price
    
        //Default Magento attribute 
        $__magproduct->setWeight($getprodId->Weight); 
    
        $__magproduct->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); 
        $__magproduct->setStatus(1); 
        $__magproduct->setTaxClassId(2); # default tax class 
    
    
        $__magproduct->setCreatedAt(strtotime('now')); 
    
        try { 
        $__magproduct->save(); 
    
        if (!($stockItem = $product->getStockItem())) {
                    $stockItem = Mage::getModel('cataloginventory/stock_item');
                    $stockItem->assignProduct($__magproduct)
                              ->setData('stock_id', 1)
                              ->setData('store_id', 1);
                }
    
                $stockItem->setData('qty', $qty)
                          ->setData('is_in_stock', $isinstock)
                          ->setData('manage_stock', 1)
                          ->setData('use_config_manage_stock', 0)
                          ->save();
    
            unset($__magproduct,$stockItem);
    

    然后插入一个捆绑的选项:

    $productCheck = Mage::getModel('catalog/product')->loadByAttribute('sku',$getprodId->Code);         
    Mage::register('product', $productCheck);
    
    
    
    
    
    
    
    
    
              $position = 0;
    
              $optionRawData = array();
    
              $selectionRawData = array();
              $selectionRawData[0] = array();
    
             foreach ($materials as $material) {
                    $material = (array) $material;
    
                    $komponenta = $client->GetProductSimple($material["ProductID"]);
                    $komponenta = (array) $komponenta;
    
                    echo "&nbsp;&nbsp;&nbsp;assigned: ".$komponenta["Code"]." ".$komponenta["ProductName"]." ks(".$material["Count"].")<br>\n";  
    
                    $itemSKU = Mage::getModel('catalog/product')->getIdBySku(trim($getprodId->Code));
                    $materialSKU = Mage::getModel('catalog/product')->getIdBySku(trim($komponenta["Code"]));
    
                    if (($itemSKU != "") && ($materialSKU != ""))
                        {
    
                    $position++;
    
    $optionRawData[$position] = array(
        'required' => 1,
        'option_id' => '',
        'position' => '',
        'type' => 'radio',
        'title' => 'Komponenta '.$komponenta["ProductName"],
        'default_title' => 'Komponenta '.$komponenta["ProductName"],
        'delete' => '',
    );
    
    
    $selectionRawData[$position][] = array(
        'product_id' => $materialSKU,
        'selection_qty' => $material["Count"],
        'selection_can_change_qty' => 0,
        'position' => 0,
        'is_default' => 1,
        'selection_id' => '',
        'selection_price_type' => 0,
        'selection_price_value' => 0.0,
        'option_id' => '',
        'delete' => ''
    );
    
    
    
    
    
    
                        };
    
                    unset($material,$komponenta,$itemSKU,$materialSKU);
    
             };
    
    
    
    
    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();
    

    捆绑物品和Configurable不一样!!

    【讨论】:

      猜你喜欢
      • 2012-11-15
      • 2013-05-05
      • 2014-08-11
      • 2013-07-19
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-04
      相关资源
      最近更新 更多