首页 文章

Magento:Addon产品

提问于
浏览
0

我想在Magento中创建“Addon Products” . 我的意思是:如果产品是贺卡,并且您要将其添加到购物车中,您可能会在结帐页面上看到添加“信封”或“邮票”的选项 . 这些将是具有自己价格的单独产品,但是,它们在商店中不可用 . 换句话说,某种“链接/儿童产品”只有在添加“父”产品后才可用 .

Magento中是否存在这种产品配置?似乎不是捆绑产品 .

1 回答

  • 1

    是Magento开箱即用 .

    针对每个产品分配“交叉销售”产品,使用左侧的交叉销售标签 . 当您第一次到达时,如果列表为空,请按“重置过滤器”按钮以显示商店中的所有产品 . 找到你想要的那些并在它们旁边打勾,然后点击保存 .

    你追求的阻碍是;

    <block type="checkout/cart_crosssell" name="checkout.cart.crosssell" as="crosssell" template="checkout/cart/crosssell.phtml"/>
    

    哪个大多数模板都加载到主题的layout / checkout.xml布局文件中的“content”中,

    <checkout_cart_index translate="label">
    ////
    <reference name="content">
    ///// HERE
    </reference>
    /////
    </checkout_cart_index>
    

    然后将其添加到checkout / cart.phtml模板(如果它已经不存在);

    <?php echo $this->getChildHtml('crosssell') ?>
    

    但是你可以把它添加到你想要的任何地方 .

    要处理这些不在其他地方出现的产品,请将它们的可见性设置为“目录”,并将它们放在一个不可见的类别中,或者不将它们添加到类别中 .

    回答你的第二个问题......

    您询问如果篮子中没有“主要产品”商品,您可以如何从购物篮中删除所有“添加”商品 . 这是一个快速解决方案,可以做到这一点 .

    创建自定义选择产品属性,为其提供ID代码'product_type_var',并为其提供2个选项'Main Product'和'Addon Product' . 将其添加到产品属性集中,并根据相应的产品设置值 .

    然后,您可以针对购物篮运行以下代码 . 理想情况下,您将使用事件观察器创建一个模块 - 但是为了这个示例,您还可以将此代码放在顶部

    app/design/frontend/XXX/YYY/template/checkout/cart.phtml
    

    这是代码;

    $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(); 
                    }
    
                }
                }
    

相关问题