[英]Magento 1.9 - add a configurable product directly to cart from cross-sells in shopping cart
I have a question about adding a configurable product directly from the cross-sell section to the cart on the shopping cart page. 我有一个关于直接从交叉销售部分向购物车页面上的购物车中添加可配置产品的问题。 With a simple product, this is not a problem because it has no attributes. 对于简单的产品,这不是问题,因为它没有属性。 But for a configurable product normally I have to choose via a dropdown which properties I want to have for my product (like size or color). 但是对于可配置的产品,通常我必须通过下拉菜单选择要为我的产品设置的属性(例如尺寸或颜色)。 If I choose a configurable product as a cross-sell and I click on the "Add-to-Cart"-Button it will redirect me to the product detail page. 如果我选择可配置产品作为交叉销售,然后单击“添加到购物车”按钮,它将重定向到产品详细信息页面。
So the idea would be to have something like a popup where I can choose the size and color directly and add the product (with the chosen properties) to the shopping cart. 因此,想法是像弹出窗口一样,在其中我可以直接选择大小和颜色,然后将产品(具有所选属性)添加到购物车中。
Is there a module which brings the functionality (I can't find it)? 是否有带功能的模块(我找不到)? Or can I write something on my own like a form for each cross-sell? 还是我可以自己为每次交叉销售写一些表格?
Like the form on the product detail page 就像产品详细信息页面上的表格一样
<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>
...
Please review this code, hopefully this code have a solution. 请查看此代码,希望此代码有解决方案。
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
}
?> ?>
Add also this script: 还添加此脚本:
<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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.