Advertisement
Guest User

Untitled

a guest
Aug 18th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.00 KB | None | 0 0
  1. <form action="<?php echo $this->getSubmitUrl($_product) ?>" method="post" id="product_addtocart_form"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
  2. <button type="button" id="product-addtocart-button" class="button btn-cart" onclick="productAddToCartForm.submit(this)"><span><span>Comprar</span></span></button>
  3. </form>
  4.  
  5. var productAddToCartForm = new VarienForm('product_addtocart_form');
  6. productAddToCartForm.submit = function(button, url) {
  7. if (this.validator.validate()) {
  8. var form = this.form;
  9. var oldUrl = form.action;
  10.  
  11. if (url) {
  12. form.action = url;
  13. }
  14. var e = null;
  15. //Start of our new ajax code
  16. if(!url){
  17. url = jQuery('#product_addtocart_form').attr('action');
  18. }
  19. url = url.replace("onestepcheckout/index","ajax/index");
  20. var data = jQuery('#product_addtocart_form').serialize();
  21. data += '&isAjax=1';
  22. jQuery('#ajax_loader').show();
  23. try {
  24. jQuery.ajax({
  25. url: url,
  26. dataType: 'json',
  27. type : 'post',
  28. data: data,
  29. success: function(data){
  30. jQuery('#ajax_loader').hide();
  31. //alert(data.status + ": " + data.message);
  32. if(jQuery('.block-cart')){
  33. jQuery('.block-cart').replaceWith(data.sidebar);
  34. }
  35. if(jQuery('.header .links')){
  36. jQuery('.header .links').replaceWith(data.toplink);
  37. window.location.href = '<?php echo Mage::getUrl("onestepcheckout/index");?>';
  38. }
  39. }
  40. });
  41. } catch (e) {
  42. }
  43.  
  44. this.form.action = oldUrl;
  45. if (e) {
  46. throw e;
  47. }
  48.  
  49. if (button && button != 'undefined') {
  50. button.disabled = true;
  51. }
  52. }
  53. }.bind(productAddToCartForm);
  54.  
  55. productAddToCartForm.submitLight = function(button, url){
  56. if(this.validator) {
  57. var nv = Validation.methods;
  58. delete Validation.methods['required-entry'];
  59. delete Validation.methods['validate-one-required'];
  60. delete Validation.methods['validate-one-required-by-name'];
  61. if (this.validator.validate()) {
  62. if (url) {
  63. this.form.action = url;
  64. }
  65. this.form.submit();
  66. }
  67. Object.extend(Validation.methods, nv);
  68. }
  69. }.bind(productAddToCartForm);
  70.  
  71. <?php
  72.  
  73. require_once 'Mage/Checkout/controllers/CartController.php';
  74. class Floresonline_Ajax_IndexController extends Mage_Checkout_CartController {
  75.  
  76. public function addAction()
  77. {
  78. $cart = $this->_getCart();
  79. $params = $this->getRequest()->getParams();
  80. if($params['isAjax'] == 1){
  81. $response = array();
  82. try {
  83. if (isset($params['qty'])) {
  84. $filter = new Zend_Filter_LocalizedToNormalized(
  85. array('locale' => Mage::app()->getLocale()->getLocaleCode())
  86. );
  87. $params['qty'] = $filter->filter($params['qty']);
  88. }
  89.  
  90. $product = $this->_initProduct();
  91. $related = $this->getRequest()->getParam('related_product');
  92.  
  93. /**
  94. * Check product availability
  95. */
  96. if (!$product) {
  97. $response['status'] = 'ERROR';
  98. $response['message'] = $this->__('Unable to find Product ID');
  99. }
  100.  
  101. $cart->addProduct($product, $params);
  102. if (!empty($related)) {
  103. $cart->addProductsByIds(explode(',', $related));
  104. }
  105.  
  106. $cart->save();
  107.  
  108. $this->_getSession()->setCartWasUpdated(true);
  109.  
  110. /**
  111. * @todo remove wishlist observer processAddToCart
  112. */
  113. Mage::dispatchEvent('checkout_cart_add_product_complete',
  114. array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
  115. );
  116.  
  117. if (!$this->_getSession()->getNoCartRedirect(true)) {
  118. if (!$cart->getQuote()->getHasError()){
  119. $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->htmlEscape($product->getName()));
  120. $response['status'] = 'O item foi adicionado com sucesso';
  121. $response['message'] = $message;
  122. }
  123. }
  124. } catch (Mage_Core_Exception $e) {
  125. if ($this->_getSession()->getUseNotice(true)) {
  126. $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
  127. } else {
  128. $messages = array_unique(explode("n", $e->getMessage()));
  129. foreach ($messages as $message) {
  130. $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
  131. }
  132. }
  133.  
  134. $url = $this->_getSession()->getRedirectUrl(true);
  135. if ($url) {
  136. $this->getResponse()->setRedirect($url);
  137. } else {
  138. $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
  139. }
  140. } catch (Exception $e) {
  141. $response['status'] = 'ERROR';
  142. $response['message'] = $this->__('Cannot add the item to shopping cart.');
  143. Mage::logException($e);
  144. }
  145. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
  146. return;
  147. }else{
  148. return parent::addAction();
  149. }
  150. }
  151.  
  152. public function optionsAction(){
  153. $productId = $this->getRequest()->getParam('product_id');
  154. // Prepare helper and params
  155. $viewHelper = Mage::helper('catalog/product_view');
  156.  
  157. $params = new Varien_Object();
  158. $params->setCategoryId(false);
  159. $params->setSpecifyOptions(false);
  160.  
  161. // Render page
  162. try {
  163. $viewHelper->prepareAndRender($productId, $this, $params);
  164. } catch (Exception $e) {
  165. if ($e->getCode() == $viewHelper->ERR_NO_PRODUCT_LOADED) {
  166. if (isset($_GET['store']) && !$this->getResponse()->isRedirect()) {
  167. $this->_redirect('');
  168. } elseif (!$this->getResponse()->isRedirect()) {
  169. $this->_forward('noRoute');
  170. }
  171. } else {
  172. Mage::logException($e);
  173. $this->_forward('noRoute');
  174. }
  175. }
  176. }
  177.  
  178. protected function _getWishlist($wishlistId = null)
  179. {
  180. $wishlist = Mage::registry('wishlist');
  181. if ($wishlist) {
  182. return $wishlist;
  183. }
  184. try {
  185. if (!$wishlistId) {
  186. $wishlistId = $this->getRequest()->getParam('wishlist_id');
  187. }
  188. $customerId = Mage::getSingleton('customer/session')->getCustomerId();
  189. $wishlist = Mage::getModel('wishlist/wishlist');
  190.  
  191. if ($wishlistId) {
  192. $wishlist->load($wishlistId);
  193. } else {
  194. $wishlist->loadByCustomer($customerId, true);
  195. }
  196.  
  197. if (!$wishlist->getId() || $wishlist->getCustomerId() != $customerId) {
  198. $wishlist = null;
  199. Mage::throwException(
  200. Mage::helper('wishlist')->__("Requested wishlist doesn't exist")
  201. );
  202. }
  203.  
  204. Mage::register('wishlist', $wishlist);
  205. } catch (Mage_Core_Exception $e) {
  206. Mage::getSingleton('wishlist/session')->addError($e->getMessage());
  207. } catch (Exception $e) {
  208. Mage::getSingleton('wishlist/session')->addException($e,
  209. Mage::helper('wishlist')->__('Cannot create wishlist.')
  210. );
  211. return false;
  212. }
  213.  
  214. return $wishlist;
  215. }
  216. public function addwishAction()
  217. {
  218.  
  219. $response = array();
  220. if (!Mage::getStoreConfigFlag('wishlist/general/active')) {
  221. $response['status'] = 'ERROR';
  222. $response['message'] = $this->__('Wishlist Has Been Disabled By Admin');
  223. }
  224. if(!Mage::getSingleton('customer/session')->isLoggedIn()){
  225. $response['status'] = 'ERROR';
  226. $response['message'] = $this->__('Please Login First');
  227. }
  228.  
  229. if(empty($response)){
  230. $session = Mage::getSingleton('customer/session');
  231. $wishlist = $this->_getWishlist();
  232. if (!$wishlist) {
  233. $response['status'] = 'ERROR';
  234. $response['message'] = $this->__('Unable to Create Wishlist');
  235. }else{
  236.  
  237. $productId = (int) $this->getRequest()->getParam('product');
  238. if (!$productId) {
  239. $response['status'] = 'ERROR';
  240. $response['message'] = $this->__('Product Not Found');
  241. }else{
  242.  
  243. $product = Mage::getModel('catalog/product')->load($productId);
  244. if (!$product->getId() || !$product->isVisibleInCatalog()) {
  245. $response['status'] = 'ERROR';
  246. $response['message'] = $this->__('Cannot specify product.');
  247. }else{
  248.  
  249. try {
  250. $requestParams = $this->getRequest()->getParams();
  251. if ($session->getBeforeWishlistRequest()) {
  252. $requestParams = $session->getBeforeWishlistRequest();
  253. $session->unsBeforeWishlistRequest();
  254. }
  255. $buyRequest = new Varien_Object($requestParams);
  256.  
  257. $result = $wishlist->addNewItem($product, $buyRequest);
  258. if (is_string($result)) {
  259. Mage::throwException($result);
  260. }
  261. $wishlist->save();
  262.  
  263. Mage::dispatchEvent(
  264. 'wishlist_add_product',
  265. array(
  266. 'wishlist' => $wishlist,
  267. 'product' => $product,
  268. 'item' => $result
  269. )
  270. );
  271.  
  272.  
  273. $referer = $session->getBeforeWishlistUrl();
  274. if ($referer) {
  275. $session->setBeforeWishlistUrl(null);
  276. } else {
  277. $referer = $this->_getRefererUrl();
  278. }
  279. $session->setAddActionReferer($referer);
  280.  
  281. Mage::helper('wishlist')->calculate();
  282.  
  283. $message = $this->__('%1$s has been added to your wishlist.',
  284. $product->getName(), Mage::helper('core')->escapeUrl($referer));
  285.  
  286. $response['status'] = 'SUCCESS';
  287. $response['message'] = $message;
  288.  
  289. Mage::unregister('wishlist');
  290.  
  291. $this->loadLayout();
  292. $toplink = $this->getLayout()->getBlock('top.links')->toHtml();
  293. $sidebar_block = $this->getLayout()->getBlock('wishlist_sidebar');
  294. $sidebar = $sidebar_block->toHtml();
  295. $response['toplink'] = $toplink;
  296. $response['sidebar'] = $sidebar;
  297. }
  298. catch (Mage_Core_Exception $e) {
  299. $response['status'] = 'ERROR';
  300. $response['message'] = $this->__('An error occurred while adding item to wishlist: %s', $e->getMessage());
  301. }
  302. catch (Exception $e) {
  303. mage::log($e->getMessage());
  304. $response['status'] = 'ERROR';
  305. $response['message'] = $this->__('An error occurred while adding item to wishlist.');
  306. }
  307. }
  308. }
  309. }
  310.  
  311. }
  312. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
  313. return;
  314. }
  315. public function compareAction(){
  316. $response = array();
  317.  
  318. $productId = (int) $this->getRequest()->getParam('product');
  319.  
  320. if ($productId && (Mage::getSingleton('log/visitor')->getId() || Mage::getSingleton('customer/session')->isLoggedIn())) {
  321. $product = Mage::getModel('catalog/product')
  322. ->setStoreId(Mage::app()->getStore()->getId())
  323. ->load($productId);
  324.  
  325. if ($product->getId()/* && !$product->isSuper()*/) {
  326. Mage::getSingleton('catalog/product_compare_list')->addProduct($product);
  327. $response['status'] = 'SUCCESS';
  328. $response['message'] = $this->__('The product %s has been added to comparison list.', Mage::helper('core')->escapeHtml($product->getName()));
  329. Mage::register('referrer_url', $this->_getRefererUrl());
  330. Mage::helper('catalog/product_compare')->calculate();
  331. Mage::dispatchEvent('catalog_product_compare_add_product', array('product'=>$product));
  332. $this->loadLayout();
  333. $sidebar_block = $this->getLayout()->getBlock('catalog.compare.sidebar');
  334. $sidebar_block->setTemplate('ajaxwishlist/catalog/product/compare/sidebar.phtml');
  335. $sidebar = $sidebar_block->toHtml();
  336. $response['sidebar'] = $sidebar;
  337. }
  338. }
  339. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
  340. return;
  341. }
  342. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement