Popular Posts

Saturday, 3 May 2014

How to fix Facebook Opengraph 2.0 extension in Magento

If you installed the Facebook Opengraph 2.0 in Magento Community 1.5 version you’ll definitely experiencing a frustrated issue. You can also read some user reviews in the extension page. Most of them are having problems about installation and most of them installed it Magento CE 1.5 version.
Since, I need this extension badly, I examined their codes and as well as the table that it should created upon installing it. I noticed that the extension is not creating the table they stated in config.xml. In that case, the extension will not definitely work. But I can say that the code is well structured but I think it is not fully tested  in 1.5 version.

Quick Solution:

So what can you do to make the extension work is to create manually the table on your magento database after installing the extension.

Here is the SQL query:

CREATE TABLE `social_facebook_actions` (
      `entity_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Entity Id',
      `facebook_id` varchar(100) NOT NULL COMMENT 'Facebook User Id',
      `facebook_name` varchar(100) NOT NULL COMMENT 'Facebook User Name',
      `facebook_action` smallint(5) unsigned NOT NULL COMMENT 'User Action',
      `item_id` int(10) unsigned NOT NULL COMMENT 'Product Id',
      PRIMARY KEY (`entity_id`)
    ) 

I know that it is not the right way to solve this but I think this is 
the fastest way to make the extension work. After that you need to 
follow their guide in setting up a facebook app.



That's it. You can now test those opengraph buttons. :)

I'm happy to help so let me know if you have questions.
 
 
........................................... :) 

Google Analytics in Older Version of Magento

Magento is one the many platforms that use Google Analytics as a tracking system. They have a build core module that allows site owner to implement GA tracking code by simply putting their GA account in Magento Configuration panel. However, there's an issue that it is not working anymore in older version of magento for the reason that GA upadted their tracking code.

GA core module for older version of magento is still using the old GA tracking script. In order to make it functional again, It's either you update your magento or simply edit the GA core module file.

Editing GA core Module

1.  Make a copy of this file (/app/code/core/Mage/GoogleAnalytics/Block/Ga.php) to your local folder. So, you will have this directory in your local folder - /app/code/local/Mage/GoogleAnalytics/Block/Ga.php


2.  Open the copied file in an editor.
3.  Go to line 171 and look for the following code:


$this->addText('
    <!-- BEGIN GOOGLE ANALYTICS CODE -->
    <script type="text/javascript">
    //<![CDATA[
        (function() {
            var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
            ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
            (document.getElementsByTagName(\'head\')[0] || document.getElementsByTagName(\'body\')[0]).appendChild(ga);
        })();
 
        _gaq.push(["_setAccount", "' . $this->getAccount() . '"]);
        _gaq.push(["_trackPageview", "'.$this->getPageName().'"]);
    //]]>
    </script>
    <!-- END GOOGLE ANALYTICS CODE -->
');

 =======================================================================

Replace it with this: 

$this->addText('
    <!-- BEGIN GOOGLE ANALYTICS CODE -->
    <script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push([\'_setAccount\', \'' . $this->getAccount() . '\']);
    _gaq.push([\'_trackPageview\']);
    (function() {
    var ga = document.createElement(\'script\'); ga.type = \'text/javascript\'; ga.async = true;
    ga.src = (\'https:\' == document.location.protocol ? \'https://ssl\' : \'http://www\') + \'.google-analytics.com/ga.js\';
    var s = document.getElementsByTagName(\'script\')[0]; s.parentNode.insertBefore(ga, s);
    })();
 
    </script>
    <!-- END GOOGLE ANALYTICS CODE -->
');
 
 
Save it. You will now be able to see data in your GA account. 
 
 
 
.......................................... :) 



Magento: Fix IE9 issue on Payment Method


We're distress from customer complaints regarding on how one of our site functioning in IE9 browser. I thought it was a relief when Microsoft released the IE9 browser; finally, they were able adhere their promised to implement the W3C standards. Unfortunately, there are some still issues that they need to fix.

One of the major issues that we’ve seen is in relation with Magento Platform. In you have only one payment method enabled in magento, your customer can't checkout because IE9 browser disable the payment method. Please see this screenshot.

There are two quick solutions that you probably need to do in order to fix this issue until Microsoft or Magento have permanent solution about it. First solution
Add a meta tag to your page header or in your header.phtml file. 

IMP:-
<meta http-equiv="X-UA-Compatible" content="IE=8" />
This meta tag will force the IE9 browser to load your content same with IE8 version. In that way, the payment method will not be disabled in default.
Second Solution The second solution is the same with the first provided you will be adding the code in .htaccess file.
BrowserMatch MSIE best-standards-support Header set X-UA-Compatible IE=8 env=best-standards-support
With this solution, you can simply remove this code when the issue is fixed permanently.

How to display subcategories anywhere in Magento

If you’re planning to display the subcategories of a specific root/parent category anywhere you can simply use the following code and paste it in your the phtml files that you want it to show. Just please take note that you need to replace the id in the code.

<?php $root = Mage::getModel('catalog/category')->load(2); // Put your category ID here. //get the children of your loaded category and exploded in array $subCat = explode(',',$root->getChildren()); //get the collection add filter using the exploded categories $collection = $root->getCollection()->addAttributeToSelect("*")->addFieldToFilter("entity_id", array("in", $subCat) ); //loop for each subscategory in a collection foreach($collection as $subcategory) { echo '<a href="'.$subcategory->getURL() .'" />'.$subcategory->getName().'</a><br/>'; } ?>


You can get the ID in your admin panel by going to Catalog>Manage Categories. Click the category that you want in the category tree. Then look for the ID beside the category name.

Magento: How to check if User is logged in

In order to for you to do that you need to check if the user logged in using 
the following code:


<?php if(Mage::getSingleton('customer/session')->isLoggedIn()): ?>

 //paste your coupon code or private information here. 

<?php endif; ?>
 
 
Also you can use this helper method. It does exactly the same as abov
 

<?php if($this->helper('customer')->isLoggedIn()): ?> //paste your coupon code or private information here. <?php endif; ?>

Magento: get skin url, get media url, get base url, get store url

To Retrieve URL path in STATIC BLOCK

SKIN URL
{{skin url='images/sampleimage.jpg'}}

{{skin url='images/sampleimage.jpg'}}

To get Media URL
{{media url='/sampleimage.jpg'}}

To get Store URL
{{store url='mypage.html'}}

To get Base URL
{{base url='yourstore/mypage.html'}}
TO Retrieve URL path in PHTML
Note: In editing PHTML don't forget to enclode the following code with PHP tag

Not secure Skin URL:
<?php echo $this->getSkinUrl('images/sampleimage.jpg') ?>
 
Secure Skin URL
<?php echo $this->getSkinUrl('images/ sampleimage.gif', array('_secure'=>true)) ?>
 
Get  Current URL
$current_url = Mage::helper('core/url')->getCurrentUrl();
 
Get Home URL
$home_url = Mage::helper('core/url')->getHomeUrl();
 
Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK);

Get Magento Media Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA);
 
Get Magento Skin Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_SKIN);
 
Get Magento Store Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
 
Get Magento Js Url
Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_JS);

Friday, 2 May 2014

PHP Secure E-mails

<html>
<body>
<?php
function spamcheck($field) {
  // Sanitize e-mail address
  $field=filter_var($field, FILTER_SANITIZE_EMAIL);
  // Validate e-mail address
  if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
    return TRUE;
  } else {
    return FALSE;
  }
}
?>

<h2>Feedback Form</h2>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"])) {
  ?>
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  From: <input type="text" name="from"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="40" name="message"></textarea><br>
  <input type="submit" name="submit" value="Submit Feedback">
  </form>
  <?php
} else {  // the user has submitted the form
  // Check if the "from" input field is filled out
  if (isset($_POST["from"])) {
    // Check if "from" email address is valid
    $mailcheck = spamcheck($_POST["from"]);
    if ($mailcheck==FALSE) {
      echo "Invalid input";
    } else {
      $from = $_POST["from"]; // sender
      $subject = $_POST["subject"];
      $message = $_POST["message"];
      // message lines should not exceed 70 characters (PHP rule), so wrap it
      $message = wordwrap($message, 70);
      // send mail
      mail("webmaster@example.com",$subject,$message,"From: $from\n");
      echo "Thank you for sending us feedback";
    }
  }
}
?>
</body>
</html>

Simple AJAX dropdown for Country State City from Single Table

Step One -Create a tables as follows


CREATE TABLE IF NOT EXISTS `cities` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_id` int(11) NOT NULL,
`city_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=24 ;
INSERT INTO `cities` (`id`, `state_id`, `city_name`) VALUES
(1, 1, ‘Nehru Place’),
(2, 1, ‘Mehrauli’),
(3, 1, ‘Govindpuri’),
(4, 1, ‘Janakpuri’),
(5, 2, ‘Patna’),
(6, 2, ‘Bhagalpur’),
(7, 2, ‘Munger’),
(8, 2, ‘Jamalpur’),
(9, 7, ‘Al Ghabah ‘),
(10, 7, ‘Al Ghabam’),
(11, 7, ‘ Al Ghashban’),
(12, 7, ‘Al Hamraniya ‘),
(13, 8, ‘Bam’),
(14, 8, ‘Shiraj’),
(15, 8, ‘Minab’),
(16, 11, ‘Adelanto’),
(17, 11, ‘Agoura Hills ‘),
(18, 11, ‘Angels Camp’),
(19, 11, ‘Apple Valley ‘),
(20, 12, ‘Alachua’),
(21, 12, ‘Astatula’),
(22, 12, ‘Bascom’),
(23, 12, ‘Belle Glade’);
CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;
INSERT INTO `countries` (`id`, `country_name`) VALUES
(1, ‘India ‘),
(2, ‘UAE’),
(3, ‘USA’);
CREATE TABLE IF NOT EXISTS `states` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_id` int(11) NOT NULL,
`state_name` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;
INSERT INTO `states` (`id`, `country_id`, `state_name`) VALUES
(1, 1, ‘Delhi’),
(2, 1, ‘Bihar’),
(7, 2, ‘Dubai’),
(8, 2, ‘Sharjah’),
(11, 3, ‘Calofornia’),
(12, 3, ‘Florida’);
Step-Two  Create a connection file as follows
<?php
$con=mysql_connect(‘localhost’,’root’,”);
mysql_select_db(‘test’,$con);
$query=mysql_query(“SELECT * FROM countries”);
while($row=mysql_fetch_assoc($query))
{
$countries[]=$row;
}
?>
Step Three-Include a javascript library to make AJAX work
Step Four-Create a main file to be run from browser as follows
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
<html>
<head>
<script type=”text/javascript” src=”jquery-1.8.0.min.js”></script>
<?php
include(‘connection.php’);
?>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″  />
<title>Drop Down</title>
</head>
<body>
<form name=”dropdown” action=”” method=”post”>
<table align=”center”>
<tr>
<td>Select Country</td>
<td><select name=”country” onChange=”getState(this.value);” id=”country”>
<option value=””>–Select–</option>
<?php
foreach($countries as $country){ ?>
<option value=”<?php echo $country['id'] ;?>”><?php echo $country['country_name'] ;?></option>
<?php }?>
</select></td>
</tr>
<tr>
<td>Select State</td>
<td><select name=”state” onChange=”getCity(this.value);” id=”state”>
<option value=””>–Select–</option>
</select></td>
</tr>
<tr>
<td>Select City</td>
<td><select name=”city”  id=”city”>
<option value=””>–Select–</option>
</select></td>
</tr>
</table>
</form>
</body>
</html>
<script type=”text/javascript”>
function getState(id)
{
if(id==”)
{
$(‘#state’).html(‘<option value=””>–Select–</option>’);
$(‘#city’).html(‘<option value=””>–Select–</option>’);
$(‘#state’).prop(‘disabled’,true);
$(‘#city’).prop(‘disabled’,true);
return
}
if(id!=”)
{
$(‘#city’).html(‘<option value=””>–Select–</option>’);
}
$.post(‘ajax_backend.php’, {act:’getState’,’Id’:id},function(data){
if(data != ” && data != ‘error’){
$(‘#state’).html(data);
$(‘#state’).prop(‘disabled’,false);
}
});
}
function getCity(id)
{
if(id==”)
{
$(‘#city’).html(‘<option value=””>–Select–</option>’);
$(‘#city’).prop(‘disabled’,true);
return
}
$.post(‘ajax_backend.php’, {act:’getCity’,’Id’:id},function(data){
if(data != ” && data != ‘error’){
$(‘#city’).html(data);
$(‘#city’).prop(‘disabled’,false);
}
});
}
$().ready(function(){
$(‘#state’).prop(‘disabled’,true);
$(‘#city’).prop(‘disabled’,true);
});
</script>
Step Five-Create a ajax_backend.php for communicating with AJAX
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” >
 <html >
<head>
<?php
include(‘connection.php’);
$action =”;
if(isset($_REQUEST['act']) && $_REQUEST['act'] != ”){
$action = $_REQUEST['act'];
}
if($action == ‘getState’){
$country_id = ”;
if(isset($_POST['Id'])){
$country_id = $_POST['Id'];
}
$data =   “<option value=”>–Select–</option>”;
if($country_id == ” || $country_id == 0){
echo $data; exit;
}
$state_list = getState($country_id);
if(!empty($state_list)){
foreach($state_list as $state){
$data .=   “<option value=’”.$state['id'].”‘>”.$state['state_name'].”</option>”;
}
}
echo $data; exit;
}
if($action == ‘getCity’){
$city_id = ”;
if(isset($_POST['Id'])){
$city_id = $_POST['Id'];
}
$data =   “<option value=”>–Select–</option>”;
if($city_id == ” || $city_id == 0){
echo $data; exit;
}
$city_list = getCity($city_id);
if(!empty($city_list)){
foreach($city_list as $city){
$data .=   “<option value=’”.$city['id'].”‘>”.$city['city_name'].”</option>”;
}
}
echo $data; exit;
}
function getState($country_id){
$sql = “SELECT * FROM states WHERE  country_id  = ‘”.$country_id.”‘ ORDER BY state_name”;
$rs = mysql_query($sql) or die(mysql_error());
$data = array();
if(mysql_num_rows($rs) > 0){
while($row = mysql_fetch_assoc($rs)){
$data[] = $row;
}
}
return $data;
}
function getCity($state_id){
$sql = “SELECT * FROM cities WHERE state_id = ‘”.$state_id.”‘ ORDER BY city_name”;
$rs = mysql_query($sql) or die(mysql_error());
$data = array();
if(mysql_num_rows($rs) > 0){
while($row = mysql_fetch_assoc($rs)){
$data[] = $row;
}
}
return $data;
}
?>

Magento: How to get last order id

There are many ways to get last order id:   1. From checkout session: $lastOrderId = Mage::getSingleton('checkout/session'...