Category: Code

  • Magento CE Adds Google’s Universal Analytics Tracking Code

    Finally, Magento has added support for the Universal Analytics tracking code. You can enable this by going to System >> Configuration >> Sales >> Google API Pick Universal Analytics from the dropdown and save the configuration!

  • Adding a File Upload Field to the Magento System.xml Configuration

    [xml] <upload_file translate=”label tooltip comment”> <label>Upload File</label> <comment>Add comments here</comment> <tooltip>Add a tooltip that appears on hover</tooltip> <frontend_type>file</frontend_type> <backend_model>adminhtml/system_config_backend_file</backend_model> <upload_dir config=”system/filesystem/media” scope_info=”1″>admin-config-uploads</upload_dir> <base_url type=”media” scope_info=”1″>admin-config-uploads</base_url> <sort_order>1</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> </upload_file> [/xml] sets the upload location for the file. In the above example, the file will be saved in a sub-folder under the media folder. e.g.…

  • Magento – Programmatically Get Order Details by Increment ID

    [php] <?php $order = Mage::getModel(‘sales/order’)->loadByIncrementId($incrementId); ?> [/php]

  • Magento – Force HTTPS for Product’s Add to Cart URL

    [php] <?php echo $this->getAddToCartUrl($_product, array(“_secure” => true)); ?> [/php]

  • Run Sentry’s Migration When Running Laravel’s Migration

    [php] <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; class CreateSentryTables extends Migration { /** * Run the migrations. * * @return void */ public function up() { Artisan::call(‘migrate’, [‘–package’ => ‘cartalyst/sentry’]); } /** * Reverse the migrations. * * @return void */ public function down() { // Not required. Laravel will use Sentry’s down function. } }…

  • Magento’s Core HTTP Helper Class – Get Remote IP Address

    Have you ever wanted to get the remote IP address of your Magento website’s visitors, the referring website’s address or even validate an IP address? Magento’s Core HTTP helper class offers a bunch of really useful functions. [php] <?php Mage::helper(‘core/http’)->getRemoteAddr($ipToLong = false); Mage::helper(‘core/http’)->getServerAddr($ipToLong = false); Mage::helper(‘core/http’)->getHttpHost($clean = true); Mage::helper(‘core/http’)->getHttpUserAgent($clean = true); Mage::helper(‘core/http’)->getHttpAcceptLanguage($clean = true); Mage::helper(‘core/http’)->getHttpAcceptCharset($clean…

  • Magento’s Scriptaculous Folder Went Missing After An Upgrade

    All the following Scriptaculous JavaScript files from Magento’s JS library disappeared after I performed an upgrade of a Magento eCommerce website. I figured that the updater must done something naughty by deleting the folder to replace it with a newer version, but must have failed to extract the new files. [code] js/scriptaculous/builder.js js/scriptaculous/effects.js js/scriptaculous/dragdrop.js js/scriptaculous/controls.js js/scriptaculous/slider.js [/code] I…

  • Magento – Check, Find and Get the Product Type (Simple, Configurable, Grouped, etc.)

    The standard product types in Magento are Simple, Configurable, Grouped, Virtual, Bundle, and Downloadable. Have you ever wanted to change the look and feel of a product based on the product type? Or, maybe custom code a logic based on the product type? The following code will help you identify the the given product’s type. [php]…

  • Magento – Flash / Session Notification Messages

    Magento offers an inbuilt flash message class and functions to display success, error, warning and notice messages. You would have seen a message similar to the one below, when submitting the Magento contact form. What’s happening there is that the contact form module redirects the visitor back to the Contact Us page by setting a…

  • Laravel – Validate Multiple Models and Combine All Error Messages

    [php] <?php $validateUser = Validator::make(Input::all(), User::$rules); $validateRole = Validator::make(Input::all(), Role::$rules); if ($validateUser->fails() OR $validateRole->fails()) : $validationMessages = array_merge_recursive($validateUser->messages()->toArray(), $validateRole->messages()->toArray()); return Redirect::back()->withErrors($validationMessages)->withInput(); endif; ?> [/php]

  • Magento – Run an Event or Function After System Configuration Save

    Open the config.xml of your module and add an event within the global declaration. <global> <events> <admin_system_config_changed_section_nameofyoursection> <observers> <yourmodule> <type>singleton</type> <class>yourmodule/observer</class> <method>adminSystemConfigChangedSectionNameofyoursection</method> </yourmodule> </observers> </admin_system_config_changed_section_nameofyoursection> </events> </global> Now create a PHP file, Observer.php, under your module’s Model folder. Model/Observer.php. Add the following code. <?php class Your_Module_Model_Observer { public function adminSystemConfigChangedSectionNameofyoursection() { // Do something here…

  • Magento – Check if Product Exists by Getting Product ID from SKU

    [php] $productSku = ‘SKU-Of-The-Product’; // Get Magento product ID by providing the SKU $productId = Mage::getModel(‘catalog/product’)->getIdBySku($productSku); // Check if a product ID was returned if ($productId) { // Product with SKU $productSku exists } else { // Product with SKU $productSku does not exist } [/php]