Add or Remove Product Using Ajax

1. rewrite core compare action

<?xml version="1.0"?>
<config>
  <modules>
    <Bysoft_Mycatalog>
      <version>0.1.0</version>
    </Bysoft_Mycatalog>
  </modules>
  <global>
    <helpers>
      <mycatalog>
        <class>Bysoft_Mycatalog_Helper</class>
      </mycatalog>
      <catalog>
		<rewrite>
			<product_compare>Bysoft_Mycatalog_Helper_Product_Compare</product_compare>
		</rewrite>
      </catalog>
    </helpers>
    <resources>
	  <mycatalog_setup>
		<setup>
		  <module>Bysoft_Mycatalog</module>
		  <class>Mage_Eav_Model_Entity_Setup</class>
		</setup>
		<connection>
		  <use>core_setup</use>
		</connection>
	  </mycatalog_setup>
	  <mycatalog_write>
		<connection>
		  <use>core_write</use>
		</connection>
	  </mycatalog_write>
	  <mycatalog_read>
		<connection>
		  <use>core_read</use>
		</connection>
	  </mycatalog_read>
	</resources>
	<blocks>
	  <mycatalog>
		<class>Bysoft_Mycatalog_Block</class>
	  </mycatalog>
			<catalog>
				<rewrite>
					<layer_view>Bysoft_Mycatalog_Block_Layer_View</layer_view>
					<category_view>Bysoft_Mycatalog_Block_Category_View</category_view>
				</rewrite>
			</catalog>
	</blocks>
  </global>
  <frontend>
        <routers>
            <catalog>
				<args>
					<modules>
						<bysoft_mycatalog before="Mage_Catalog">Bysoft_Mycatalog</bysoft_mycatalog>
					</modules>
				</args>
			</catalog>
        </routers>
        <layout>
            <updates>
                <arithmetic>
                    <file>mycatalog.xml</file>
                </arithmetic>
            </updates>
        </layout>   
  </frontend>
</config> 

 2. in controller file. 

<?php 
require_once(Mage::getModuleDir('controllers','Mage_Catalog').DS.'Product'.DS.'CompareController.php');
class Bysoft_Mycatalog_Product_CompareController extends Mage_Catalog_Product_CompareController {
	public function changeajaxAction() {
		$productId = (int) $this->getRequest()->getParam('product');
		if ($productId
				&& (Mage::getSingleton('log/visitor')->getId() || Mage::getSingleton('customer/session')->isLoggedIn())
		) {
			if ($this->is_compare($productId)) {
				// to uncompared
				 $this->remove_compare($productId);
				
			} else {
				// to compared
				 $this->add_compare($productId);
			}			
		}		
	}
	/**
	 * check the product is compare or not
	 * @param unknown $productId
	 */
	public function is_compare($productId) {
		
		$product = Mage::getModel('catalog/product')->load($productId);
		$item = Mage::getModel('catalog/product_compare_item');
		$list = Mage::getModel('catalog/product_compare_list');
		//$list->_addVisitorToItem($item);
	
		$item->addVisitorId(Mage::getSingleton('log/visitor')->getId());
		if (Mage::getSingleton('customer/session')->isLoggedIn()) {
			$item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer());
		}
	
		$item->loadByProduct($product);
		if ($item->getId()) {
			return true;
		} else {
			return false;
		}
	}
	
	public function add_compare($productId) {
		try {
			if ($productId
					&& (Mage::getSingleton('log/visitor')->getId() || Mage::getSingleton('customer/session')->isLoggedIn())
			) {
				$product = Mage::getModel('catalog/product')
				->setStoreId(Mage::app()->getStore()->getId())
				->load($productId);
		
				if ($product->getId()/* && !$product->isSuper()*/) {
					Mage::getSingleton('catalog/product_compare_list')->addProduct($product);
					Mage::getSingleton('catalog/session')->addSuccess(
					$this->__('The product %s has been added to comparison list.', Mage::helper('core')->escapeHtml($product->getName()))
					);
					Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
				}
		
				
				Mage::helper('catalog/product_compare')->calculate();
			}
			echo json_encode(array('status'=>'1','is_compare'=>'1'));
		} catch (Exception $e) {
			echo json_encode(array('status'=>'2','msg'=>$e->getMessage()));
		}
	}
	
	public function remove_compare($productId) {
		try {
			if ((int) $productId) {
				$product = Mage::getModel('catalog/product')
				->setStoreId(Mage::app()->getStore()->getId())
				->load($productId);
		
				if($product->getId()) {
					/** @var $item Mage_Catalog_Model_Product_Compare_Item */
					$item = Mage::getModel('catalog/product_compare_item');
					if(Mage::getSingleton('customer/session')->isLoggedIn()) {
						$item->addCustomerData(Mage::getSingleton('customer/session')->getCustomer());
					} elseif ($this->_customerId) {
						$item->addCustomerData(
								Mage::getModel('customer/customer')->load($this->_customerId)
						);
					} else {
						$item->addVisitorId(Mage::getSingleton('log/visitor')->getId());
					}
		
					$item->loadByProduct($product);
		
					if($item->getId()) {
						$item->delete();
						Mage::getSingleton('catalog/session')->addSuccess(
						$this->__('The product %s has been removed from comparison list.', $product->getName())
						);
						Mage::dispatchEvent('catalog_product_compare_remove_product', array('product'=>$item));
						Mage::helper('catalog/product_compare')->calculate();
					}
				}
			}
			echo  json_encode(array('status'=>'1','is_compare'=>0));
		} catch (Exception $e) {
			echo  json_encode(array('status'=>'2','msg'=>$e->getMessage()));
		}
	}
	
}
?>

 3. in frontend phtml file

<div class="compare-box">
	                        <a class="compare-a <?php echo $this->getCompareClass($_product->getId());?>"	                     	                 
	                        to_url="<?php echo $_compare_helper->getChangeAjaxUrl($_product);?>"
	                        >
	                    	<?php echo $this->getCompareLabel($_product->getId());?></a></div>

 4. jquery ajax

<script type="text/javascript">//<![CDATA[
   (function($){
		$(".compare-a").click(function(){
			var current_obj = $(this);
			var is_compare =  $(this).attr('is_compare');		
			var ajax_url =  $(this).attr('to_url');
			$.ajax({  
		            url: ajax_url,  
		            type: "POST",  
		            dataType:"json",  
		            success: function(json_obj) { 
		                if(json_obj.status == 1) {
		                	if (json_obj.is_compare == '1') {
			                	alert('is_compare:1');
			                	current_obj.addClass('compared');
			                	current_obj.removeClass('uncompared');			                	
			                	current_obj.html('Compared'); 
		                	} else {
		                		alert('is_compare:0');
		                		current_obj.addClass('uncompared');
			                	current_obj.removeClass('compared');			                
			                	current_obj.html('Uncompared');
			                }
		                } else {
			                alert(json_obj.msg);		            
		                }  
		            }
		    }); 
		})
    })(jQuery); 
         //]]></script>

 

你可能感兴趣的:(remove)