Magento 1.9 - 从购物车的croll-sells直接添加可配置产品到购物车
问题描述:
我有一个关于直接从交叉销售部分向购物车页面上的购物车添加可配置产品的问题。使用简单的产品,这不是问题,因为它没有属性。但对于可配置产品,通常我必须通过下拉列表选择我想要的产品(如尺寸或颜色)。如果我选择可配置的产品作为交叉销售,并且点击“加入购物车” - 按钮,它会将我重定向到产品详细信息页面。Magento 1.9 - 从购物车的croll-sells直接添加可配置产品到购物车
所以这个想法是有一个像弹出的东西,我可以直接选择大小和颜色,并将产品(具有选定的属性)添加到购物车。
是否有一个模块带来的功能(我找不到)?还是我可以像我每个交叉销售的表格一样自己写一些东西?
喜欢该产品的详细信息页面
<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>
...
答
请仔细阅读该代码,希望这对代码的形式有一个解决办法。 ?
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
}
>
添加也是这个脚本:
<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);
搜索的Magento的快速视图模块。将可配置产品添加到购物车时,它会弹出一个窗口。检查代码并尝试。另外,请记住MD_QuickView模块有一个SQL注入漏洞。 [示例搜索](https://www.magentocommerce.com/magento-connect/catalogsearch/result/?id=&s=7&pl=0&eb=0&hp=0&q=quickview&t=1&p=1) – aki
不错。感谢您分享链接。 –