来自属性的Magento产品

时间:2015-04-07 12:22:05

标签: magento

我有以下代码,它将从一个登录客户的所有订单中获取所有产品,该工作正常。我想添加到此代码,以便它只返回指定属性集中的产品。我相信我有两个代码,我需要他们才能赢得合作。

我要添加的代码是:

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

    }
  }
}
?> 

我以前用过的代码来从指定的属性集中获取产品

$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);

1 个答案:

答案 0 :(得分:1)

您可以添加这段代码,使您的脚本在foreach循环中运行。

$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;
}

<强>更新

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

?>