Wednesday 20 August 2014

How to create coupon codes in Magento

Want to set up a promotion on your Magento eCommerce store? Would you like to give your users free shipping? Maybe offer free shipping for just a limited time only? Or perhaps buy 1, get one 50% off? This is why Magento is great! It allows you, the admin, to create very basic coupon codes and/or very specific codes all based on the conditions and actions you set.
In this tutorial, I will show you three examples of different shopping cart rules that will help you set up rules for your own eCommerce store and let your customers take advantage of these great offers!

1. Free Shipping

  1. Log in to Magento Admin.
  2. Click on Promotions –> Shopping Cart Price Rule.
  3. Fill in the fields; be sure to give the rule a Name and Description and decide whether or not you are choosing ALL Customer groups or not.
  4. In the General Information page in the coupon menu, select the Specific Coupon option.
  5. Enter a code in the coupon code field (can be letters or numbers).
  6. In the Uses Per Coupon field, specify the number of times a customer can use this coupon code if you would like to provide a limit. If not, leave blank.
  7. In the Uses Per Customer field, specify the number of times a customer can use this promotion if you would like to provide a limit. If not, leave blank.
  8. In the From/To Date menu, select a time frame for your coupon if you would like to provide one. If not, leave blank.
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Make sure these conditions are met [none for this case]:
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Make sure these actions are met:
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
And if you want something more specific, such as: Free shipping on only a certain item, follow these actions:
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Remember to click Save Rule to save all changes.

2.      Buy 1, Get One [Free] Coupon:

  • Follow steps a-h from previous example first.
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Then ensure these Actions are met:
  • Set the Discount amount to 1. This is the quantity that is discounted (the Y value, the quantity received for free).
  • [Optional] If you want to set the number needed higher than 1, e.g., to 5, set the discount Qty Step (Buy X) to 5. This is the quantity the customer must purchase in order to qualify for the free item.
  • [Optional] If you want to set it to a specific product SKU, you can enter these in the Conditions on the Actions tab.
  • Click Save Rule to save all changes.

3.       Creating a Coupon Code for a Specific Product

  • Follow steps a-h from #1.
Set Conditions
  • On left sidebar, click conditions tab.
  • Click the + button.
  • Select Product Attribute Combination.
  • Click + button.
  • Select SKU.
  • Now you will see the SKU.
  • Place your product SKU here.
Note: If you are not seeing SKU in the dropdown, go to: Catalog –> Attributes –> Manage Attributes. Search for SKU attribute and set the drop down “Use for Promo Rule Conditions” to “YES.” 
 Create Coupon Codes in Magento Tutorial  |  Pixafy.com
Set Actions
  • Under Actions tab, choose how much you’d like to discount.
  • Click Save Rule to save all changes.
Create Coupon Codes in Magento Tutorial  |  Pixafy.com
And… voila! You are now on your way to successful coupon creation in Magento!

What is PHP? PHP - Introduction



PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.
  • PHP is a recursive acronym for “PHP: Hypertext Preprocessor”.
  • PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.
  • It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
  • PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server, once started, executes even very complex queries with huge result sets in record-setting time.
  • PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support for Java and distributed object architectures (COM and CORBA), making n-tier development a possibility for the first time.
  • PHP is forgiving: PHP language tries to be as forgiving as possible.
  • PHP Syntax is C-Like.
  • PHP is forgiving: PHP language tries to be as forgiving as possible.
  • PHP Syntax is C-Like.

Thursday 14 August 2014

Add-to-cart using ajax in Magento

I will teach you to add the product in shopping cart when you will click on the Add-to-Cart button without refresh the page and this is very fast method and user like this functionality overall.
1) Include jQuery on Product Page
First download <a href=”http://docs.jquery.com/Downloading_jQuery”>jQuery</a> and then place it inside /js/jquery folder, so path would be /js/jquery/jquery.js. Next create a javascript file called noconflict.js in the jquery folder (/js/jquery/noconflict.js) . Write this code inside noconflict.js file
i) jQuery.noConflict();
Next open that catalog.xml layout file in your theme folder [app/design/frontend/base/default/layout/catalog.xml in default magento theme] and place this code inside tag <catalog_product_view>;
<reference name=”head”>
<action method=”addItem”><type>js</type><name>jquery/jquery-1.6.4.min.js</name></action>
<action method=”addItem”><type>js</type><name>jquery/noconflict.js</name></action>
</reference>
Now open any product page in your magento, and through firebug or chrome inspector, see if these two jquery files are included in your page.
2) Product Page
Next we need to make changes in product page, so that instead of form submit an ajax request is fired. To do this open the catalog/product/view.phtml file in your theme in default magento theme, the path is app\design\frontend\base\default\template\catalog\product\view.phtml
In this file you will find the javascript code as:
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;
try {
this.form.submit();
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
if (button && button != ‘undefined’) {
button.disabled = true;
}
}
}.bind(productAddToCartForm);
change this code to
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’);
}
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);
}
});
} catch (e) {
}
//End of our new ajax code
this.form.action = oldUrl;
if (e) {
throw e;
}
}
}.bind(productAddToCartForm);
Next to do a little bit of styling go to phtml file catalog/product/view/addtocart.phtml
and then find this code there:
<button type=”button” title=”<?php echo $buttonTitle ?>” class=”button btn-cart” onclick=”productAddToCartForm.submit(this)”><span><span><?php echo $buttonTitle ?></span></span></button>
change this to
<button type=”button” title=”<?php echo $buttonTitle ?>” class=”button btn-cart” onclick=”productAddToCartForm.submit(this)”><span><span><?php echo $buttonTitle ?></span></span></button>
<span id=’ajax_loader’ style=’display:none’><img src=’<?php echo $this->getSkinUrl(‘images/opc-ajax-loader.gif’)?>’/></span>
Now open your product page again and when you press the add to cart button, you should see a loading image + ajax request being sent.
3) Add to cart Controller
Next, we need to change the code at CartController.php in the addAction. Right now, we will directly change the core file, but later will show you how do this using magento best practices.
Open the class Mage_Checkout_CartController located at app\code\core\Mage\Checkout\controllers\CartController.php and find the addAction() function. In the addAction() you have the code,
just after this line <strong>$params = $this->getRequest()->getParams();</strong> place this code:
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'] = ‘SUCCESS’;
$response['message'] = $message;
}
}
} catch (Mage_Core_Exception $e) {
$msg = “”;
if ($this->_getSession()->getUseNotice(true)) {
$msg = $e->getMessage();
} else {
$messages = array_unique(explode(“\n”, $e->getMessage()));
foreach ($messages as $message) {
$msg .= $message.’<br/>’;
}
}
$response['status'] = ‘ERROR’;
$response['message'] = $msg;
} 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;
}
Save the file and go back to the product page. Now add to cart using ajax should be functional. After clicking add to cart, you see a alert box with success message.
For this first we need to change our javascript code we added in the view.phtml file. Change the jQuery.ajax function to
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);
}
}
});
and in our CartController.php addAction() method, put this new code instead of the old code
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'] = ‘SUCCESS’;
$response['message'] = $message;
//New Code Here
$this->loadLayout();
$toplink = $this->getLayout()->getBlock(‘top.links’)->toHtml();
$sidebar = $this->getLayout()->getBlock(‘cart_sidebar’)->toHtml();
$response['toplink'] = $toplink;
$response['sidebar'] = $sidebar;
}
}
} catch (Mage_Core_Exception $e) {
$msg = “”;
if ($this->_getSession()->getUseNotice(true)) {
$msg = $e->getMessage();
} else {
$messages = array_unique(explode(“\n”, $e->getMessage()));
foreach ($messages as $message) {
$msg .= $message.’<br/>’;
}
}
$response['status'] = ‘ERROR’;
$response['message'] = $msg;
} 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;
}
Now, when you click on AddtoCart button, instead of alert, the MyCart Box + Top Links will get updated. So now the user experience is much better.

Wednesday 13 August 2014

Get current Url of the page in magento

The following code gives you the current url of the page you are:-

$currentUrl = $this->helper('core/url')->getCurrentUrl();

// Gives the base url of your magento installation
$baseUrl = Mage::getBaseUrl();

// Gives the url of media directory inside your magento installation
$mediaUrl = Mage::getBaseUrl('media');
Another way to get current url
$urlRequest = Mage::app()->getFrontController()->getRequest();
$urlPart = $urlRequest->getServer('ORIG_PATH_INFO');
if(is_null($urlPart))
{
    $urlPart = $urlRequest->getServer('PATH_INFO');
}
$urlPart = substr($urlPart, 1 );
$currentUrl = $this->getUrl($urlPart);