Magento – Currency Converter in multi-site multi-currency website

In Magento on August 16th, 2011

While working multi-site and multi-currency, one way to convert currency:

  1. $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
  2. $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
  3. $price = 100;
  4.  
  5. // convert price from current currency to base currency
  6. $priceOne = Mage::helper('directory')->currencyConvert($price, $currentCurrencyCode, $baseCurrencyCode);
  7.  
  8. // convert price from base currency to current currency
  9. $priceTwo = Mage::helper('directory')->currencyConvert($price, $baseCurrencyCode, $currentCurrencyCode);

Above code is copied from Mukesh’s blog.

Another way to convert currency is:

  1. $price = "100";
  2. $block = Mage::getBlockSingleton('checkout/cart_shipping');
  3. $block->getQuote()->getStore()->convertPrice(Mage::helper('tax')->getShippingPrice($price));

Above code automatically convert the base currency to current currency.

Magento 1.4.1 – deleting all orders

In Magento on August 5th, 2011

Deleting orders from Magento is little difficult. I found following SQL, and saved it for my future reference :-)

  1. SET FOREIGN_KEY_CHECKS=0;
  2. TRUNCATE `catalogsearch_query`;
  3. ALTER TABLE `catalogsearch_query` AUTO_INCREMENT=1;
  4. TRUNCATE `sales_flat_creditmemo`;
  5. TRUNCATE `sales_flat_creditmemo_comment`;
  6. TRUNCATE `sales_flat_creditmemo_grid`;
  7. TRUNCATE `sales_flat_creditmemo_item`;
  8. TRUNCATE `sales_flat_invoice`;
  9. TRUNCATE `sales_flat_invoice_comment`;
  10. TRUNCATE `sales_flat_invoice_grid`;
  11. TRUNCATE `sales_flat_invoice_item`;
  12. TRUNCATE `sales_flat_order`;
  13. TRUNCATE `sales_flat_order_address`;
  14. TRUNCATE `sales_flat_order_grid`;
  15. TRUNCATE `sales_flat_order_item`;
  16. TRUNCATE `sales_flat_order_payment`;
  17. TRUNCATE `sales_flat_order_status_history`;
  18. TRUNCATE `sales_flat_quote`;
  19. TRUNCATE `sales_flat_quote_address`;
  20. TRUNCATE `sales_flat_quote_address_item`;
  21. TRUNCATE `sales_flat_quote_item`;
  22. TRUNCATE `sales_flat_quote_item_option`;
  23. TRUNCATE `sales_flat_quote_payment`;
  24. TRUNCATE `sales_flat_quote_shipping_rate`;
  25. TRUNCATE `sales_flat_shipment`;
  26. TRUNCATE `sales_flat_shipment_comment`;
  27. TRUNCATE `sales_flat_shipment_grid`;
  28. TRUNCATE `sales_flat_shipment_item`;
  29. TRUNCATE `sales_flat_shipment_track`;
  30. TRUNCATE `sales_invoiced_aggregated`;
  31. TRUNCATE `sales_invoiced_aggregated_order`;
  32. TRUNCATE `sales_order_aggregated_created`;
  33. TRUNCATE `sendfriend_log`;
  34. TRUNCATE `tag`;
  35. TRUNCATE `tag_relation`;
  36. TRUNCATE `tag_summary`;
  37. TRUNCATE `wishlist`;
  38. TRUNCATE `log_quote`;
  39. TRUNCATE `report_event`;
  40. ALTER TABLE `sales_flat_creditmemo` AUTO_INCREMENT=1;
  41. ALTER TABLE `sales_flat_creditmemo_comment` AUTO_INCREMENT=1;
  42. ALTER TABLE `sales_flat_creditmemo_grid` AUTO_INCREMENT=1;
  43. ALTER TABLE `sales_flat_creditmemo_item` AUTO_INCREMENT=1;
  44. ALTER TABLE `sales_flat_invoice` AUTO_INCREMENT=1;
  45. ALTER TABLE `sales_flat_invoice_comment` AUTO_INCREMENT=1;
  46. ALTER TABLE `sales_flat_invoice_grid` AUTO_INCREMENT=1;
  47. ALTER TABLE `sales_flat_invoice_item` AUTO_INCREMENT=1;
  48. ALTER TABLE `sales_flat_order` AUTO_INCREMENT=1;
  49. ALTER TABLE `sales_flat_order_address` AUTO_INCREMENT=1;
  50. ALTER TABLE `sales_flat_order_grid` AUTO_INCREMENT=1;
  51. ALTER TABLE `sales_flat_order_item` AUTO_INCREMENT=1;
  52. ALTER TABLE `sales_flat_order_payment` AUTO_INCREMENT=1;
  53. ALTER TABLE `sales_flat_order_status_history` AUTO_INCREMENT=1;
  54. ALTER TABLE `sales_flat_quote` AUTO_INCREMENT=1;
  55. ALTER TABLE `sales_flat_quote_address` AUTO_INCREMENT=1;
  56. ALTER TABLE `sales_flat_quote_address_item` AUTO_INCREMENT=1;
  57. ALTER TABLE `sales_flat_quote_item` AUTO_INCREMENT=1;
  58. ALTER TABLE `sales_flat_quote_item_option` AUTO_INCREMENT=1;
  59. ALTER TABLE `sales_flat_quote_payment` AUTO_INCREMENT=1;
  60. ALTER TABLE `sales_flat_quote_shipping_rate` AUTO_INCREMENT=1;
  61. ALTER TABLE `sales_flat_shipment` AUTO_INCREMENT=1;
  62. ALTER TABLE `sales_flat_shipment_comment` AUTO_INCREMENT=1;
  63. ALTER TABLE `sales_flat_shipment_grid` AUTO_INCREMENT=1;
  64. ALTER TABLE `sales_flat_shipment_item` AUTO_INCREMENT=1;
  65. ALTER TABLE `sales_flat_shipment_track` AUTO_INCREMENT=1;
  66. ALTER TABLE `sales_invoiced_aggregated` AUTO_INCREMENT=1;
  67. ALTER TABLE `sales_invoiced_aggregated_order` AUTO_INCREMENT=1;
  68. ALTER TABLE `sales_order_aggregated_created` AUTO_INCREMENT=1;
  69. ALTER TABLE `sendfriend_log` AUTO_INCREMENT=1;
  70. ALTER TABLE `tag` AUTO_INCREMENT=1;
  71. ALTER TABLE `tag_relation` AUTO_INCREMENT=1;
  72. ALTER TABLE `tag_summary` AUTO_INCREMENT=1;
  73. ALTER TABLE `wishlist` AUTO_INCREMENT=1;
  74. ALTER TABLE `log_quote` AUTO_INCREMENT=1;
  75. ALTER TABLE `report_event` AUTO_INCREMENT=1;
  76. SET FOREIGN_KEY_CHECKS=1;

Magento – Discount Coupon in quick checkout review step

In Magento on June 15th, 2011

For Magento 1.4.1
———–

If you configure onepage checkout in Magento store, buyers will have no chance to use discount coupon. However, there’s way to do so with little customization. I have implemented coupon in review step of onepage checkout.
Magento - discount coupon in onepage checkout

1. Override Onepage checkout model – core/Mage/Checkout/controllers/OnepageController.php. Here mainly you have to override _getReviewHTML() function and add function to save/remove coupon code.

  1. function _getReviewHtml()
  2. {
  3.  $text = $this->getLayout()->getBlock('root')->toHtml();
  4.  
  5.  $text .= '<form id="discount-coupon-form" action="/checkout/onepage/coupon/" method="post">';
  6.  $text .= '<label for="coupon_code"> ' . $this->__('Enter your coupon code if you have one.') . '</label><br />';
  7.  //$text .= '<input type="hidden" name="remove" id="remove-coupone" value="0" />';
  8.  //if(!strlen($this->getCouponCode()))
  9.  $text .= '<input class="input-text" id="coupon_code" name="coupon_code" value="' . $this->_getQuote()->getCouponCode() . '"/>';
  10.  $text .= '<input type="hidden" id="coupon" name="coupon" value="1"/>';
  11.  $text .= '&nbsp;&nbsp;&nbsp;<button type="button" class="button" onclick="updateCupon()" value="' . $this->__('Apply Coupon') .'"><span>' . $this->__('Apply Coupon') .'</span></button>';
  12.  $text .= '</form>';
  13.  
  14.  return $text;
  15. }
  16.  
  17. /**
  18.  * Coupon check
  19.  */
  20. function couponAction()
  21. {
  22.  $this->loadLayout('checkout_onepage_review');
  23.  
  24.  $this->couponCode = (string) $this->getRequest()->getParam('coupon_code');
  25.  
  26.  Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
  27.  Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode(strlen($this->couponCode) ? $this->couponCode : '')
  28.    ->collectTotals()
  29.    ->save();
  30.  
  31.  $result['goto_section'] = 'review';
  32.  $result['update_section'] = array(
  33.   'name' => 'review',
  34.   'html' => $this->_getReviewHtml()
  35.  );
  36.    
  37.  $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  38. }

couponAction() update cart validating coupon number. May be you want to improve _getReviewHTML(), but I wanted quick solution, and without modifying core.

2. Edit onepage.phtml template. You need to add some javascript code.

  1. <script type="text/javascript">
  2. //<![CDATA[
  3. function updateCupon() {
  4.  $('discount-coupon-form').request({
  5.   method: 'post',
  6.   onComplete: payment.onComplete,
  7.   onSuccess: payment.onSave,
  8.   onFailure: checkout.ajaxFailure.bind(checkout),
  9.  })
  10. }
  11. //]]>
  12. </script>

This script will submit coupon with ajax using post method. And it will update review HTML using regular checkout function.

For Magento 1.4.1