Categories
Programming

Magento – Adding a Custom Category Attribute (EAV)

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->startSetup();

$setup->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'show_sale_icon',
    array(
        'backend'           => '',
        'default'           => 0,
        'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'group'             => 'General',
        'input'             => 'select',
        'label'             => 'Show Category Sale Icon',
        'position'          => 100,
        'required'          => false,
        'source'            => 'eav/entity_attribute_source_boolean',
        'type'              => 'int',
        'user_defined'      => true,
        'visible'           => true,
        'visible_on_front'  => true,
    )
);

$setup->endSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->startSetup();

$setup->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'additional_description',
    array(
        'backend'                   => '',
        'global'                    => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
        'group'                     => 'General',
        'input'                     => 'textarea',
        'is_html_allowed_on_front'  => true,
        'is_wysiwyg_enabled'        => true,
        'label'                     => 'Additional Description',
        'position'                  => 100,
        'required'                  => false,
        'type'                      => 'text',
        'user_defined'              => true,
        'visible'                   => true,
        'visible_on_front'          => true,
    )
);

$setup->endSetup();
Categories
Programming

Magento Fix – Email Validation for New gTLDs

Have you come across the following errors in Magento?

  • Invalid email address “[email protected]
  • [email protected]” is not a valid hostname.
  • ‘domain.events’ appears to be a DNS hostname but cannot match TLD against known list
  • ‘domain.events’ appears to be a local network name but local network names are not allowed

It is probably because the Zend email validation library is not uptodate with all the new gTLDs that are being released.

Copy
/lib/Zend/Validate/Hostname.php
to
/app/code/local/Zend/Validate/Hostname.php

Navigate to line 539 and comment out the following lines of code. This is where the domain name is checked against an array of validTlds.

if (!in_array($this->_tld, $this->_validTlds)) {
    // $this->_error(self::UNKNOWN_TLD);
    // $status = false;
    // break;
}

The alternative is to add all possible TLDs into the validTlds array. I prefer the above method as I do not have to keep updating the validTlds array with hundreds of new gTLDs.

Categories
Programming

Yoast Google Analytics Reassign ‘__gaTracker’ Tracker to ‘ga’ Variable

Google Analytics by Yoast is a must have plugin for all WordPress installs. It makes adding GA trackers a breeze and it can also ping certain events to Google Analytics. The only unexplained bit of code is the issue. At some point, the ‘ga’ variable changed to ‘__gaTracker’. This raised some concerns among the techy folk.

The change affects anyone adding custom bits of Google Analytics code such as event tracking. Mostly, people would probably notice their Contact Form 7 event tracking stop working. There is a solution to this by changing all your custom trackers from ‘ga’ to ‘_gaTracker’.

The other safer way would be to remap ‘_gaTracker’ to ‘ga’ and that will allow both trackers to work. To do this, open up your Yoast Google Analytics > Settings > Advanced tab and navigate to the “Custom code:” field. Enter the following:

__gaTracker( function() {
  window.ga = __gaTracker;
});

This simply waits for ‘_gaTracker’ to be set and then assigns ‘_gaTracker’ to ‘ga’. Now, both ‘ga’ and ‘_gaTracker’ will work fine.

Source: http://leho.kraav.com/blog/restore-ga-tracker-variable-name-after-yoast-google-analytics-__gatracker-transition/

Categories
Programming

Magento – Reset File and Folder Permissions

You might have come across the following error while in Magento Connect Manager – Warning: Your Magento folder does not have sufficient write permissions. Magento needs certain file and folder permissions to be able to automatically performs upgrades and module installs.

Open your SSH and browse to your root folder. Then, run the following commands.

find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;
chmod 777 -R downloader var media

This should set your Magento file and folder permissions to the preferred state.

Categories
Programming

Magento – Get CMS Static Block Model Data (Title, Status, Content)

You can store template and HTML data in Magento’s static blocks. If you want to pull it into your PHTML templates, just call load up the model and use the data fields.

<?php
$staticBlock = Mage::getModel('cms/block')->load('static-block-identifier');
echo $staticBlock->getTitle();
echo $staticBlock->getIdentifier();
echo $staticBlock->getIsActive();
echo $staticBlock->getContent();
?>
Categories
Programming

Magento – Check if CMS Static Block is Enabled / Active

Static blocks in Magento are really useful. They offer an easy way to insert content into various parts of the template. You are only limited by your imagination. You can check if the static blocks are enabled, before printing them to screen.

<?php

if(Mage::getModel('cms/block')->load('static-block-identifier')->getIsActive()) :

    echo Mage::app()->getLayout()->createBlock('cms/block')->setBlockId('static-block-identifier')->toHtml();

endif;

?>
Categories
Programming

Magento – Get Logged In Customer’s Group ID

<?php
/* Check if customer is logged in */
$isLoggedIn = Mage::getSingleton('customer/session')->isLoggedIn();
/* If customer is logged in */
if($isLoggedIn) :
	/* Get the logged in customer's group ID */
	$customerGroupId = Mage::getSingleton('customer/session')->getCustomerGroupId();
	/* Check if the logged in customer's group ID matches with the ID you are after */
	/* Customer group IDs are listed against each group under Admin > Customers > Customer Groups */
	if($customerGroupId == 3) :
		echo 'Welcome Retailer';
	endif;
endif;
?>

You can also write static blocks or custom variables to contain group specific information. You can name the static block identifier based on the $customerGroupId and retrieve it on the front end.

Categories
Programming

How to Clone a Git Repository into the Current Directory

Somehow, most examples of Git’s clone command show a subfolder for the repository to be cloned into. I, for example, prefer navigating to the desired directory and like to clone into the current directory. The Git command has to be changed to appear as follows:

git clone [email protected]:your-username/repository-name.git .

git clone https://github.com/your-username/repository-name .

The ‘.’ (period) indicates the current folder.

Categories
Programming

Magento Fix – Blank Index Management Page (caused by GeoIP)

Do you get a blank page when navigating to your Index Management section of Magento admin? It could be that you have Openstream’s GeoIP module and that module is conflicting with Magento’s core. Navigate to app/code/community/Openstream/GeoIP/etc/config.xml (line 47) and make the changes shown below.

    <adminhtml>
        <layout>
            <updates>
                <index>
                    <geoip> <!-- Added -->
                        <file>openstream/geoip.xml</file>
                    </geoip> <!-- Added -->
                </index>
            </updates>
        </layout>
    </adminhtml>

Once you add the node and clear cache, the Index Management page comes back!

This fix was found on StackOverflow (http://stackoverflow.com/questions/23285181/magento-index-management-page-just-shows-blank).

Categories
Programming

WordPress Contact Form 7 – Tracking Form Submissions in Universal Analytics

Are your Contact Form 7 submissions not working fine? Is the ‘Thank You’ success message flashing for a second and disappearing? The problem might be in the on_sent_ok hook.

The standard recommendation on the Contact Form 7 website is to use the following code.

on_sent_ok: "_gaq.push(['_trackEvent', 'Contact Form', 'Submit']);"

The above code will work fine with Google’s Classic Analytics. For the new Universal Analytics code you need to change the JavaScript call to the following:

on_sent_ok: "ga('send', 'event', 'Contact Form', 'Submit');"

Now, if you are using Yoast’s WordPress Google Analytics plugin, you will have to change the trigger to the following:

on_sent_ok: "__gaTracker('send', 'event', 'Contact Form', 'Submit');"

I do not know why, but Yoast has decided to use ‘__gaTracker’ instead of ‘ga’.