$quote = Mage::getSingleton('checkout/session')->getQuote();
$needsAction = true;
$toRemove = array();
foreach ($quote->getAllItems() as $item) {
$product = $item->getProduct();
$productLoad = Mage::getModel('catalog/product')->load($product->getId());
$customVariable = $productLoad->getResource()->getAttribute('product_type_var')->getFrontend()->getValue($productLoad);
if($customVariable == 'Main Product') {
$needsAction = false;
break; // No need to do anything
}
if($customVariable == 'Addon Product') {
$toRemove[] = $productLoad->getId(); // Build list of addon IDs
}
}
if($needsAction && (!empty($toRemove))) {
// There are no Main Products and 1 or more Addons
foreach($toRemove as $removeId) {
$quote->removeItem($removeId)->save();
}
}
修订版3
为了确保任何“插件”产品只在购物车中保留,如果它们与购物车中的特定“主要产品”相关,请尝试这样做;
$quote = Mage::getSingleton('checkout/session')->getQuote();
$allowedUpsells = array();
$upsellsInCart = array();
$allIdsInCart = array();
foreach ($quote->getAllItems() as $item) {
$product = $item->getProduct();
$productLoad = Mage::getModel('catalog/product')->load($product->getId());
$customVariable = $productLoad->getResource()->getAttribute('product_type_var')->getFrontend()->getValue($productLoad);
if($customVariable == 'Main Product') {
$allIdsInCart[] = $productLoad->getId(); // Build list of all products in the cart
$upsells = $productLoad->getUpSellProductCollection(); // Get this products available upsells
foreach($upsells as $upsell) {
$allowedUpsells[] = $upsell->getId(); // Build full list of allowed addon IDs
}
}
if($customVariable == 'Addon Product') {
$allIdsInCart[] = $productLoad->getId(); // Build list of all products in the cart
$upsellsInCart[] = $productLoad->getId(); //Build full list of addon IDs
}
}
if(!empty($upsellsInCart)) { // Upsells might need attention
$allowedVsInCart = array_intersect($allowedUpsells, $allIdsInCart); // Remove other upsells that are avaiable to the product but not in the cart
$toBeRemoved = array_diff_assoc($allowedVsInCart, $upsellsInCart); // Now find the products in the cart that shouldnt be
if(!empty($toBeRemoved)) {
foreach($toBeRemoved as $removeId) {
$quote->removeItem($removeId)->save();
}
}
}
1 回答
是Magento开箱即用 .
针对每个产品分配“交叉销售”产品,使用左侧的交叉销售标签 . 当您第一次到达时,如果列表为空,请按“重置过滤器”按钮以显示商店中的所有产品 . 找到你想要的那些并在它们旁边打勾,然后点击保存 .
你追求的阻碍是;
哪个大多数模板都加载到主题的layout / checkout.xml布局文件中的“content”中,
然后将其添加到checkout / cart.phtml模板(如果它已经不存在);
但是你可以把它添加到你想要的任何地方 .
要处理这些不在其他地方出现的产品,请将它们的可见性设置为“目录”,并将它们放在一个不可见的类别中,或者不将它们添加到类别中 .
回答你的第二个问题......
您询问如果篮子中没有“主要产品”商品,您可以如何从购物篮中删除所有“添加”商品 . 这是一个快速解决方案,可以做到这一点 .
创建自定义选择产品属性,为其提供ID代码'product_type_var',并为其提供2个选项'Main Product'和'Addon Product' . 将其添加到产品属性集中,并根据相应的产品设置值 .
然后,您可以针对购物篮运行以下代码 . 理想情况下,您将使用事件观察器创建一个模块 - 但是为了这个示例,您还可以将此代码放在顶部
这是代码;
修订版3
为了确保任何“插件”产品只在购物车中保留,如果它们与购物车中的特定“主要产品”相关,请尝试这样做;