代码之家  ›  专栏  ›  技术社区  ›  bonez

使用Magento创建捆绑产品

  •  1
  • bonez  · 技术社区  · 13 年前

    我一直在跟随这个例子: Magento programmaticaly create bundle Product

    当我创建一个新产品时,代码是有效的,然而,当我加载一个捆绑或简单的产品时,我无法使它工作。[EDIT]我可以通过下面的代码加载我以编程方式创建的捆绑产品,并将产品添加到捆绑包中。我通过GUI创建的捆绑产品也无法添加产品。

    知道我如何加载一个产品,然后将其与另一个产品捆绑在一起吗?

    这是我当前的代码:

        $items = array();
    $items[] = array(
        'title'     => 'Bundle Option',
        'option_id' => '',
        'delete'    => '',
        'type'      => 'radio',
        'required'  => 1,
        'position'  => 0,
    );
    
    $selectionRawData = array();
    $selectionRawData[0] = array();
    $selectionRawData[0][] = array(
        'selection_id'             => '',
        'option_id'                => '',
        'product_id'               => 3,
        'delete'                   => '',
        'selection_price_value'    => 0,
        'selection_price_type'     => 0,
        'selection_qty'            => 1,
        'selection_can_change_qty' => 0,
        'position'                 => 0,
        'is_default'               => 1,
    );
    
    $selections = $selectionRawData;
    
    $websiteIDs = array(1);
    $cats = array(4);
    
    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    
    // load product
    // NOT WORKING
    $product = Mage::getModel('catalog/product');
    $product->load(127);
    
    
    // new product
    /******
     * THIS WORKS
    
    $p = array(
                'sku_type' => 1,
                'sku' => '123321',
                'name' => "BarProduct",
                'description' => 'Foo',
                'short_description' => 'Bar',
                'type_id' => 'bundle',
                'attribute_set_id' => 4,
                'weight_type' => 0,
                'visibility' => 4,
                'price_type' => 1,
                'price_view' => 0,
                'price' => 1.99,
                'has_options' => 1,
                'required_options' => 1,
                'status' => 1,
                'created_at' => strtotime('now'),
                'category_ids' => $cats,
                'store_id' => 0,
                'website_ids' => $websiteIDs,
                'weight' => 0,
                'weight_type' => 1,
                'delivery_time' => '',
                'generate_meta' => 1,
                'tax_class_id' => 1, //19%
        );
        $product->setData($p);
    
    *****/ 
    
    Mage::register('product', $product);
    Mage::register('current_product', $product);
    
    $product->setCanSaveConfigurableAttributes(false);
    $product->setCanSaveCustomOptions(true);
    
    $product->setBundleOptionsData($items);
    $product->setBundleSelectionsData($selections);
    $product->setCanSaveCustomOptions(true);
    $product->setCanSaveBundleSelections(true);
    $product->setAffectBundleProductSelections(true);
    
    $product->save();
    
    $result['product_name'] = $product->getId();
    return $result;
    
    3 回复  |  直到 9 年前
        1
  •  1
  •   Houssem BDIOUI    13 年前

    这对于捆绑产品非常重要:

    $product->setData("price_type", 0);
    

    在保存之前,必须将此属性设置为0(动态价格)。 当然,重新编制索引是必要的。

        2
  •  0
  •   bonez    13 年前

    我的产品指数有些古怪。我删除了我所有的产品并修复了我的索引,现在这很有效,尽管不太好。

    以下是我从这个过程中收集到的信息:

    如果你想把两个简单的产品捆绑在一起,你需要通过 $p = array 上面的代码,然后添加两个简单的产品。

    否则,您将需要通过magento gui预先制作的捆绑产品。那么您将不得不使用$product->load(product_id)命令,然后将您的简单产品添加到该命令中。

        3
  •  0
  •   wutzebaer    13 年前

    只需删除所有不需要的选项,如下所示:

    $optionsselectionsmap = array();
            $options = Mage::getModel("bundle/option")->getCollection()->setProductIdFilter($product->getId());
            foreach($options as $option){
                $selection = Mage::getModel("bundle/selection")->getCollection()->setOptionIdsFilter($option->getId())->getFirstItem();
                $tmp = array();
                $tmp['option_id'] = $option->getId();
                $tmp['selection_id'] = $selection->getData('selection_id');
                $optionsselectionsmap[$selection->getData('sku')] = $tmp;
            }
    
    $deleteoptionids = array();
            foreach($optionsselectionsmap as $k=>$v) $deleteoptionids[] = $v['option_id'];
            foreach($product->getTypeInstance(true)->getOptionsCollection($product) as $deleteitem){
                $deleteitem = $deleteitem->getData();
                $deleteitem['delete'] = 1;
                $bundleOptions[] = $deleteitem;
            }
    
    推荐文章