Problem Solved

  • Setting Default Product Values in Magento

    When creating new products in the Magento Admin, there are a few fields that you often fill out with the same values over and over again. Values such as the product status, tax class, stock availability, weight, etc. Wouldn't it be nice to have the system automatically assign default values to these fields every time you create a new product? Well good news! You can easily accomplish this by hooking into Magento's catalog_product_new_action event. I'm not going to cover how to create new modules in Magento, but here is the basic structure and files you'll need to accomplish this. Make sure to provide your own value anywhere Namespace is referenced below. /app/code/local/Namespace/Catalog/etc/config.xml /app/code/local/Namespace/Catalog/Model/Product/Observer.php

    1) Place the following in the config.xml file listed above.

    <?xml version="1.0"?>
    <config>
    	<modules>
            <Namespace_Catalog>
                <version>1.0.0</version>
            </Namespace_Catalog>
        </modules>
    
    	<global>
    
            <models>
    			<namespace_catalog>
    	             <class>Namespace_Catalog_Model</class>
    	        </namespace_catalog>
    		</models>
    
            <events>
    			<catalog_product_new_action>
    	        	<observers>
    		          <namespace_catalog_product_observer>
    		            <type>singleton</type>
    		            <class>namespace_catalog/product_observer</class>
    		            <method>catalog_product_new_action</method>
    		          </namespace_catalog_product_observer>
    		        </observers>
    			</catalog_product_new_action>
    		</events>
    
    	</global>
    </config>

    2) Place the following in the Observer.php file. Set product default values as desired.

    <?php
    
    class Namespace_Catalog_Model_Product_Observer
    {
    	public function catalog_product_new_action($observer) {
    
    		$product = $observer->getEvent()->getProduct();
    		$product->setStatus(1);
    		$product->setTaxClassId(2);
    
    		$stockItem = Mage::getModel('cataloginventory/stock_item');
    		$stockItem->assignProduct($product);
    		$stockItem->setData('is_in_stock', 1);
    		$stockItem->setData('qty', 1);
    
    		$product->setStockItem($stockItem);
    	}
    }

    3) Finally, you'll need to activate your new module by creating the file below with the following contents.

    /app/etc/modules/Namespace_All.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
    	<modules>
    		<Namespace_Catalog>
    			<active>true</active>
    			<codePool>local</codePool>
    		</Namespace_Catalog>
    	</modules>
    </config>
    Make sure to clear the Magento cache when you're done with this step as well! Check out the Magento wiki article on using Event/Observer for more information on this topic.

  • Using Facebook Open Graph META Tags

    When someone likes a page on your website or shares your site in a post, Facebook automatically includes a thumbnail image, title and description for the page. This is great, until Facebook finds that one unsightly image from the bowels of your site and decides to use that as your organization's social media banner.

    That being said, how do you control which image Facebook uses when your site is shared? How about the title and description? Read on.

    Facebook and Open Graph

    Open Graph is an API that allows developers to work with Facebook's platform in a structured manner. That is, it gives you a way to communicate with Facebook through the use of metadata, API calls, and other developer tools.

    When someone shares your page, Facebook looks for Open Graph metadata in your site's HTML that will help it describe your content – the thumbnail image being one of those pieces of information. Below are some basic instructions for defining the metadata necessary to specify the thumbnail image, title and description Facebook should use for your page.

    Specifying your Page's Thumbnail Image, Title and Description

    Step 1)

    Use the Open Graph debugger to see Facebook's recommendations for your specific page. This is a great tool for gaining insight into how Facebook sees your content. To use the tool, simply type in your page's full URL and hit the debug button.

    On the next page, you'll likely see some warnings like these:

    opengraph_errors

    Step 2)

    Now, open up your favorite HTML code editor and insert the following code between the <head> and </head> tags on your page. Replace the content sections with your own content, of course.

    <meta property="og:title" content="Awesome, Inc. – We're even more awesome.">
    <meta property="og:description" content="Awesome's mission is to take cool to a whole new level.">
    <meta property="og:image" content="http://YOURWEBSITE.com/YOURIMAGE.png">
    <meta property="og:image:secure_url" content="https://YOURWEBSITE.com/YOURIMAGE.png">
    <meta property="og:image:type" content="image/png">
    <meta property="og:image:width" content="1500">
    <meta property="og:image:height" content="1500">
    You'll notice that the image width and height are 1500x1500 pixels, which is what Facebook recommends. The minimum image dimensions you can use are 200x200 pixels. If you go any smaller, Facebook will ignore your recommendation and use a different image from your page.

    Also, specifying the og:image:secure_url meta property is optional. If you don't have an SSL certificate installed on your site, simply skip this step.

    Step 3)

    Run the Open Graph debugger on your page one more time. This will force Facebook to scrape your page's content and update their database with your updated metadata!

    That's it for now. You can find more information on Open Graph and images here.

  • Using console.log() in Magento

    Using the JavaScript Console to do basic debugging is very useful. Unfortunately, Magento hijacks the console object, rendering console.log() useless. There are a number of ways to get around this, but I've found this snippet to be particularly successful in restoring the console:

    var i = document.createElement('iframe');
    i.style.display = 'none';
    document.body.appendChild(i);
    window.console = i.contentWindow.console;

  • Renaming Database Tables Using Laravel 4's Schema Builder

    If you're like us and have been playing around with the Laravel 4 beta, you've probably been enjoying the database migrations and other utilities that come with it out of the box. For us, as we've been building Quillio - our upcoming service built on Laravel 4 - the database migrations and schema builder have been instrumental in keeping our local and beta development environments in sync, and then again when we're ready to go to production. And if you're like us and constantly refine and improve your application, you've probably had to rename a database table or two during development. Unfortunately, on the surface it doesn't look like Laravel 4's schema builder has an easy way to do this - but looks can often be deceiving. I've been diving into Laravel 4's core files more and more lately to get a better understanding of what it can do, especially since the documentation is still in flux and being fleshed out. It was while I was poking around these files one day that I discovered the schema builder does, in fact, have a method to rename database tables for you, making it nice and easy to call this from within your database migrations and helping you avoid writing raw SQL, which isn't very portable from driver to driver and I personally always have to look up the syntax. So how exactly do you rename a table using Laravel 4's schema builder?

    Schema::rename($from, $to);
    That's it! It really couldn't be simpler. Just call Schema::rename() and pass in the original table name and the new one you'd like to rename it to. Here's an example of how this might look in a migration where we want to rename table 'foo' to 'bar':
    <?php
    
    use IlluminateDatabaseMigrationsMigration;
    
    class RenameFooTable extends Migration {
    
    	/**
    	 * Run the migrations.
    	 *
    	 * @return void
    	 */
    	public function up()
    	{
    		Schema::rename('foo', 'bar');
    	}
    
    	/**
    	 * Reverse the migrations.
    	 *
    	 * @return void
    	 */
    	public function down()
    	{
    		Schema::rename('bar', 'foo');
    	}
    
    }
    Sometimes you just have to get your hands dirty and dive into the source code a bit and you'll uncover some simple, but incredibly useful, gems like this one. I hope this quick tip helps keep your development quick and migrations clean! We'll post more hidden features like this as we uncover them, so stay tuned!

4 Item(s)