Magento 1.9 - 从购物车中的croll-sells直接添加可配置产品到购物车

时间:2016-06-09 09:48:36

标签: php magento module shopping-cart configurable-product

我有一个问题,即直接从交叉销售部分向购物车页面上的购物车添加可配置产品。使用简单的产品,这不是问题,因为它没有属性。但对于可配置产品,我通常需要通过下拉列表选择我想要的产品属性(如尺寸或颜色)。如果我选择可配置产品作为交叉销售,我点击"添加到购物车" -Button,它会将我重定向到产品详细信息页面。

所以我的想法就是有一个类似弹出窗口的东西,我可以直接选择尺寸和颜色,并将产品(带有所选属性)添加到购物车。

是否有一个带有功能的模块(我无法找到它)?或者我可以自己写一些东西,就像每个交叉的形式一样?

与产品详情页面上的表格一样

<form action="<?php echo $this->getSubmitUrl($_product, array('_secure' => $this->_isSecure())) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
    <?php echo $this->getBlockHtml('formkey') ?>
    <div class="no-display">
        <input type="hidden" name="product" value="<?php echo $_product->getId() ?>" />
        <input type="hidden" name="related_product" id="related-products-field" value="" />
    </div>
...

1 个答案:

答案 0 :(得分:1)

请查看此代码,希望此代码有解决方案。

foreach ($_productCollection as $_product) {

   ?>
   <div class="sqs-col-4 item-product">
      <div class="thumb"><a href="<?php echo $_product->getProductUrl(); ?>" title="<?php echo $_product->getName(); ?>"><img src="<?php echo $_product->getImageUrl(); ?>" alt="" /></a></div>
      <h1><a href="<?php echo $_product->getProductUrl(); ?>" title="<?php echo $_product->getName(); ?>"><?php echo $_product->getName(); ?></a></h1>
      <h4><?php echo Mage::helper('core')->currency($_product->getPrice()); ?></h4>

      <form action="<?php echo $this->helper('checkout/cart')->getAddUrl($_product);?>" method="post" id="product_addtocart_form">

        <?php 
          if ($_product->getData('type_id') == "configurable")
            {
                //get the configurable data from the product
                $config = $_product->getTypeInstance(true);
                //loop through the attributes

                foreach($config->getConfigurableAttributesAsArray($_product) as $attributes)
                {

                    ?>
                    <div id="product-options-wrapper" class="select_number">
                        <label class="required last"><em>*</em><?php echo $attributes["frontend_label"]; ?></label>
                            <select class="required-entry" name="super_attribute[<?php echo $attributes['attribute_id'] ?>]" id="attribute<?php echo $attributes['attribute_id'] ?>">
                                    <option value=""><?php echo $attributes["store_label"]; ?></option>
                                    <?php

                                    foreach($attributes["values"] as $values)
                                    {
                                        echo "<option value=".$values["value_index"].">".$values["label"]."</option>";
                                    }
                                    ?>
                            </select>
                    </div>
                    <div style="display: none;" id="advice-required-entry-attribute<?php echo $attributes['attribute_id'] ?>" class="validation-advice">This is a required field.</div>
                    <?php
                }
            }
        if(!$_product->isGrouped()): ?>
        <label for="qty"><?php echo $this->__('Quantity') ?>:</label>
        <input type="number" name="qty" id="qty" maxlength="3" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>"/>
        <?php endif; ?>

        <?php if($_product->isSaleable()): ?>
            <button type="button" id="" title="<?php echo $this->__('Add to Cart') ?>" onclick="productAddToCartForm.submit(this)" value="Add To cart" /></button>
        <?php else: ?>
            <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
        <?php endif; ?>
     </form>
  </div>
  <?php    
}

&GT;

还添加此脚本:

<script>
var productAddToCartForm = new VarienForm('product_addtocart_form');

        productAddToCartForm.submit = function(button, url) {
            if (this.validator.validate()) {
                var form = this.form;
                var oldUrl = form.action;

                if (url) {
                   form.action = url;
                }
                var e = null;
                try {
                    this.form.submit();
                } catch (e) {
                }
                this.form.action = oldUrl;
                if (e) {
                    throw e;
                }

                if (button && button != 'undefined') {
                    button.disabled = true;
                }
            }
        }.bind(productAddToCartForm);