CIS
794_The.Definitive.Guide.to.Magento.pdf

Xml version10 config modules

Info icon This preview shows pages 185–189. Sign up to view the full content.

\app\etc\modules\Definitivemagento_All.xml <?xml version="1.0"?> <config> <modules> <Definitivemagento_Helloworld> <active>true</active> <codePool>local</codePool> </Definitivemagento_Helloworld> </modules> </config> Creating this file under \app\etc\modules\ will register your custom module into your Magento site. Notice that all the Mage core files are listed here, too. The <codePool> represents the directory structure \app\code\local\. If you didn’t place your custom module package under \local, you would need to update this value. Tip The naming convention must be exact. For example, <codepool> is not the same as <codePool>. This will save you hours of debugging if you create a working template such as Helloworld, and keep it extending it for future modules.
Image of page 185

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

CHAPTER 10 MAGENTO EXTENSIONS 164 Verify that the Module Is Registered After all the files are in place, it’s best to verify whether your module has been registered correctly with Magento. From the Admin Panel, choose System, Configuration, Advanced, Advanced. Expand the Disable Modules Output row. You should see your module listed, as shown in Figure 10-6. Figure 10-6. Verifying modules Displaying Hello World in Blocks Now that Magento knows about the new modules, let’s inject the code into a Block and test it out. From the Admin Panel, choose CMS, Manage Pages. Let’s display the Helloworld module in the Home page. Select the row labeled Home page. From the General Information tab, under Content, you can add the following line of code: <h1> {{block type="Definitivemagento_Helloworld/hi"}} </h1> Figure 10-7. Calling Helloworld Note Pay close attention to which letter requires capitalization and which should be lowercase. The namespace starts with capital letters, but the block action, in this case “hi”, is lowercase.
Image of page 186
CHAPTER 10 MAGENTO EXTENSIONS 165 Refresh your Magento site and see the results, as shown in Figure 10-8. Figure 10-8. Results of Hello World Okay, now give yourself a pat on the back. You have just created a Magento module! Now let’s do some real-world stuff. Random Products Module Let’s extend the Helloworld module to query a random list of products and display them to the user. You will build your own customize models as well as use Magento’s core models. Files for Random Products Module \app \code \local \Definitivemagento \Helloworld \Block \Hi.php \Randomproducts.php \Model \Randomproducts.php \etc \config.xml \etc \modules \Definitivemagento_All.xml
Image of page 187

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

CHAPTER 10 MAGENTO EXTENSIONS 166 You added a new Model directory. It will do the heavy lifting, but you need the Block to handle the view component. \app\code\local\Definitivemagento\Helloworld\Model\Randomproducts.php <?php class Definitivemagento_Helloworld_Model_Randomproducts extends Mage_Core_Model_Abstract { public function getRandomProducts($maxCount = 5) { $randProducts = array(); $allProducts = array(); $productCollection = Mage::getModel('catalog/product') ->getCollection() ->addAttributeToSelect('*') ->getItems(); foreach ($productCollection as $id => $data) { $allProducts[] = $data; } $productIds = array_keys($allProducts); $totalProductIds = count($productIds);
Image of page 188
Image of page 189
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern