Magento 1.9 – Get Products from Orders Where Attribute Set Equals Value

attribute-setattributesmagento-1.9module

I have the following code

    <?php
    if (Mage::getSingleton('customer/session')->isLoggedIn()) {

        /* Get the customer data */
        $customer       = Mage::getSingleton('customer/session')->getCustomer();
        /* Get the customer's email address */
        $customer_email = $customer->getEmail();
        $customer_id = $customer->getId();

    }

    $collection = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer_email', array(
        'like' => $customer_email
    ));



        $uniuqProductSkus = array();

        foreach ($collection as $order) { 

            $order_id = $order->getId(); 
            $order = Mage::getModel("sales/order")->load($order_id); 
            $ordered_items = $order->getAllItems(); 
                foreach ($ordered_items as $item) 
                { 
                if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) { 
                continue; 
                } else { 
                    array_push($uniuqProductSkus, $item->getProduct()->getSku()); 

                    $_product                 = Mage::getModel('catalog/product')->load($item->getProductId());
                    $product_small_image_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200);
                    $product_thumbnail_path   = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150);
                    $summaryData              = Mage::getModel('review/review_summary')->load($item->getProductId());


                    echo "<div style='margin-top:20px; margin-bottom:20px; border-bottom:1px solid black; padding-bottom:20px;'>";

                    echo "<div class='previous-name'><p><a href='" . $_product->getProductUrl() . "'>";
                    echo $item->getName() . "</a></p></div>";

                    echo $_product->getShortDescription();

                    ?>

                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">Find out more</a>

                    <?php

                    echo "<div class='previous-clear'></div>";

                    echo "</div>";

            }
        }
    }
    ?> 

Which works (not the neatest I know but will tidy up at the end)

I then want to add in a filter so that it only displays products that have an attribute set of 'Beer' so I have the following code

    <?php
    if (Mage::getSingleton('customer/session')->isLoggedIn()) {

      /* Get the customer data */
      $customer       = Mage::getSingleton('customer/session')->getCustomer();
      /* Get the customer's email address */
      $customer_email = $customer->getEmail();
      $customer_id = $customer->getId();

      }

                                                                       $collection =  
         Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('customer    _email', array(
    'like' => $customer_email
    ));

    $uniuqProductSkus = array();

    foreach ($collection as $order) { 

       $order_id = $order->getId(); 
       $order = Mage::getModel("sales/order")->load($order_id); 
       $ordered_items = $order->getAllItems(); 
        foreach ($ordered_items as $item) 
        { 
        if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) { 
        continue; 
        } else { 


          $product = Mage::getModel('catalog/product')->load($sku, 'sku');
          $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
          $attributeSetModel->load($product->getAttributeSetId());
          $attributeSetName = $attributeSetModel->getAttributeSetName();
         if(0 == strcmp($attributeSetName, 'Beer') {

                    array_push($uniuqProductSkus, $item->getProduct()->getSku()); 

                    $_product                 = Mage::getModel('catalog/product')->load($item->getProductId());
                    $product_small_image_path = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(200);
                    $product_thumbnail_path   = Mage::helper('catalog/image')->init($_product, 'small_image')->resize(150);
                    $summaryData              = Mage::getModel('review/review_summary')->load($item->getProductId());


                    echo "<div style='margin-top:20px; margin-bottom:20px; border-bottom:1px solid black; padding-bottom:20px;'>";

                    echo "<div class='previous-name'><p><a href='" . $_product->getProductUrl() . "'>";
                    echo $item->getName() . "</a></p></div>";

                    echo $_product->getShortDescription();

                    ?>

                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($_product->getName(), null, true) ?>">Find out more</a>

                    <?php

                    echo "<div class='previous-clear'></div>";

                    echo "</div>";

           // echo various variables here;
         }else{
            continue;
         }

       }
      }
     }
    ?> 

However this then doesn't display any products I'm guessing the error is somewhere around the line if(0 == strcmp($attributeSetName, 'Beer') but could any one point me in the right direction?

Best Answer

This is the correct code for any one that wants to achieve the same thing

    <?php
    if (Mage::getSingleton('customer/session')->isLoggedIn()) {
        /* Get the customer data */
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        /* Get the customer's email address */
        $customer_email = $customer->getEmail();
        $customer_id = $customer->getId();

    }
     $collection =  
     Mage::getModel('sales/order')->getCollection();
    $collection->addAttributeToFilter('customer_email', array(
    'like' => $customer_email
    ));

    $uniuqProductSkus = array();
    foreach ($collection as $order) { 
       $order_id = $order->getId(); 
       $order = Mage::getModel("sales/order")->load($order_id); 
       $ordered_items = $order->getAllItems(); 
        foreach ($ordered_items as $item) 
        { 
        $item->getProduct()->getSku();
        if (in_array($item->getProduct()->getSku(), $uniuqProductSkus)) { 
            continue;  
        } else { 
          $attributeSetModel = Mage::getModel("eav/entity_attribute_set");
          $attributeSetModel->load($item->getProduct()->getAttributeSetId());
         $attributeSetName = $attributeSetModel->getAttributeSetName();
         if(0 == strcmp($attributeSetName, 'Beer')) {

            array_push($uniuqProductSkus, $item->getProduct()->getSku()); 

           // echo various variables here;
         }else{
            continue;
         }

       }
      }
    }


    ?>
Related Topic