Search code examples
phpmagento

Magento products from attribute


I have the following code, which will get all products from all orders for one logged in customer which works fine. I want to add to this code so that it only returns products from a specified attribute set. I believe I have both bits of code that I need they just won't work together.

Code I want to add to is:

<?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()); 

            echo various variables here;

    }
  }
}
?> 

Code I have used before to get products from a specified attribute set

$attrSetName = 'Beer';
            $attributeSetId = Mage::getModel('eav/entity_attribute_set')
                ->load($attrSetName, 'attribute_set_name')
                ->getAttributeSetId();

            //Load product model collecttion filtered by attribute set id
            $products = Mage::getModel('catalog/product')
                ->getCollection()
                ->addAttributeToSelect('name')
                ->addFieldToFilter('attribute_set_id', $attributeSetId);

Solution

  • You can add this piece of code to make your script work in the foreach loop.

    $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') {
       //add your logic
    }else{
       continue;
    }
    

    Update:

    <?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;
         }
    
       }
      }
    }
    print_r($uniuqProductSkus);
    
    ?>