我的客户要求在他们销售的捆绑产品(服装顶部和底部)中的每个简单产品在用户添加时作为购物车中的单独行项目添加。有谁可以指导我如何完成这个任务吗?我对MVC和Zend Framework相当在行,但我需要一些帮助来找到控制将捆绑的产品添加到购物车中的确切文件,或者使用另一种方法来单独添加这些项目。请假设此服装的唯一可能产品类型是捆绑产品类型。
发布于 2012-03-29 05:40:53
你需要一个观察者:
<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);
}
}这只是一个简单的示例,但它可能会有所帮助。
当通过代码使用捆绑产品时,它可能很难理解:以下是一个示例图像:

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

在购物车中,一旦你点击“Add to cart”,它可能看起来像这样: Bundle Sample
Sample Shampoo
1 x Moisturiser-125ml $29.95
Default Conditioner
1 x Moisturiser-60g $99.95在通过代码查询此产品时,您可以像加载任何普通产品一样加载它:
$oProduct->load($vProductId);加载后,您需要获取一个产品类型实例,以便可以加载此产品类型的选项。
$oTypeInstance = $oProduct->getTypeInstance(true);现在,我们可以通过以下方式获取该产品的选项ID列表:
$oTypeInstance = $oProduct->getTypeInstance(true);要询问选择,我们将把选项列表添加到集合中,然后获取选项集合以及它们各自的选择:
$aSelections = $oTypeInstance->getSelectionsCollection($aOptionIds, $oProduct );
$aOptions = $oTypeInstance->getOptionsByIds($aOptionIds, $oProduct);
$bundleOptions = $aOptions->appendSelections($aSelections, true);现在我们有了一个选项集合,每个选项都有一个选择集合。我们现在可以遍历这些选项,并查看它们的选择。
foreach ($bundleOptions as $bundleOption) {
if ($bundleOption->getSelections()) {
$bundleSelections = $bundleOption->getSelections();
foreach ($bundleSelections as $bundleSelection) {
// get some data here
$vName = $bundleOption->getTitle();
}
}
}要获取捆绑产品所需的选择产品列表,可以使用以下代码:
$requiredChildren = $this->getChildrenIds($product->getId(),$required=true);然后,您可以遍历ID数组并通过ID加载产品,以获得有关这些产品的更多信息。
购物车中捆绑的产品
为了遍历购物卡中捆绑产品的所选选项,您可以使用如下代码:
/**
* @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捆绑的Product中获取选项,然后在集合中获取该产品的选项,然后查找“Selected”选项。
hth,肖恩
发布于 2019-01-25 08:16:53
下面是如何在Magento 2.3中通过Magento\Checkout\Model\Cart->addProduct()方法的“环绕”插件(拦截器)来实现这一点。
当客户将产品添加到购物车时,将调用addProduct()方法。通过插件将代码添加到此方法中,您可以更改将捆绑产品添加到购物车的方式。
在Vendor/Module/etc/frontend/di.xml:中定义
<?xml version="1.0"?>
<!--
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="\Magento\Checkout\Model\Cart">
<plugin name="add-bundle-products-separate" type="Vendor\Module\Plugin\Checkout\Model\CartPlugin" sortOrder="1"/>
</type>
</config>这就是为什么Magento会为这个特殊的方法提供一个插件。
参考:https://devdocs.magento.com/guides/v2.3/extension-dev-guide/plugins.html
中
<?php
namespace Vendor\Module\Plugin\Checkout\Model;
use \Magento\Catalog\Model\Product;
use \Magento\Framework\DataObject;
use \Magento\Checkout\Model\Cart;
/**
* Class CartPlugin
*
* @package Ppwd\CrossSell\Plugin\Checkout\Model
*
*/
class CartPlugin {
/**
* @param Cart $subject
* @param \Closure $proceed
* @param Product $productInfo
* @param DataObject|int|array $requestInfo
* @return Cart $subject
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function aroundAddProduct(
$subject,
$proceed,
$productInfo,
$requestInfo = null
) {
// Detect if we are adding a bundle product to cart
if (!is_numeric($productInfo) && $productInfo->getTypeId() == 'bundle') {
$buyRequest = new DataObject($requestInfo);
// List of products selected as part of the bundle
$cartCandidates = $productInfo->getTypeInstance()->prepareForCartAdvanced($buyRequest, $productInfo);
$productIds = [];
// Add each item in bundle as if it were separately added to cart
/** @var Product $cartCandidate */
foreach ($cartCandidates as $cartCandidate) {
if ($cartCandidate->getTypeId() != 'bundle') {
for ($i = 0; $i < $cartCandidate->getCartQty(); $i++) {
$productIds[] = $cartCandidate->getId();
}
}
}
$subject->addProductsByIds($productIds);
return $subject;
}
// Return original result from addProduct() as if plugin didn't exist
$result = $proceed($productInfo, $requestInfo);
return $result;
}
}完成后,如果您将捆绑添加到购物车中,行项目将单独出现,而不是像捆绑产品通常那样组合在一起。
发布于 2015-02-13 02:29:47
我这样做是为了解决客户将捆绑内容作为单独项目添加到购物车中的请求。只需替换
$cart->addProduct($product, $params) 使用
if ($product->getTypeId() == 'bundle') {
$request = new Varien_Object($params);
$cartCandidates = $product->getTypeInstance(true)->prepareForCartAdvanced($request, $product, Mage_Catalog_Model_Product_Type_Abstract::PROCESS_MODE_FULL);
$idstoadd = array();
foreach ($cartCandidates as $cartCandidate) {
if ($cartCandidate->getTypeId() == 'simple') {
for ($i = 0; $i < $cartCandidate->getCartQty(); $i++) {
$idstoadd[] = $cartCandidate->getId();
}
}
}
$cart->addProductsByIds($idstoadd);
} else {
$cart->addProduct($product, $params);
}在文件cartController.中
https://stackoverflow.com/questions/9915427
复制相似问题