Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
- <button type="button" id="product-addtocart-button" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span>Comprar</span></span></button>
- </form>
- 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;
- //Start of our new ajax code
- if(!url){
- url = jQuery('#product_addtocart_form').attr('action');
- }
- url = url.replace("onestepcheckout/index","ajax/index");
- var data = jQuery('#product_addtocart_form').serialize();
- data += '&isAjax=1';
- jQuery('#ajax_loader').show();
- try {
- jQuery.ajax({
- url: url,
- dataType: 'json',
- type : 'post',
- data: data,
- success: function(data){
- jQuery('#ajax_loader').hide();
- //alert(data.status + ": " + data.message);
- if(jQuery('.block-cart')){
- jQuery('.block-cart').replaceWith(data.sidebar);
- }
- if(jQuery('.header .links')){
- jQuery('.header .links').replaceWith(data.toplink);
- window.location.href = '<?php echo Mage::getUrl("onestepcheckout/index");?>';
- }
- }
- });
- } catch (e) {
- }
- this.form.action = oldUrl;
- if (e) {
- throw e;
- }
- if (button && button != 'undefined') {
- button.disabled = true;
- }
- }
- }.bind(productAddToCartForm);
- productAddToCartForm.submitLight = function(button, url){
- if(this.validator) {
- var nv = Validation.methods;
- delete Validation.methods['required-entry'];
- delete Validation.methods['validate-one-required'];
- delete Validation.methods['validate-one-required-by-name'];
- if (this.validator.validate()) {
- if (url) {
- this.form.action = url;
- }
- this.form.submit();
- }
- Object.extend(Validation.methods, nv);
- }
- }.bind(productAddToCartForm);
- <?php
- require_once 'Mage/Checkout/controllers/CartController.php';
- class Floresonline_Ajax_IndexController extends Mage_Checkout_CartController {
- public function addAction()
- {
- $cart = $this->_getCart();
- $params = $this->getRequest()->getParams();
- if($params['isAjax'] == 1){
- $response = array();
- try {
- if (isset($params['qty'])) {
- $filter = new Zend_Filter_LocalizedToNormalized(
- array('locale' => Mage::app()->getLocale()->getLocaleCode())
- );
- $params['qty'] = $filter->filter($params['qty']);
- }
- $product = $this->_initProduct();
- $related = $this->getRequest()->getParam('related_product');
- /**
- * Check product availability
- */
- if (!$product) {
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('Unable to find Product ID');
- }
- $cart->addProduct($product, $params);
- if (!empty($related)) {
- $cart->addProductsByIds(explode(',', $related));
- }
- $cart->save();
- $this->_getSession()->setCartWasUpdated(true);
- /**
- * @todo remove wishlist observer processAddToCart
- */
- Mage::dispatchEvent('checkout_cart_add_product_complete',
- array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
- );
- if (!$this->_getSession()->getNoCartRedirect(true)) {
- if (!$cart->getQuote()->getHasError()){
- $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
- $response['status'] = 'O item foi adicionado com sucesso';
- $response['message'] = $message;
- }
- }
- } catch (Mage_Core_Exception $e) {
- if ($this->_getSession()->getUseNotice(true)) {
- $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
- } else {
- $messages = array_unique(explode("n", $e->getMessage()));
- foreach ($messages as $message) {
- $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
- }
- }
- $url = $this->_getSession()->getRedirectUrl(true);
- if ($url) {
- $this->getResponse()->setRedirect($url);
- } else {
- $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
- }
- } catch (Exception $e) {
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('Cannot add the item to shopping cart.');
- Mage::logException($e);
- }
- $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
- return;
- }else{
- return parent::addAction();
- }
- }
- public function optionsAction(){
- $productId = $this->getRequest()->getParam('product_id');
- // Prepare helper and params
- $viewHelper = Mage::helper('catalog/product_view');
- $params = new Varien_Object();
- $params->setCategoryId(false);
- $params->setSpecifyOptions(false);
- // Render page
- try {
- $viewHelper->prepareAndRender($productId, $this, $params);
- } catch (Exception $e) {
- if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) {
- if (isset($_GET['store']) && !$this->getResponse()->isRedirect()) {
- $this->_redirect('');
- } elseif (!$this->getResponse()->isRedirect()) {
- $this->_forward('noRoute');
- }
- } else {
- Mage::logException($e);
- $this->_forward('noRoute');
- }
- }
- }
- protected function _getWishlist($wishlistId = null)
- {
- $wishlist = Mage::registry('wishlist');
- if ($wishlist) {
- return $wishlist;
- }
- try {
- if (!$wishlistId) {
- $wishlistId = $this->getRequest()->getParam('wishlist_id');
- }
- $customerId = Mage::getSingleton('customer/session')->getCustomerId();
- $wishlist = Mage::getModel('wishlist/wishlist');
- if ($wishlistId) {
- $wishlist->load($wishlistId);
- } else {
- $wishlist->loadByCustomer($customerId, true);
- }
- if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) {
- $wishlist = null;
- Mage::throwException(
- Mage::helper('wishlist')->__("Requested wishlist doesn't exist")
- );
- }
- Mage::register('wishlist', $wishlist);
- } catch (Mage_Core_Exception $e) {
- Mage::getSingleton('wishlist/session')->addError($e->getMessage());
- } catch (Exception $e) {
- Mage::getSingleton('wishlist/session')->addException($e,
- Mage::helper('wishlist')->__('Cannot create wishlist.')
- );
- return false;
- }
- return $wishlist;
- }
- public function addwishAction()
- {
- $response = array();
- if (!Mage::getStoreConfigFlag('wishlist/general/active')) {
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('Wishlist Has Been Disabled By Admin');
- }
- if(!Mage::getSingleton('customer/session')->isLoggedIn()){
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('Please Login First');
- }
- if(empty($response)){
- $session = Mage::getSingleton('customer/session');
- $wishlist = $this->_getWishlist();
- if (!$wishlist) {
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('Unable to Create Wishlist');
- }else{
- $productId = (int) $this->getRequest()->getParam('product');
- if (!$productId) {
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('Product Not Found');
- }else{
- $product = Mage::getModel('catalog/product')->load($productId);
- if (!$product->getId() || !$product->isVisibleInCatalog()) {
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('Cannot specify product.');
- }else{
- try {
- $requestParams = $this->getRequest()->getParams();
- if ($session->getBeforeWishlistRequest()) {
- $requestParams = $session->getBeforeWishlistRequest();
- $session->unsBeforeWishlistRequest();
- }
- $buyRequest = new Varien_Object($requestParams);
- $result = $wishlist->addNewItem($product, $buyRequest);
- if (is_string($result)) {
- Mage::throwException($result);
- }
- $wishlist->save();
- Mage::dispatchEvent(
- 'wishlist_add_product',
- array(
- 'wishlist' => $wishlist,
- 'product' => $product,
- 'item' => $result
- )
- );
- $referer = $session->getBeforeWishlistUrl();
- if ($referer) {
- $session->setBeforeWishlistUrl(null);
- } else {
- $referer = $this->_getRefererUrl();
- }
- $session->setAddActionReferer($referer);
- Mage::helper('wishlist')->calculate();
- $message = $this->__('%1$s has been added to your wishlist.',
- $product->getName(), Mage::helper('core')->escapeUrl($referer));
- $response['status'] = 'SUCCESS';
- $response['message'] = $message;
- Mage::unregister('wishlist');
- $this->loadLayout();
- $toplink = $this->getLayout()->getBlock('top.links')->toHtml();
- $sidebar_block = $this->getLayout()->getBlock('wishlist_sidebar');
- $sidebar = $sidebar_block->toHtml();
- $response['toplink'] = $toplink;
- $response['sidebar'] = $sidebar;
- }
- catch (Mage_Core_Exception $e) {
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('An error occurred while adding item to wishlist: %s', $e->getMessage());
- }
- catch (Exception $e) {
- mage::log($e->getMessage());
- $response['status'] = 'ERROR';
- $response['message'] = $this->__('An error occurred while adding item to wishlist.');
- }
- }
- }
- }
- }
- $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
- return;
- }
- public function compareAction(){
- $response = array();
- $productId = (int) $this->getRequest()->getParam('product');
- 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);
- $response['status'] = 'SUCCESS';
- $response['message'] = $this->__('The product %s has been added to comparison list.', Mage::helper('core')->escapeHtml($product->getName()));
- Mage::register('referrer_url', $this->_getRefererUrl());
- Mage::helper('catalog/product_compare')->calculate();
- Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
- $this->loadLayout();
- $sidebar_block = $this->getLayout()->getBlock('catalog.compare.sidebar');
- $sidebar_block->setTemplate('ajaxwishlist/catalog/product/compare/sidebar.phtml');
- $sidebar = $sidebar_block->toHtml();
- $response['sidebar'] = $sidebar;
- }
- }
- $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
- return;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement