本文介绍了Magento:将捆绑中的简单产品添加到购物车中的单独行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的客户要求在用户添加时,将他们销售的捆绑产品中的每个简单产品(上衣和下衣)作为单独的行项目添加到购物车中.谁能指导我如何做到这一点?我对 MVC 和 Zend 框架相当擅长,但我需要一些帮助来找到控制将捆绑产品添加到购物车的确切文件,或者另一种方法来单独添加这些项目.请假设此服装唯一可能的产品类型是捆绑产品类型.

My client is requesting that each simple product within a bundled product they are selling (Clothing Top and Bottom) be added as a separate line item in the cart whenever a user adds it. Can anyone direct me in how to accomplish this? I am fairly good with MVC and the Zend Framework, but I need a bit of help finding the exact files that control adding bundled products to the cart, or an alternate method for getting these items added separately. Please assume that the only possible product type for this clothing is the Bundled product type.

推荐答案

你需要一个观察者:

    <checkout_cart_product_add_after>
        <observers>
            <reporting>
                <type>singleton</type>
                <class>Yourcompany_yourmodelname_Model_Observer</class>
                <method>onCartProductAdd</method>
            </reporting>
        </observers>
    </checkout_cart_product_add_after>

然后做观察者:

class ourcompany_yourmodelname_Model_Observer extends Mage_Core_Model_Abstract
{
    /**
     * Binds to the checkout_cart_product_add_after Event and passes control to the helper function to process the quote
     *
     * @param Varien_Event_Observer $observer
     * @return void
     */
    public function onCartProductAdd($observer){
        $product = $observer->getProduct();
        $isProductBundle = ($product->getTypeId() == 'bundle');
        $items_to_add = array();
        $oTypeInstance = $oProduct->getTypeInstance(true);
        $aSelections = $oTypeInstance->getSelectionsCollection($aOptionIds, $product );
        $aOptions = $oTypeInstance->getOptionsByIds($aOptionIds, $product);
        $bundleOptions = $aOptions->appendSelections($aSelections, true);
        foreach ($bundleOptions as $bundleOption) {
            if ($bundleOption->getSelections()) {
                $bundleSelections = $bundleOption->getSelections();
                foreach ($bundleSelections as $bundleSelection) {
                       $items_to_add[] = $bundleSelection.getID();

               }

            }
       }
       insertExtractedProducts($items_to_add);
    }

     /**
     * Add extracted products into quote
     *
     * @param array $items_to_add
      */
    public function insertExtractedProducts($items_to_add){
        /**@var $cart Mage_Checkout_Model_Cart**/
        $cart = Mage::helper('checkout/cart')->getCart();
        $ids_to_add = array();
        foreach($items_to_add as $item_to_be_added){
            $ids_to_add[] = $item_to_be_added->getProductId();
        }
        $cart->addProductsByIDs($ids_to_add);
        $cart->save();
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        }
   }

只是一个简单的示例,但可能会有所帮助.

Just a simple sample, but it might help.

捆绑产品在通过代码使用时可能会很复杂,难以理解:这是一个示例图像:

Bundled products can be complicated to understand, when working with it via code:Here is a sample image:

每个捆绑产品都有一对多的选项,最终将是要添加到购物车中捆绑产品的链接.

Each Bundled product, have one to many options, which in the end will be links to products to be added to the bundle in the Shopping Cart.

每个选项由一对多的选择组成,这些选择将是最终出现在购物车中的链接产品,位于此捆绑产品下.一个选择,通常可以设置为默认值,并且已经在产品页面上被选中.可以在此链接中找到有关如何创建和配置捆绑产品的更多信息,因为在本文档中,我们将只讨论它的编程方面.捆绑产品的显示页面将如下所示:

Each Option consists of one to many Selections, which will be the linked products that will end up in the Shopping cart, under this bundled product. One Selection, can typically be set as the default, and will already be selected on the product page. More information can be found at this link on how to create and configure bundled products, because in this document, we will only discuss the programming side of it.The bundled product’s display page, will look like this:

点击添加到购物车"后,它在购物车中可能看起来像这样:捆绑样品

It could look like this in the shopping cart, once you clicked on "Add to Cart":Bundle Sample

Sample Shampoo
1 x Moisturiser-125ml $29.95
Default Conditioner
1 x Moisturiser-60g $99.95

当通过代码查询这个产品时,你像任何普通产品一样加载它:

When interrogating this product through code, you load it like any normal product:

$oProduct->load($vProductId);

一旦加载完毕,您需要获取一个产品类型实例,以便您可以加载该产品类型的选项.

Once it is loaded, you need to get a product type instance, so that you can load the options for this product type.

$oTypeInstance = $oProduct->getTypeInstance(true);

现在我们可以通过这种方式获取此产品的选项 ID 列表:

Now we can get the list of option ID’s for this product in this manner:

$oTypeInstance = $oProduct->getTypeInstance(true);

为了查询选择,我们将选项列表添加到集合中,然后获取选项集合,以及它们各自的选择:

To interrogate the Selections, we will add the list of options to a collection, then get the Options collection, as well as their respective Selections:

$aSelections = $oTypeInstance->getSelectionsCollection($aOptionIds, $oProduct );
$aOptions = $oTypeInstance->getOptionsByIds($aOptionIds, $oProduct);
$bundleOptions = $aOptions->appendSelections($aSelections, true);

现在我们有一个选项集合,每个选项都有一个选择集合.我们现在可以遍历选项,并查看它们的选择.

Now we have a Collection of Options, and each Option will have a collection of Selections. We can now loop through the options, and look at their Selctions.

foreach ($bundleOptions as $bundleOption) {
            if ($bundleOption->getSelections()) {
                $bundleSelections = $bundleOption->getSelections();

                foreach ($bundleSelections as $bundleSelection) {
                     // get some data here
                     $vName = $bundleOption->getTitle();
               }

            }
}

要获取捆绑产品的必选产品列表,您可以使用以下代码:

To get a list of the Required Selection Products for the Bundled product, you can use the following code:

$requiredChildren = $this->getChildrenIds($product->getId(),$required=true);

然后,您可以遍历 ID 数组并按 ID 加载产品,以获取有关这些产品的更多信息.

You can then loop through the array of ID’s and load the products by their ID’s to get more information regarding those products.

购物车中的捆绑产品

为了循环浏览购物卡中捆绑产品的选定选项,您可以使用以下内容:

In order to loop through the selected options of a Bundled product in the shopping card, you can use something like this:

/**
 * @var Mage_Bundle_Model_Product_Type
 */
$typeInstance = $this->getProduct()->getTypeInstance(true);

// get bundle options
$optionsQuoteItemOption =  $this->getItem()->getOptionByCode('bundle_option_ids');
$bundleOptionsIds = unserialize($optionsQuoteItemOption->getValue());
if ($bundleOptionsIds) {
    /**
    * @var Mage_Bundle_Model_Mysql4_Option_Collection
    */
    $optionsCollection = $typeInstance->getOptionsByIds($bundleOptionsIds, $this->getProduct());

    // get and add bundle selections collection
    $selectionsQuoteItemOption = $this->getItem()->getOptionByCode('bundle_selection_ids');

    $selectionsCollection = $typeInstance->getSelectionsByIds(
        unserialize($selectionsQuoteItemOption->getValue()),
        $this->getProduct()
    );

    $bundleOptions = $optionsCollection->appendSelections($selectionsCollection, true);
    foreach ($bundleOptions as $bundleOption) {
        if ($bundleOption->getSelections()) {
            $label = $bundleOption->getTitle()

            $bundleSelections = $bundleOption->getSelections();

            foreach ($bundleSelections as $bundleSelection) {
                    $sName = $bundleSelection->getName();
            }
            // some more code here to do stuff
        }
    }
}

此代码从 Quote Item Bundled Product 中获取 Options,然后在一个集合中获取该产品的 Options,然后找到Selected"Option Selection.

This code gets the Options from the Quote Item Bundled Product, and then gets the Options for that product in a collection, and then finds the "Selected" Option Selection.

嗯,肖恩

这篇关于Magento:将捆绑中的简单产品添加到购物车中的单独行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 05:07