Guest User

Untitled

a guest
Jan 17th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. class Mymodule_Addtocartredirect_Model_Customizablereplace {
  2. public function replaceItems($observer) {
  3. $quote = Mage::getSingleton('checkout/session')->getQuote();
  4. $storeId = Mage::app()->getStore()->getStoreId();
  5.  
  6. foreach ($quote->getAllItems() as $item) {
  7. $result = array();
  8. Mage::log("We have a product! " . $item->getSku(),null,"test.log");
  9. if($item->getOptionByCode('simple_product')) {
  10. $childProduct = $item->getOptionByCode('simple_product')->getProduct()->getId();
  11. Mage::log("We have ".$childProduct,null,"test.log");
  12. }
  13.  
  14. if ($option = $item->getOptionByCode('simple_product')) {
  15. $cartHelper = Mage::helper('checkout/cart')->getCart();
  16. $quantity = $item->getQty();
  17. $childProduct = $option->getProduct();
  18. Mage::log("Child Product: ".$childProduct->getSku()." (".$childProduct->getId().")",null,"test.log");
  19. $cartHelper->removeItem($item->getId());
  20. $cartHelper->addItem($childProduct->getId(),$quantity);
  21. }
  22. }
  23. $quote->collectTotals()->save();
  24. }
  25. }
  26.  
  27. <?php
  28. require_once 'Mage/Checkout/controllers/CartController.php';
  29. class Yourname_Mymodule_CartController extends Mage_Checkout_CartController
  30. {
  31. public function addAction()
  32. {
  33. if (!$this->_validateFormKey()) {
  34. $this->_goBack();
  35. return;
  36. }
  37. $cart = $this->_getCart();
  38. $params = $this->getRequest()->getParams();
  39. try {
  40. if (isset($params['qty'])) {
  41. $filter = new Zend_Filter_LocalizedToNormalized(
  42. array('locale' => Mage::app()->getLocale()->getLocaleCode())
  43. );
  44. $params['qty'] = $filter->filter($params['qty']);
  45. }
  46.  
  47. $product = $this->_initProduct();
  48. $child = $product->getTypeInstance(true)->getProductByAttributes($params['super_attribute'], $product);
  49. $related = $this->getRequest()->getParam('related_product');
  50.  
  51.  
  52. if (!$child) {
  53. $this->_goBack();
  54. return;
  55. }
  56.  
  57. $cart->addProduct($child, $params);
  58. if (!empty($related)) {
  59. $cart->addProductsByIds(explode(',', $related));
  60. }
  61.  
  62. $cart->saveSpecial();
  63.  
  64. $this->_getSession()->setCartWasUpdated(true);
  65.  
  66.  
  67. Mage::dispatchEvent('checkout_cart_add_product_complete',
  68. array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
  69. );
  70.  
  71. if (!$this->_getSession()->getNoCartRedirect(true)) {
  72. if (!$cart->getQuote()->getHasError()) {
  73. $message = $this->__('%s was added to your shopping cart.', Mage::helper('core')->escapeHtml($child->getName()));
  74. $this->_getSession()->addSuccess($message);
  75. }
  76. $this->_goBack();
  77. }
  78. } catch (Mage_Core_Exception $e) {
  79. if ($this->_getSession()->getUseNotice(true)) {
  80. $this->_getSession()->addNotice(Mage::helper('core')->escapeHtml($e->getMessage()));
  81. } else {
  82. $messages = array_unique(explode("n", $e->getMessage()));
  83. foreach ($messages as $message) {
  84. $this->_getSession()->addError(Mage::helper('core')->escapeHtml($message));
  85. }
  86. }
  87.  
  88. $url = $this->_getSession()->getRedirectUrl(true);
  89. if ($url) {
  90. $this->getResponse()->setRedirect($url);
  91. } else {
  92. $this->_redirectReferer(Mage::helper('checkout/cart')->getCartUrl());
  93. }
  94. } catch (Exception $e) {
  95. $this->_getSession()->addException($e, $this->__('Cannot add the item to shopping cart.'));
  96. Mage::logException($e);
  97. $this->_goBack();
  98. }
  99. }
  100. }
  101.  
  102. <?php
  103. class Yourname_Mymodule_Model_Cart extends Mage_Checkout_Model_Cart
  104. {
  105. public function save()
  106. {
  107. Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));
  108.  
  109. $this->getQuote()->getBillingAddress();
  110. $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
  111. $this->getQuote()->collectTotals();
  112. $this->getQuote()->save();
  113. $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
  114. Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
  115. foreach($this->getQuote()->getAllItems() as $_item)
  116. {
  117. $_item->setCustomPrice($_item->getPrice());
  118. $_item->setRowTotal($_item->getPrice()*$_item->getQty());
  119. $_item->setBaseRowTotal($_item->getBasePrice()*$_item->getQty());
  120. $_item->setOriginalCustomPrice($_item->getCustomPrice());
  121. $_item->setCalculationPrice($_item->getCustomPrice());
  122. }
  123. return $this;
  124. }
  125.  
  126. public function saveSpecial()
  127. {
  128. Mage::dispatchEvent('checkout_cart_save_before', array('cart'=>$this));
  129.  
  130. $this->getQuote()->getBillingAddress();
  131. $this->getQuote()->getShippingAddress()->setCollectShippingRates(true);
  132. $this->getQuote()->collectTotals();
  133. $this->getQuote()->save();
  134. $this->getCheckoutSession()->setQuoteId($this->getQuote()->getId());
  135.  
  136. Mage::dispatchEvent('checkout_cart_save_after', array('cart'=>$this));
  137. return $this;
  138. }
  139. }
  140.  
  141. <?php
  142. namespace VendorModuleControllerPluginCart;
  143.  
  144. class Add
  145. {
  146.  
  147. public function beforeExecute(
  148. MagentoCheckoutControllerCartAdd $subject
  149. ) {
  150. $params = $subject->getRequest()->getParams();
  151. if (isset($params['selected_configurable_option']) && $params['selected_configurable_option'] !== '') {
  152. $params['product'] = $params['item'] = $params['selected_configurable_option'];
  153. $params['selected_configurable_option'] = '';
  154. unset($params['super_attribute']);
  155. $subject->getRequest()->setParams($params);
  156. }
  157. }
  158. }
Add Comment
Please, Sign In to add comment