magento添加多个产品到购物车(Add multiple products to cart )
Step 1
app\design\frontend\base\default\template\catalog\product\list.phtml
<?php
$_productCollection=$this->getLoadedProductCollection();
$_helper = $this->helper('catalog/output');
?>
<?php if(!$_productCollection->count()): ?>
<p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p>
<?php else: ?>
<div class="category-products">
<?php echo $this->getToolbarHtml() ?>
<!-- junpeng add start -->
<form action="<?php echo
$this->helper('multiadd/cart')->getAddToCartUrl() ?>"
method="post" id="productAddToCartForm">
<!-- junpeng end end -->
<?php // List mode ?>
<?php if($this->getMode()!='grid'): ?>
<?php $_iterator = 0; ?>
<ol class="products-list" id="products-list">
<?php foreach ($_productCollection as $_product): ?>
<li class="item<?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>">
<?php // Product Image ?>
<a href="http://zjp2230987.blog.163.com/blog/<?php echo $_product->getProductUrl() ?>"
title="<?php echo
$this->stripTags($this->getImageLabel($_product, 'small_image'),
null, true) ?>" class="product-image"><img src="http://zjp2230987.blog.163.com/blog/<?php echo
$this->helper('catalog/image')->init($_product,
'small_image')->resize(135); ?>" width="135" height="135"
alt="<?php echo
$this->stripTags($this->getImageLabel($_product, 'small_image'),
null, true) ?>" /></a>
<?php // Product description ?>
<div class="product-shop">
<div class="f-fix">
<?php $_productNameStripped = $this->stripTags($_product->getName(), null, true); ?>
<h2 class="product-name"><a href="http://zjp2230987.blog.163.com/blog/<?php echo
$_product->getProductUrl() ?>" title="<?php echo
$_productNameStripped; ?>"><?php echo
$_helper->productAttribute($_product, $_product->getName() ,
'name'); ?></a></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product) ?>
<?php endif; ?>
<!-- junpeng add start -->
<?php if(Mage::getSingleton('customer/session')->isLoggedIn()):?>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php if($_product->isSaleable()): ?>
<?php if(!$_product->isGrouped()): ?>
<input type="hidden" name="products[]" value="<?php echo $_product->getId()?>" />
<label for="qty<?php echo $_product->getId()?>"><?php
echo $this->__('Qty') ?>:</label>
<input type="text" name="qty<?php echo
$_product->getId()?>" id="qty<?php echo
$_product->getId()?>" maxlength="12" value="<?php echo
($this->getMinimalQty($_product)?$this->getMinimalQty($_product):0)
?>" class="input-text qty" />
<?php endif; ?>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo
$this->__('Out of stock') ?></span></p>
<?php endif; ?>
<?php else: ?>
<p><a href="http://zjp2230987.blog.163.com/blog/<?php echo
Mage::helper('customer')->getLoginUrl(); ?>"><?php echo
$this->__('Login to view pricing') ?></a></p>
<?php endif; ?>
<!-- junpeng add end -->
<div class="desc std">
<?php echo $_helper->productAttribute($_product,
$_product->getShortDescription(), 'short_description') ?>
<a href="http://zjp2230987.blog.163.com/blog/<?php echo $_product->getProductUrl() ?>"
title="<?php echo $_productNameStripped ?>"
class="link-learn"><?php echo $this->__('Learn More')
?></a>
</div>
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="http://zjp2230987.blog.163.com/blog/<?php echo
$this->helper('wishlist')->getAddUrl($_product) ?>"
class="link-wishlist"><?php echo $this->__('Add to Wishlist')
?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <a
href="http://zjp2230987.blog.163.com/blog/<?php echo $_compareUrl ?>" class="link-compare"><?php
echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
</div>
</li>
<?php endforeach; ?>
</ol>
<script type="text/javascript">decorateList('products-list', 'none-recursive')</script>
<?php else: ?>
<?php // Grid Mode ?>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<ul class="products-grid">
<?php endif ?>
<li class="item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">
<a href="http://zjp2230987.blog.163.com/blog/<?php echo
$_product->getProductUrl() ?>" title="<?php echo
$this->stripTags($this->getImageLabel($_product, 'small_image'),
null, true) ?>" class="product-image"><img src="http://zjp2230987.blog.163.com/blog/<?php echo
$this->helper('catalog/image')->init($_product,
'small_image')->resize(135); ?>" width="135" height="135"
alt="<?php echo
$this->stripTags($this->getImageLabel($_product, 'small_image'),
null, true) ?>" /></a>
<h2
class="product-name"><a href="http://zjp2230987.blog.163.com/blog/<?php echo
$_product->getProductUrl() ?>" title="<?php echo
$this->stripTags($_product->getName(), null, true)
?>"><?php echo $_helper->productAttribute($_product,
$_product->getName(), 'name') ?></a></h2>
<?php if($_product->getRatingSummary()): ?>
<?php echo $this->getReviewsSummaryHtml($_product, 'short') ?>
<?php endif; ?>
<!-- junpeng add start -->
<?php if(Mage::getSingleton('customer/session')->isLoggedIn()):?>
<?php echo $this->getPriceHtml($_product, true) ?>
<?php endif; ?>
<!-- junpeng add end -->
<div class="actions">
<!-- junpeng add start -->
<?php if(Mage::getSingleton('customer/session')->isLoggedIn()):?>
<?php if($_product->isSaleable()): ?>
<?php if(!$_product->isGrouped()): ?>
<input type="hidden" name="products[]" value="<?php echo $_product->getId()?>" />
<label for="qty<?php echo
$_product->getId()?>"><?php echo $this->__('Qty')
?>:</label>
<input
type="text" name="qty<?php echo $_product->getId()?>"
id="qty<?php echo $_product->getId()?>" maxlength="12"
value="<?php echo
($this->getMinimalQty($_product)?$this->getMinimalQty($_product):0)
?>" class="input-text qty" />
<?php endif; ?>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo
$this->__('Out of stock') ?></span></p>
<?php endif; ?>
<?php else: ?>
<p><a href="http://zjp2230987.blog.163.com/blog/<?php echo
Mage::helper('customer')->getLoginUrl(); ?>"><?php echo
$this->__('Login to view pricing') ?></a></p>
<?php endif; ?>
<!-- junpeng add end -->
<ul class="add-to-links">
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<li><a href="http://zjp2230987.blog.163.com/blog/<?php echo
$this->helper('wishlist')->getAddUrl($_product) ?>"
class="link-wishlist"><?php echo $this->__('Add to Wishlist')
?></a></li>
<?php endif; ?>
<?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
<li><span class="separator">|</span> <a
href="http://zjp2230987.blog.163.com/blog/<?php echo $_compareUrl ?>" class="link-compare"><?php
echo $this->__('Add to Compare') ?></a></li>
<?php endif; ?>
</ul>
</div>
</li>
<?php if ($i%$_columnCount==0 || $i==$_collectionSize): ?>
</ul>
<?php endif ?>
<?php endforeach ?>
<script
type="text/javascript">decorateGeneric($$('ul.products-grid'),
['odd','even','first','last'])</script>
<?php endif; ?>
<div class="toolbar-bottom">
<!-- junpeng add start -->
<?php if(Mage::getSingleton('customer/session')->isLoggedIn()):?>
<div style="text-align:center;">
<button class="button btn-cart" type="button"
onclick="productAddToCartForm.submit()"><span><span><?php
echo $this->__('Add to Cart')
?></span></span></button>
</div>
<br>
</form>
<?php endif; ?>
<!-- junpeng end start -->
<?php echo $this->getToolbarHtml() ?>
</div>
</div>
<?php endif; ?>
Step 2
app\code\local\Perpetual\MultiAdd\controllers\Checkout\CartController.php
<?php
require_once('Mage/Checkout/controllers/CartController.php');
class Perpetual_MultiAdd_Checkout_CartController extends Mage_Checkout_CartController
{
public function addmultipleAction()
{
$productIds = $this->getRequest()->getParam('products');
if (!is_array($productIds)) {
$this->_goBack();
return;
}
foreach( $productIds as $productId) {
try {
$qty = $this->getRequest()->getParam('qty' . $productId, 0);
if ($qty <= 0) continue; // nothing to add
$cart = $this->_getCart();
$product = Mage::getModel('catalog/product')
->setStoreId(Mage::app()->getStore()->getId())
->load($productId)
->setConfiguredAttributes($this->getRequest()->getParam('super_attribute'))
->setGroupedProducts($this->getRequest()->getParam('super_group', array()));
$eventArgs = array(
'product' => $product,
'qty' => $qty,
'request' => $this->getRequest(),
'response' => $this->getResponse(),
);
Mage::dispatchEvent('checkout_cart_before_add', $eventArgs);
$cart = Mage::getModel('checkout/cart')->init();
$cart->addProduct($product, $qty);
Mage::dispatchEvent('checkout_cart_after_add', $eventArgs);
$cart->save();
Mage::dispatchEvent('checkout_cart_add_product', array('product'=>$product));
$message = $this->__('%s was successfully added to your shopping cart.', $product->getName());
Mage::getSingleton('checkout/session')->addSuccess($message);
}
catch (Mage_Core_Exception $e) {
if (Mage::getSingleton('checkout/session')->getUseNotice(true)) {
Mage::getSingleton('checkout/session')->addNotice($product->getName() . ': ' . $e->getMessage());
}
else {
Mage::getSingleton('checkout/session')->addError($product->getName() . ': ' . $e->getMessage());
}
}
catch (Exception $e) {
Mage::getSingleton('checkout/session')->addException($e, $this->__('Can not add item to shopping cart'));
}
}
$this->_goBack();
}
}
?>
Step 3
app\code\local\Perpetual\MultiAdd\etc\config.xml
<?xml version="1.0"?>
<config>
<modules>
<Perpetual_MultiAdd>
<version>0.1.0</version>
</Perpetual_MultiAdd>
</modules>
<global>
<rewrite>
<perpetual_multiadd_checkout_cart>
<from><![CDATA[#^/checkout/cart/addmultiple/.*$#]]></from>
<to>/multiadd/checkout_cart/addmultiple/</to>
</perpetual_multiadd_checkout_cart>
</rewrite>
<helpers>
<multiadd>
<class>Perpetual_MultiAdd_Helper</class>
</multiadd>
</helpers>
</global>
<frontend>
<routers>
<perpetual_multiadd>
<use>standard</use>
<args>
<module>Perpetual_MultiAdd</module>
<frontName>multiadd</frontName>
</args>
</perpetual_multiadd>
</routers>
</frontend>
</config>
Step 4
app\code\local\Perpetual\MultiAdd\Helper\Cart.php
<?php
class Perpetual_MultiAdd_Helper_Cart extends Mage_Core_Helper_Url
{
/**
* Return url to add multiple items to the cart
* @return url
*/
public function getAddToCartUrl()
{
if ($currentCategory = Mage::registry('current_category')) {
$continueShoppingUrl = $currentCategory->getUrl();
} else {
$continueShoppingUrl = $this->_getUrl('*/*/*', array('_current'=>true));
}
$params = array(
Mage_Core_Controller_Front_Action::PARAM_NAME_URL_ENCODED =>
Mage::helper('core')->urlEncode($continueShoppingUrl)
);
if ($this->_getRequest()->getModuleName() == 'checkout'
&& $this->_getRequest()->getControllerName() == 'cart') {
$params['in_cart'] = 1;
}
return $this->_getUrl('checkout/cart/addmultiple', $params);
}
}
?>
Step 5
app\etc\modules\Perpetual_MultiAdd.xml
<?xml version="1.0"?>
<config>
<modules>
<Perpetual_MultiAdd>
<active>true</active>
<codePool>local</codePool>
<version>0.1.0</version>
</Perpetual_MultiAdd>
</modules>
</config>
magento添加多个产品到购物车(Add multiple products to cart )的更多相关文章
- 解决magento添加产品在前台不显示问题
有时候我们在magento系统添加产品,前台不显示,最模板分析可能 以下几个原因: 1 添加新品要重新index一下,magento是静态的.html页面,不reindex不出来的.在System→I ...
- magento -- 添加新产品时状态默认为激活,库存状态默认为有库存
添加新产品时状态默认为激活 打开文件/app/code/core/Mage/Catalog/Model/Product/Status.php,注释掉“Please Select” /** * Retr ...
- magento购物车添加减少数量 实时更新购物车
magento实时更新购物车商品数量 <td> <button onclick="addQtyone();" type="submit"> ...
- Magento添加一个下拉登陆菜单Create Magento Dropdown Login in a few minutes
Dropdown login forms are not a feature many online stores use, but in some cases they could be quite ...
- magento添加分类属性
在magento中给产品添加自定义属性是很容易实现在后台就可以很轻易添加,但是给分类就不行了,magento本身没有提供给category添加自定义属性.在实际的运用过程中我们想给cagegory添加 ...
- magento批量上传产品
Step1:表格仔细检查无误后,将准备好的图片上传至 media/import中.如果使用专用的图片服务器,把图片上传到服务器上,当然表格中的图片地址要做相应的修改. Step2:然后,登陆Magen ...
- magento中如何实现产品图片放大效果
Magento列表页用jQuery实现产品图片放大效果今天看到个网站,鼠标移到列表页的产品图片上,旁边会弹出一个大图,感觉不错,就自己在Magento里写了个.先看看效果 这个效果比较好实现,打开li ...
- Magento后台订单显示产品图片的修改方法
Magento后台订单原来是没有显示产品图片,客服业务还得到网站前台查找这个产品的图片提供给发货部,这样是很不方便的.为提高工作效率,应客服业务要求,现对Magento后台订单进行修改,使订单页面就显 ...
- magento -- 添加中国省份列表
magento本身的数据库里不包含中国的省份信息,你可以执行以下的SQL语句来添加 添加到directory_country_region表 INSERT INTO `directory_count ...
随机推荐
- hadoop2.0 和1.0的区别
1. Hadoop 1.0中的资源管理方案Hadoop 1.0指的是版本为Apache Hadoop 0.20.x.1.x或者CDH3系列的Hadoop,内核主要由HDFS和MapReduce两个系统 ...
- JFrame背景
1.引言 在了解了JFrame面板的相关知识后,我们可以选择在RootPane根面板或LayeredPane面板中设置背景图案. 2.方法 对于大小固定的窗口背景设置如下: //导入图案 ImageI ...
- apache 日志为每个域名独立配置单独的日志文件
<VirtualHost *:80>DocumentRoot "E:\luyou\viplijiang"ServerName vip.li.comTransferLog ...
- CentOS 7.2 安装教程
1.CentOS 7.2 下载 下载地址: http://www.centoscn.com/CentosSoft/iso/2016/0601/7341.html 下载:CentOS-7-x86_64- ...
- [Js]缓冲运动
一.运动框架 1.在开始运动时,关闭已有定时器(否则会不断有新的定时器执行) 2.把运动和停止隔开(if/else) 二.缓冲运动 逐渐变慢,最后停止(距离越远速度越大) 速度=(目标值-当前值)/缩 ...
- 修改PE文件的入口函数OEP
修改入口函数地址.这个是最省事的办法,在原PE文件中新增加一个节,计算新节的RVA,然后修改入口代码,使其指向新增加的节.当然,如果.text节空隙足够大的话,不用添加新节也可以. BOOL Chan ...
- ASP.NET MVC学习之路由篇(3)
根据路由输出链接 既然是网站开发自然少不了链接,我们已经学会了强大的路由,但是还缺少一步就是能够将这些路由的路径输出到页面,下面我们就开始学习如何输出路由路径. 首先我们的路由注册部分如下所示: 1 ...
- <转>提高iOS开发效率的方法和工具
介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先要说的肯定是IDE了,说到IDE,Xcode不能跑,当然你也可能同时在使用AppCode等其他的ID ...
- Java Bad version number in .class file
错误信息: java.lang.UnsupportedClassVersionError: Bad version number in .class file at java.lang.ClassLo ...
- DotNetBar v12.9.0.0 Fully Cracked
更新信息: http://www.devcomponents.com/customeronly/releasenotes.asp?p=dnbwf&v=12.9.0.0 如果遇到破解问题可以与我 ...