nne*_*son 6 php model-view-controller zend-framework magento e-commerce
我的客户要求他们销售的捆绑产品中的每个简单产品(服装上装和下装)在用户添加时作为单独的订单项添加到购物车中.任何人都可以指导我如何实现这一目标吗?我对MVC和Zend Framework相当不错,但是我需要一些帮助来找到控制将捆绑产品添加到购物车的确切文件,或者另外一种方法来分别添加这些项目.请假设此服装唯一可能的产品类型是捆绑产品类型.
你需要一个观察者:
<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>
Run Code Online (Sandbox Code Playgroud)
然后做观察者:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
只是一个简单的样本,但它可能会有所帮助.
通过代码使用捆绑产品时,可能很难理解:这是一个示例图像:

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

一旦您点击"添加到购物车",它就会在购物车中显示如下:Bundle Sample
Sample Shampoo
1 x Moisturiser-125ml $29.95
Default Conditioner
1 x Moisturiser-60g $99.95
Run Code Online (Sandbox Code Playgroud)
通过代码查询此产品时,您可以像任何普通产品一样加载它:
$oProduct->load($vProductId);
Run Code Online (Sandbox Code Playgroud)
加载后,您需要获取产品类型实例,以便可以加载此产品类型的选项.
$oTypeInstance = $oProduct->getTypeInstance(true);
Run Code Online (Sandbox Code Playgroud)
现在我们可以通过以下方式获取此产品的选项ID列表:
$oTypeInstance = $oProduct->getTypeInstance(true);
Run Code Online (Sandbox Code Playgroud)
要查询选择,我们将向集合添加选项列表,然后获取选项集合以及它们各自的选择:
$aSelections = $oTypeInstance->getSelectionsCollection($aOptionIds, $oProduct );
$aOptions = $oTypeInstance->getOptionsByIds($aOptionIds, $oProduct);
$bundleOptions = $aOptions->appendSelections($aSelections, true);
Run Code Online (Sandbox Code Playgroud)
现在我们有一个选项集合,每个选项都有一个选择集合.我们现在可以遍历选项,并查看他们的选择.
foreach ($bundleOptions as $bundleOption) {
if ($bundleOption->getSelections()) {
$bundleSelections = $bundleOption->getSelections();
foreach ($bundleSelections as $bundleSelection) {
// get some data here
$vName = $bundleOption->getTitle();
}
}
}
Run Code Online (Sandbox Code Playgroud)
要获取Bundled产品的Required Selection Products列表,可以使用以下代码:
$requiredChildren = $this->getChildrenIds($product->getId(),$required=true);
Run Code Online (Sandbox Code Playgroud)
然后,您可以遍历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
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码从Quote Item Bundled Product获取Options,然后在集合中获取该产品的Options,然后找到"Selected"选项选择.
哈恩,肖恩