【问题标题】:Programmatically added Bundle Products in Magento don't display correctly in the frontendMagento 中以编程方式添加的捆绑产品无法在前端正确显示
【发布时间】:2012-12-01 13:45:53
【问题描述】:

首先:我使用的是 Magento 1.7.0.2。 我编写了一个函数,它可以即时创建捆绑产品并将其添加到购物车中。此产品在后端正确显示,但在前端我想从购物车中编辑此项目时缺少选项。

我还遇到了一个问题,我无法将生成的商品正确添加到购物车中。它向我显示一个错误:

Some of the products below do not have all the required options. Please edit them and configure all the required options.

如果我在后端打开生成的产品并将其保存在那里,它会正确显示前端的每个选项,但仍然出现其他错误。

到目前为止,这是我的代码:

        $storeID = 1;
        $websiteIDs = array(1);
        $cats = array( $this->categoryPresent );
        $productCounter = 0;

        $presentParams['additional'] = 171;  //Product ID
        $presentParams['box'] = 167;  //Product ID
        $presentParams['count'] = 3; //amount of ordered presents

        //Create base bundle with all required attributes
        $product = Mage::getModel('catalog/product');
        $p = array(
          'sku_type' => 0,
          'sku' => 'present - ' . strtotime('now'),
          'name' => "Present",
          'description' => 'Present',
          'short_description' => 'Present',
          'type_id' => 'bundle',
          'attribute_set_id' => 9,
          '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,
          'base_price_amount' => '',
          'base_price_base_amount' => 1,
          'base_price_base_unit' => 'St',
          'options_container' => 'container1'
        );
        $product->setData($p);
        $product->setStockData(array(
            'is_in_stock' => 1,
            'qty' => 99999
        ));

        $optionRawData = array();
        $selectionRawData = array();

        //Insert Box
        $productCounter++;
        $optionRawData[] = array(
              'required' => 1,
              'option_id' => '',
              'position' => $productCounter,
              'type' => 'select',
              'title' => 'Box',
              'default_title' => 'Box',
              'delete' => '',
            );
        $selectionRawData[$productCounter-1] = array();
        $selectionRawData[$productCounter-1][] = array(
              'product_id' => $presentParams['box'],
              'selection_qty' => 1,
              'selection_can_change_qty' => 0,
              'position' => $productCounter,
              'is_default' => 1,
              'selection_id' => '',
              'selection_price_type' => 0,
              'selection_price_value' => 0.0,
              'option_id' => '',
              'delete' => ''
        );

        //Insert Additional
        $productCounter++;
        $optionRawData[] = array(
              'required' => 1,
              'option_id' => '',
              'position' => $productCounter,
              'type' => 'select',
              'title' => 'Zubehör',
              'default_title' => 'Zubehör',
              'delete' => '',
            );
        $selectionRawData[$productCounter-1] = array();
        $selectionRawData[$productCounter-1][] = array(
              'product_id' => $presentParams['additional'],
              'selection_qty' => 1,
              'selection_can_change_qty' => 0,
              'position' => $productCounter,
              'is_default' => 1,
              'selection_id' => '',
              'selection_price_type' => 0,
              'selection_price_value' => 0.0,
              'option_id' => '',
              'delete' => ''
        );


        // Set the Bundle Options & Selection Data ; Save the present
        Mage::register('product', $product);
        Mage::register('current_product', $product);
        $product->setCanSaveConfigurableAttributes(false);
        $product->setCanSaveCustomOptions(true);
        $product->setCanSaveBundleSelections(true);
        $product->setAffectBundleProductSelections(true);
        $product->setBundleOptionsData($optionRawData);
        $product->setBundleSelectionsData($selectionRawData);
        $product->save();

        //Add Product to cart
        $cart = Mage::getSingleton('checkout/cart');

//Try an other way of setting the bundled_option            
/*$option_ids = $product->getTypeInstance(true)->getOptionsIds($product);
        $selectionCollection = $product->getTypeInstance(true)->getSelectionsCollection($option_ids, $product);
        $bundled_items = array();
        foreach($selectionCollection as $key => $option){
            //$bundled_items[$key] = $option->product_id;
            $bundled_items[$key] = 1;
        }

        $params = array(
            'product' => $product->getId(),
            'related_product' => null,
            'bundle_option' => $bundled_items,
            'qty' => $presentParams['count'],
        );*/

        $cart->addProduct( $product, $presentParams['count']);
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

我认为,只是缺少一小段代码。我希望,有人可以帮助解决这个问题。

更新

捆绑产品在前端生成。所以我发现要做的一件事就是为新产品编制索引。 这是附加代码:

//Index new item
        $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
        $stockItem->setForceReindexRequired(true);
        Mage::getSingleton('index/indexer')->processEntityAction(
            $stockItem,
            Mage_CatalogInventory_Model_Stock_Item::ENTITY,
            Mage_Index_Model_Event::TYPE_SAVE
        );

        $product->setForceReindexRequired(true)->setIsChangedCategories(true);
        Mage::getSingleton('index/indexer')->processEntityAction($product, Mage_Catalog_Model_Product::ENTITY, Mage_Index_Model_Event::TYPE_SAVE);

这样,生成的产品就会正确地列在响应类别的列表视图中。但是捆绑包的内容仍然丢失。

【问题讨论】:

  • 添加了附加信息。

标签: php magento magento-1.7 bundle


【解决方案1】:

我只想与您分享我用于创建产品的代码。
让我们将其与您的进行比较并搜索错误。

                $product = Mage::getModel('catalog/product');

                $product->setSku($sku);
                $product->setName($product_data['name']);
                $product->setDescription($product_data['description']);
                $product->setShortDescription($product_data['name']);
                $product->setPrice($product_data['price'] * $price_multiplier);
                $product->setTypeId('simple');
                $product->setAttributeSetId(9); // need to look this up
                $product->setCategoryIds(implode(',', $local_categories)); // need to look these up
                $product->setWeight(1.0);
                $product->setTaxClassId(0); // taxable goods
                $product->setVisibility(4); // catalog, search
                $product->setStatus(1); // enabled

                // assign product to the default website
                $product->setWebsiteIds(array(1));

                $product->setMediaGallery(array('images' => array(), 'values'=>array()));

                $first = true;
                foreach ($local_images as $local_image_fname) {
                    $ma = array();
                    if ($first) {
                        $first = false;
                        $ma    = array('image', 'small_image', 'thumbnail');
                    }
                    $product->addImageToMediaGallery($local_image_fname, $ma, false, false);
                }

                $stockData        = $product->getStockData();
                $stockData['qty'] = 1;
                $stockData['is_in_stock'] = 1;
                $stockData['manage_stock'] = 1;
                $stockData['use_config_manage_stock'] = 0;
                $product->setStockData($stockData); 

                $product->save();
                $product_id = $product->getId();

我明白了,你至少不要设置 TaxClassId。 还有重量。

编辑

还有一个建议。 您正在将捆绑产品添加到购物车。 为什么不设置捆绑选项? 你只设置计数。 查看如何以编程方式将捆绑产品添加到购物车的好示例:

http://inchoo.net/ecommerce/magento/programatically-add-bundle-product-to-cart-n-magento/

$params = array(
    'product' => 164,
    'related_product' => null,
    'bundle_option' => array(
        21 => 58,
        20 => 55,
        11 => 28,
        12 => array(
            0 => 31,
        ),
        13 => array(
            0 => 32,
            1 => 35,
        ),
    ),
    'options' => array(
        3 => 'olaaaaaaaa',
    ),
    'qty' => 2,
);
$cart = Mage::getSingleton('checkout/cart');
$product = new Mage_Catalog_Model_Product();
$product->load(164);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$message = $this->__('Custom message: %s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);

【讨论】:

  • 我已经找到了你发布的这个例子。问题是 - 我如何确定选项?或者 - bundle_option 和 options 有什么区别?添加的产品都是简单的产品。他们自己别无选择。还是我错过了一点?捆绑产品的重量也是动态的。设置它可能会停用此功能。
猜你喜欢
  • 1970-01-01
  • 2011-09-03
  • 2012-05-18
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
相关资源
最近更新 更多