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

For i0 imaxcount i randindex randproductid

Info icon This preview shows pages 188–191. Sign up to view the full content.

for ($i=0; $i<$maxCount; $i++) { $randIndex = rand(0,$totalProductIds); $randProductId = $productIds[$randIndex]; $randProducts[] = $allProducts[$randProductId]; } return $randProducts; } } There’s a lot going on here. Similar to the Block class, you go ahead and extend the base core Model. The purpose of this model is to fetch the random set of products and return that data to the Block. There are numerous ways to pull a list of products from your store. If you’re old school, you can go straight SQL by reviewing its open schema ( ). But the most flexible way is to use Magento’s models: $productCollection = Mage::getModel('catalog/product') This line of code allows you to ask Magento for its catalog product model. A full list of available models and methods can be found on its massive documentation page ( ). Be warned; this is not for the faint of heart. Once you have obtained the model, go ahead and grab the collection: ->getCollection() Then set the MySQL Select query to return all columns: ->addAttributeToSelect('*')
Image of page 188

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

CHAPTER 10 MAGENTO EXTENSIONS 167 And finally return it as an array of items: ->getItems(); Note The quickest way to view what type of data is being returned is to reverse engineer the methods. Perform var_export or var_dump within PHP. Then you can see how the data is set up within the objects. Now that you have all the products in the store, let’s clean up the result set. We performed a simple loop to extract only the product objects: foreach ($productCollection as $id => $data) { $allProducts[] = $data; } Within each product objects are key value arrays: $productIds = array_keys($allProducts); This will return all the product ids so you can perform a random draw. You’ll use the rand() method from PHP to extract five products from the collection ( ). To use the rand() method, give PHP a start number and an ending number. Because an array index starts at 0, you need to obtain only the end value, which is the total size of the array: $totalProductIds = count($productIds); Now you loop five times: for ($i=0; $i<$maxCount; $i++) Draw a random number between 0 and the total size of the array: $randIndex = rand(0,$totalProductIds); Extract the random productId from the array: $randProductId = $productIds[$randIndex]; Then store the product object into the result set: $randProducts[] = $allProducts[$randProductId];
Image of page 189
CHAPTER 10 MAGENTO EXTENSIONS 168 The model is complete. Let’s move into the Block layer. \app\code\local\Definitivemagento\Helloworld\Block\Randomproducts.php: class Definitivemagento_Helloworld_Block_Randomproducts extends Mage_Core_Block_Template { protected function _toHtml() { $randProdModel = Mage::getModel('Definitivemagento_Helloworld/Randomproducts'); $randProducts = $randProdModel->getRandomProducts(); $html = "<ul>"; foreach ($randProducts as $product) { $name = $product->getName(); $price = number_format($product->getPrice(), 2); $imageLink = $this->helper('catalog/image') ->init($product, 'thumbnail')->resize(100,100); $productLink = $this->helper('catalog/product')->getProductUrl($product); $html .= " <p>
Image of page 190

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

Image of page 191
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