<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	>

<channel>
	<title>Tridian Developer Blog</title>
	<atom:link href="http://www.tridian.com/developer-blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tridian.com/developer-blog</link>
	<description>Development tips from Tridian.  Los Angeles Web Design development tips.</description>
	<pubDate>Mon, 13 Apr 2009 16:36:27 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tier Pricing in Magento</title>
		<link>http://www.tridian.com/developer-blog/tier-pricing-in-magento/</link>
		<comments>http://www.tridian.com/developer-blog/tier-pricing-in-magento/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 23:17:08 +0000</pubDate>
		<dc:creator>chris</dc:creator>
		
		<category><![CDATA[Magento]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[ecommerce]]></category>

		<guid isPermaLink="false">http://www.tridian.com/developer-blog/?p=29</guid>
		<description><![CDATA[So, Magento is a really great piece of software, but like any relatively new product, it&#8217;s missing some things that people think are important.  One thing in particular that&#8217;s missing is a major aspect of tier pricing pertaining to configurable products.  For example, if someone were to buy 4 red shirts and 6 [...]]]></description>
			<content:encoded><![CDATA[<p>So, Magento is a really great piece of software, but like any relatively new product, it&#8217;s missing some things that people think are important.  One thing in particular that&#8217;s missing is a major aspect of tier pricing pertaining to configurable products.  For example, if someone were to buy 4 red shirts and 6 blue shirts all from the same product, then they should still receive the discount for 10 shirts of that product.  Currently Magento only applies the tier pricing if they bought say 10 red shirts (all the same color, instead of a mix of colors).  This is a big problem when involving colors and sizes - if your customer orders 5 smalls and 5 mediums, and they expect a discount at 10 items, they will be upset that they do not receive the tier pricing discount. <span id="more-29"></span></p>
<p>Luckily, I have a solution for you to try out.  Seems to work pretty good for one of Tridian&#8217;s Magento clients.  Are you curious?  Good.  Then let&#8217;s dive right in.</p>
<p>The goal for this module is to accommodate the following requirement:  If you have a tier price discount at 10 and add 5 of one color and 5 of another, the cart will adjust the price for the 2 cart items to the 10 qty price. </p>
<p>I installed it as a module that extends Mage_Catalog_Model_Product_Type_Configurable_Price. </p>
<p>1. Create your module.  I created mine in app/code/local/Tridian/Catalog.  Use your own namespace instead of Tridian, if you prefer.  Create Model and etc sub folders in Catalog.<br />
2. Add xml to Tridian_All.xml in app/etc/modules/Tridian_All.xml<br />
3. Set up rewrite in app/code/local/Tridian/Catalog/etc/config.xml<br />
4. Extend the class and save to app/code/local/Tridian/Catalog/Model/Product/Type/Configurable/Price.php </p>
<p>Here’s my app/etc/modules/Tridian_All.xml </p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc3"><span class="re1">&lt;</span>?xml <span class="re0">version</span>=<span class="st0">"1.0"</span>?<span class="re2">&gt;</span></span>
<span class="sc3"><span class="re1">&lt;config<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;modules<span class="re2">&gt;</span></span></span>
        <span class="sc3"><span class="re1">&lt;Tridian_Catalog<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;active<span class="re2">&gt;</span></span></span>true<span class="sc3"><span class="re1">&lt;/active<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;codePool<span class="re2">&gt;</span></span></span>local<span class="sc3"><span class="re1">&lt;/codePool<span class="re2">&gt;</span></span></span>
        <span class="sc3"><span class="re1">&lt;/Tridian_Catalog<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/modules<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/config<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>Here’s my app/code/local/Tridian/Catalog/etc/config.xml </p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc3"><span class="re1">&lt;</span>?xml <span class="re0">version</span>=<span class="st0">"1.0"</span>?<span class="re2">&gt;</span></span>

<span class="sc3"><span class="re1">&lt;config<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;modules<span class="re2">&gt;</span></span></span>
        <span class="sc3"><span class="re1">&lt;Tridian_Catalog<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;version<span class="re2">&gt;</span></span></span>0.1.0<span class="sc3"><span class="re1">&lt;/version<span class="re2">&gt;</span></span></span>
        <span class="sc3"><span class="re1">&lt;/Tridian_Catalog<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/modules<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;global<span class="re2">&gt;</span></span></span>
        <span class="sc3"><span class="re1">&lt;models<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;catalog<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;rewrite<span class="re2">&gt;</span></span></span>
                    <span class="sc3"><span class="re1">&lt;product_type_configurable_price<span class="re2">&gt;</span></span></span>Tridian_Catalog_Model_Product_Type_Configurable_Price<span class="sc3"><span class="re1">&lt;/product_type_configurable_price<span class="re2">&gt;</span></span></span>
                <span class="sc3"><span class="re1">&lt;/rewrite<span class="re2">&gt;</span></span></span>
            <span class="sc3"><span class="re1">&lt;/catalog<span class="re2">&gt;</span></span></span>
        <span class="sc3"><span class="re1">&lt;/models<span class="re2">&gt;</span></span></span>
    <span class="sc3"><span class="re1">&lt;/global<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/config<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>And lastly here’s the extended class </p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">class</span> Tridian_Catalog_Model_Product_Type_Configurable_Price extends Mage_Catalog_Model_Product_Type_Configurable_Price
<span class="br0">&#123;</span>
    <span class="coMULTI">/**
     * Get product final price
     *
     * @param   double $qty
     * @param   Mage_Catalog_Model_Product $product
     * @return  double
     */</span>
    public <span class="kw2">function</span> getFinalPrice<span class="br0">&#40;</span><span class="re0">$qty</span>=<span class="kw2">null</span>, <span class="re0">$product</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/is_null"><span class="kw3">is_null</span></a><span class="br0">&#40;</span><span class="re0">$qty</span><span class="br0">&#41;</span> &amp;&amp; !<a href="http://www.php.net/is_null"><span class="kw3">is_null</span></a><span class="br0">&#40;</span><span class="re0">$product</span>-&gt;<span class="me1">getCalculatedFinalPrice</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="kw1">return</span> <span class="re0">$product</span>-&gt;<span class="me1">getCalculatedFinalPrice</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
        <span class="br0">&#125;</span>

        <span class="re0">$finalPrice</span> = parent::<span class="me2">getFinalPrice</span><span class="br0">&#40;</span><span class="re0">$qty</span>, <span class="re0">$product</span><span class="br0">&#41;</span>;

        <span class="co1">// Call config product tier pricing function - Chris Lohman 1/6/2009</span>
        <span class="coMULTI">/* This allows for tier price to affect configurable items of different sizes, colors, etc.
         * so that when you have some qty of each, the total will be counted towards the tier pricing matrix.
         */</span>
        <span class="re0">$tierPrice</span> = <span class="re0">$this</span>-&gt;<span class="me1">calcConfigProductTierPricing</span><span class="br0">&#40;</span><span class="re0">$product</span><span class="br0">&#41;</span>;
        <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$tierPrice</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span>
            <span class="re0">$finalPrice</span> = <span class="re0">$tierPrice</span>;
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;<span class="re0">$finalPrice</span> = <span class="re0">$this</span>-&gt;_applySpecialPrice<span class="br0">&#40;</span><span class="re0">$product</span>, <span class="re0">$finalPrice</span><span class="br0">&#41;</span>; 
        
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$product</span>-&gt;<span class="me1">getTypeInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">setStoreFilter</span><span class="br0">&#40;</span><span class="re0">$product</span>-&gt;<span class="me1">getStore</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
        <span class="re0">$attributes</span> = <span class="re0">$product</span>-&gt;<span class="me1">getTypeInstance</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getConfigurableAttributes</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;

        <span class="re0">$selectedAttributes</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="re0">$product</span>-&gt;<span class="me1">getCustomOption</span><span class="br0">&#40;</span><span class="st0">'attributes'</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$selectedAttributes</span> = <a href="http://www.php.net/unserialize"><span class="kw3">unserialize</span></a><span class="br0">&#40;</span><span class="re0">$product</span>-&gt;<span class="me1">getCustomOption</span><span class="br0">&#40;</span><span class="st0">'attributes'</span><span class="br0">&#41;</span>-&gt;<span class="me1">getValue</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
        <span class="br0">&#125;</span>
        
        <span class="re0">$basePrice</span> = <span class="re0">$finalPrice</span>;
        <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$attributes</span> <span class="kw1">as</span> <span class="re0">$attribute</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
            <span class="re0">$attributeId</span> = <span class="re0">$attribute</span>-&gt;<span class="me1">getProductAttribute</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
            <span class="re0">$value</span> = <span class="re0">$this</span>-&gt;_getValueByIndex<span class="br0">&#40;</span>
                <span class="re0">$attribute</span>-&gt;<span class="me1">getPrices</span><span class="br0">&#40;</span><span class="br0">&#41;</span> ? <span class="re0">$attribute</span>-&gt;<span class="me1">getPrices</span><span class="br0">&#40;</span><span class="br0">&#41;</span> : <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>,
                <a href="http://www.php.net/isset"><span class="kw3">isset</span></a><span class="br0">&#40;</span><span class="re0">$selectedAttributes</span><span class="br0">&#91;</span><span class="re0">$attributeId</span><span class="br0">&#93;</span><span class="br0">&#41;</span> ? <span class="re0">$selectedAttributes</span><span class="br0">&#91;</span><span class="re0">$attributeId</span><span class="br0">&#93;</span> : <span class="kw2">null</span>
            <span class="br0">&#41;</span>;
            <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$value</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$value</span><span class="br0">&#91;</span><span class="st0">'pricing_value'</span><span class="br0">&#93;</span> != <span class="nu0">0</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
                    <span class="re0">$finalPrice</span> += <span class="re0">$this</span>-&gt;_calcSelectionPrice<span class="br0">&#40;</span><span class="re0">$value</span>, <span class="re0">$basePrice</span><span class="br0">&#41;</span>;
                <span class="br0">&#125;</span>
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
        <span class="re0">$product</span>-&gt;<span class="me1">setFinalPrice</span><span class="br0">&#40;</span><span class="re0">$finalPrice</span><span class="br0">&#41;</span>;
        <span class="kw1">return</span> <a href="http://www.php.net/max"><span class="kw3">max</span></a><span class="br0">&#40;</span><span class="nu0">0</span>, <span class="re0">$product</span>-&gt;<span class="me1">getData</span><span class="br0">&#40;</span><span class="st0">'final_price'</span><span class="br0">&#41;</span><span class="br0">&#41;</span>;
    <span class="br0">&#125;</span>

    <span class="coMULTI">/**
     * Get product final price via configurable product's tier pricing structure.  Uses qty of parent item to determine price.
     *
     * @param   float $price
     * @param   Mage_Catalog_Model_Product $product
     * @return  float
     */</span>
    public <span class="kw2">function</span> calcConfigProductTierPricing<span class="br0">&#40;</span><span class="re0">$product</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
        <span class="re0">$tierPrice</span> = <span class="nu0">0</span>;
        <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$items</span> = Mage::<span class="me2">getSingleton</span><span class="br0">&#40;</span><span class="st0">'checkout/session'</span><span class="br0">&#41;</span>-&gt;<span class="me1">getQuote</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getItemsCollection</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
        <span class="br0">&#123;</span>
            <span class="co1">//The items collection has instances of the parent and simple product.  The correct quantity value is related to the parent</span>
            <span class="co1">//so if we loop through and create an array of skus w/quantities, the quantities will fall into array items under the parent sku (array key).</span>
            <span class="re0">$cfg_quantities</span> = <a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="br0">&#41;</span>;
            <span class="kw1">foreach</span> <span class="br0">&#40;</span><span class="re0">$items</span> <span class="kw1">as</span> <span class="re0">$item</span><span class="br0">&#41;</span>
            <span class="br0">&#123;</span>
                <span class="re0">$sku</span> = <span class="re0">$item</span>-&gt;<span class="me1">getSku</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
                <span class="re0">$qty</span> = <span class="re0">$item</span>-&gt;<span class="me1">getQty</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
                <span class="re0">$cfg_quantities</span><span class="br0">&#91;</span><span class="re0">$sku</span><span class="br0">&#93;</span><span class="br0">&#91;</span><span class="br0">&#93;</span> = <span class="re0">$qty</span>;
            <span class="br0">&#125;</span>

            <span class="kw1">if</span><span class="br0">&#40;</span><a href="http://www.php.net/array_key_exists"><span class="kw3">array_key_exists</span></a><span class="br0">&#40;</span><span class="re0">$product</span>-&gt;<span class="me1">getSku</span><span class="br0">&#40;</span><span class="br0">&#41;</span>, <span class="re0">$cfg_quantities</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
            <span class="br0">&#123;</span>
                <span class="re0">$cfg_qty</span> = <a href="http://www.php.net/array_sum"><span class="kw3">array_sum</span></a><span class="br0">&#40;</span><span class="re0">$cfg_quantities</span><span class="br0">&#91;</span><span class="re0">$product</span>-&gt;<span class="me1">getSku</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#93;</span><span class="br0">&#41;</span>;
                <span class="re0">$tierPrice</span> = <span class="re0">$this</span>-&gt;<span class="me1">getTierPrice</span><span class="br0">&#40;</span><span class="re0">$cfg_qty</span>, <span class="re0">$product</span><span class="br0">&#41;</span>;
            <span class="br0">&#125;</span>
        <span class="br0">&#125;</span>
        <span class="kw1">return</span> <span class="re0">$tierPrice</span>;
    <span class="br0">&#125;</span>
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>I know - that&#8217;s a lot of code, right?  Well, let me explain how it works and you&#8217;ll see that it&#8217;s not all that bad, ok?  The theory is to use the relationship that already exists between the configurable product and its child simple products.  The function getTierPrice takes a quantity and a product as parameters, so all we need to do is figure out the total number of items in the cart that share the same parent configurable item and then pass that new total to the getTierPrice function.  This is exactly what our  calcConfigProductTierPricing function does.  Counting up the products is accomplished by taking advantage of the structure of the products collection that&#8217;s returned from the cart.  The product collection has entries for the parent and simple version of each product in the cart.  The quantity of that item in the cart is assigned to the parent member of the collection.  So when we have 2 items in the cart that share the same parent, as a red shirt and black shirt of the same product, we can loop through the collection and take the sku value from the parent and then create a secondary array of the quantity of each item.  That way, quantities for different child products will all become secondary arrays under the same key (which happens to be the sku of the parent).  Once we&#8217;ve looped through the entire cart collection, we check if the current product&#8217;s sku is in the configurable products array using the array_key_exists function, and if so, we sum the quantities of the array and then use that new total to submit to our getTierPrice function.  Now, if you offer a price break at ten items and someone buys 5 red and 5 black, they&#8217;ll get the pricing discount!  </p>
<p>Keep in mind, that if you are using a different version of Magento, like 1.1.7 or 1.1.8, etc. you should extend *YOUR* version of Mage_Catalog_Model_Product_Type_Configurable_Price, instead of copying my code from this post.  Most of the code of the function getFinalPrice in my extended class is copied directly from my 1.1.6 version of Mage_Catalog_Model_Product_Type_Configurable_Price.  The only lines I added to getFinalPrice() are the 4 lines that call my calcConfigProductTierPricing() function, then check to see if the price is greater than 0, and then apply the special price if it&#8217;s lower than the tier price.  Everything else in the function is existing code from the core class.</p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="co1">// Call config product tier pricing function - Chris Lohman 1/6/2009</span>
        <span class="coMULTI">/* This allows for tier price to affect configurable items of different sizes, colors, etc.
         * so that when you have some qty of each, the total will be counted towards the tier pricing matrix.
         */</span>
        <span class="re0">$tierPrice</span> = <span class="re0">$this</span>-&gt;<span class="me1">calcConfigProductTierPricing</span><span class="br0">&#40;</span><span class="re0">$product</span><span class="br0">&#41;</span>;
        <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$tierPrice</span> &gt; <span class="nu0">0</span><span class="br0">&#41;</span>
            <span class="re0">$finalPrice</span> = <span class="re0">$tierPrice</span>;
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$finalPrice</span> = <span class="re0">$this</span>-&gt;_applySpecialPrice<span class="br0">&#40;</span><span class="re0">$product</span>, <span class="re0">$finalPrice</span><span class="br0">&#41;</span>;</div>
</div>
</pre>
<p>So, use the getFinalPrice function from *YOUR* Magento version’s Mage_Catalog_Model_Product_Type_Configurable_Price class as your starting point, and then insert those 4 additional lines to call calcConfigProductTierPricing, and then your version of Tridian_Catalog_Model_Product_Type_Configurable_Price will be good to go. </p>
<p>Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tridian.com/developer-blog/tier-pricing-in-magento/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Adding New Arrivals to Magento</title>
		<link>http://www.tridian.com/developer-blog/adding-new-arrivals-to-magento/</link>
		<comments>http://www.tridian.com/developer-blog/adding-new-arrivals-to-magento/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 23:50:55 +0000</pubDate>
		<dc:creator>chris</dc:creator>
		
		<category><![CDATA[Magento]]></category>

		<category><![CDATA[development]]></category>

		<category><![CDATA[ecommerce]]></category>

		<guid isPermaLink="false">http://www.tridian.com/developer-blog/?p=23</guid>
		<description><![CDATA[Hey Everyone - 
So, Tridian has been doing quite a bit of Magento development work lately, and in keeping with the spirit of “open source” software, we thought we&#8217;d share a module that we recently developed that addresses a very common need when creating an e-store with Magento.  The requirements are to get the [...]]]></description>
			<content:encoded><![CDATA[<p>Hey Everyone - </p>
<p>So, Tridian has been doing quite a bit of Magento development work lately, and in keeping with the spirit of “open source” software, we thought we&#8217;d share a module that we recently developed that addresses a very common need when creating an e-store with Magento.  The requirements are to get the X most recently products added to the store (108 in this case) and display them in their own “New Arrivals” category, but make it so that the cart will automatically display the newest products based on the product&#8217;s creation date, without the site admin having to manually assigned them into and out of a “New Arrivals” category.</p>
<p>In order to create “New Arrivals” functionality on my client’s Magento store (which is 1.1.6, but this code also works for all versions of Magento through the current 1.2.0.1), I created the following module.  My approach was to override the normal category behavior for my “New Arrivals” category and display a custom product collection instead. </p>
<p>Here’s how it went down… <span id="more-23"></span></p>
<p>Step 1: In the admin, create a category called “New Arrivals” and leave it empty. </p>
<p>Step 2: Find the code that generates the product listings on the Category pages.  Turns out the code that does this is in the class Mage_Catalog_Block_Product_List.  Great.  But now, in order to get the cart to do what we want, we have to override this class and add my own custom code to handle the creation of the product collection for the empty category “New Arrivals”.  To do this, I created a new block file called Tridian_Newarrivals_Block_Newarrivals, which extends the original, and saved it here: app/code/local/Tridian/Newarrivals/Block/Newarrivals.php. </p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">class</span> Tridian_Newarrivals_Block_Newarrivals extends Mage_Catalog_Block_Product_List
<span class="br0">&#123;</span>
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;protected <span class="kw2">function</span> _getProductCollection<span class="br0">&#40;</span><span class="br0">&#41;</span>&nbsp; 
&nbsp; &nbsp; &nbsp;<span class="br0">&#123;</span>&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span><a href="http://www.php.net/is_null"><span class="kw3">is_null</span></a><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_productCollection<span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$curr_category</span> = Mage::<span class="me2">registry</span><span class="br0">&#40;</span><span class="st0">'current_category'</span><span class="br0">&#41;</span>-&gt;<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$curr_categoryid</span> = Mage::<span class="me2">registry</span><span class="br0">&#40;</span><span class="st0">'current_category'</span><span class="br0">&#41;</span>-&gt;<span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$curr_category</span> = <a href="http://www.php.net/strtolower"><span class="kw3">strtolower</span></a><span class="br0">&#40;</span><span class="re0">$curr_category</span><span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$curr_category</span> == <span class="st0">'new arrivals'</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$storeId</span>&nbsp; &nbsp; = Mage::<span class="me2">app</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getStore</span><span class="br0">&#40;</span><span class="br0">&#41;</span>-&gt;<span class="me1">getId</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$product</span>&nbsp; &nbsp; = Mage::<span class="me2">getModel</span><span class="br0">&#40;</span><span class="st0">'catalog/product'</span><span class="br0">&#41;</span>;&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;_productCollection = Mage::<span class="me2">getModel</span><span class="br0">&#40;</span><span class="st0">'catalog/resource_eav_mysql4_product_collection'</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">setStoreId</span><span class="br0">&#40;</span><span class="re0">$storeId</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">addAttributeToSelect</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">'name'</span>, <span class="st0">'price'</span>, <span class="st0">'small_image'</span><span class="br0">&#41;</span>, <span class="st0">'inner'</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">addAttributeToSelect</span><span class="br0">&#40;</span><a href="http://www.php.net/array"><span class="kw3">array</span></a><span class="br0">&#40;</span><span class="st0">'special_price'</span><span class="br0">&#41;</span>, <span class="st0">'left'</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">addAttributeToSelect</span><span class="br0">&#40;</span><span class="st0">'status'</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">setOrder</span><span class="br0">&#40;</span><span class="st0">'created_at'</span>, <span class="st0">'desc'</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">setPageSize</span><span class="br0">&#40;</span><span class="nu0">12</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -&gt;<span class="me1">addUrlRewrite</span><span class="br0">&#40;</span><span class="re0">$curr_categoryid</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ;<span class="co1">//-&gt;load(true);&nbsp; // passing true to load will echo the sql to the browser.</span>

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mage::<span class="me2">getSingleton</span><span class="br0">&#40;</span><span class="st0">'catalog/product_status'</span><span class="br0">&#41;</span>-&gt;<span class="me1">addVisibleFilterToCollection</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_productCollection<span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// add InStock filter to prevent out of stock stuff from showing up.&nbsp; Chris Lohman 1/29/2009</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mage::<span class="me2">getSingleton</span><span class="br0">&#40;</span><span class="st0">'cataloginventory/stock'</span><span class="br0">&#41;</span>-&gt;<span class="me1">addInStockFilterToCollection</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_productCollection<span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Mage::<span class="me2">getSingleton</span><span class="br0">&#40;</span><span class="st0">'catalog/product_visibility'</span><span class="br0">&#41;</span>-&gt;<span class="me1">addVisibleInSearchFilterToCollection</span><span class="br0">&#40;</span><span class="re0">$this</span>-&gt;_productCollection<span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$this</span>-&gt;_productCollection = parent::_getProductCollection<span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$this</span>-&gt;_productCollection;&nbsp; 
&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp;<span class="br0">&#125;</span>&nbsp; 
<span class="br0">&#125;</span></div>
</div>
</pre>
<p>As you can see in the code above, when the category name equals “new arrivals”, we implement the custom product collection.  If not, then the class calls the parent function, which is just the _getProductCollection() function in the Mage_Catalog_Block_Product_List class.  The key things to notice here are the page size (12), which should be chosen to match your default page size for your store (our client&#8217;s store defaults to 12 items per page), and the setOrder(’created_at’, ‘desc’) call, which sorts the results in descending order by when the product was created/added to the Magento admin.  You can also see that we&#8217;ve added some filters to the collection to keep unwanted products from showing up in the results (out of stock items, etc).</p>
<p>Great!  Sounds like we&#8217;re done, right?  Not so fast.  Turns out that the toolbar that handles the category pagination uses the size of the product collection (which in this case is the entire catalog, sorted from most recently added to least recently added).  Doh.  We only want to show the most recent 108.  In order for our result set to actually be limited to 108 products, it turns out that we also need to change the behavior of the Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection class to use our new limit instead of using the size of the returned product collection.  That leads us to&#8230;</p>
<p>Step 3: Create the following model class: app/code/local/Tridian/Newarrivals/Model/Newarrivals.php </p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="kw2">class</span> Tridian_Newarrivals_Model_Newarrivals extends Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection
<span class="br0">&#123;</span>
&nbsp; &nbsp; <span class="coMULTI">/**
&nbsp; &nbsp; &nbsp;* Render SQL for retrieve product count
&nbsp; &nbsp; &nbsp;*/</span>
&nbsp; &nbsp; public <span class="kw2">function</span> getSelectCountSql<span class="br0">&#40;</span><span class="br0">&#41;</span>
&nbsp; &nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$curr_category</span> = Mage::<span class="me2">registry</span><span class="br0">&#40;</span><span class="st0">'current_category'</span><span class="br0">&#41;</span>-&gt;<span class="me1">getName</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$curr_category</span> = <a href="http://www.php.net/strtolower"><span class="kw3">strtolower</span></a><span class="br0">&#40;</span><span class="re0">$curr_category</span><span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span><span class="br0">&#40;</span><span class="re0">$curr_category</span> == <span class="st0">'new arrivals'</span><span class="br0">&#41;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$sql</span> = <span class="st0">"select 108"</span>;
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="re0">$sql</span> = parent::<span class="me2">getSelectCountSql</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">return</span> <span class="re0">$sql</span>;
&nbsp; &nbsp; <span class="br0">&#125;</span>

<span class="br0">&#125;</span></div>
</div>
</pre>
<p>You’re probably wondering why I picked 108, right?  Well to make a long story short - my catalog pages display either 12, 24, or 36 items.  If I didn’t use a multiple of all of those values, then the toolbar would display the pagination links incorrectly.  If you don&#8217;t believe me, try it out for yourself.  As before, you see that if the category isn&#8217;t “new arrivals”, then the class falls back to its original behavior by calling the parent function.  </p>
<p>Well done!  The hard part is over.  Now all we need to do is activate our module in Magento and then set up our rewrite xml in our config.xml file to get Magento to use our new classes instead of the originals.</p>
<p>Here’s the code to tell the cart to override the original block and model classes.  Save it as config.xml and put it here app/code/local/Tridian/Newarrivals/etc/config.xml: </p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc3"><span class="re1">&lt;</span>?xml <span class="re0">version</span>=<span class="st0">"1.0"</span>?<span class="re2">&gt;</span></span>

<span class="sc3"><span class="re1">&lt;config<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;modules<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Tridian_Newarrivals<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;version<span class="re2">&gt;</span></span></span>0.1.0<span class="sc3"><span class="re1">&lt;/version<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Tridian_Newarrivals<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/modules<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;global<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;blocks<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;catalog<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;rewrite<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;product_list<span class="re2">&gt;</span></span></span>Tridian_Newarrivals_Block_Newarrivals<span class="sc3"><span class="re1">&lt;/product_list<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/rewrite<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/catalog<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/blocks<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;models<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;catalog<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;rewrite<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;resource_eav_mysql4_product_collection<span class="re2">&gt;</span></span></span>Tridian_Newarrivals_Model_Newarrivals<span class="sc3"><span class="re1">&lt;/resource_eav_mysql4_product_collection<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/rewrite<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/catalog<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/models<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/global<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/config<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>You need to activate your module in app/etc/modules.  We created a file named Tridian_All.xml for this. </p>
<pre>
<div class="codesnip-container" >
<div class="codesnip"><span class="sc3"><span class="re1">&lt;</span>?xml <span class="re0">version</span>=<span class="st0">"1.0"</span>?<span class="re2">&gt;</span></span>
<span class="sc3"><span class="re1">&lt;config<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;modules<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;Tridian_Newarrivals<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;active<span class="re2">&gt;</span></span></span>true<span class="sc3"><span class="re1">&lt;/active<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;codePool<span class="re2">&gt;</span></span></span>local<span class="sc3"><span class="re1">&lt;/codePool<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; &nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/Tridian_Newarrivals<span class="re2">&gt;</span></span></span>
&nbsp; &nbsp; <span class="sc3"><span class="re1">&lt;/modules<span class="re2">&gt;</span></span></span>
<span class="sc3"><span class="re1">&lt;/config<span class="re2">&gt;</span></span></span></div>
</div>
</pre>
<p>Just to be crystal clear, since this is where a lot of slip ups occur with Magento – make sure you follow the naming conventions and file locations to a “T”.  They are very specific and case sensitive.  </p>
<p>As a recap, here’s where your files should go: </p>
<p>Tridian_All.xml (turns on module) goes -> app/etc/modules/Tridian_All.xml<br />
Module code (3 components) goes -> app/code/local/Tridian/Newarrivals<br />
Block class Tridian_Newarrivals_Block_Newarrivals goes -> app/code/local/Tridian/Newarrivals/Block/Newarrivals.php<br />
Model class Tridian_Newarrivals_Model_Newarrivals goes -> app/code/local/Tridian/Newarrivals/Model/Newarrivals.php<br />
config.xml (xml config file for module) goes -> app/code/local/Tridian/Newarrivals/etc/config.xml </p>
<p>And there you have it.  The front end will now display your most recent products in their own “New Arrivals” category and it will always be the most recently added. </p>
<p>Feel free to post any questions or comments.</p>
<p>Cheers! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.tridian.com/developer-blog/adding-new-arrivals-to-magento/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
