Showing posts with label Filtering a Magento product collection by multiple categories and Stock item and filter current product in product detail page. Show all posts
Showing posts with label Filtering a Magento product collection by multiple categories and Stock item and filter current product in product detail page. Show all posts

Thursday 24 September 2015

Filtering a Magento product collection by multiple categories and Stock item and filter current product in product detail page

Hello,

I  Want to create section to get same product of different categories of products with Stock or quantity.

after i want get product of minimum of product quantity 50%.

for that i will join 'cataloginventory/stock_item', and  'catalog/category_product'  get this collection.

this section create in product detail page that why i am filter current product using it product id.

Please Below Code it help to you.

$product_id = Mage::registry('current_product')->getId();
    $category_ids = $_product->getCategoryIds();
$collection = Mage::getModel('catalog/product')->getCollection()
 ->addAttributeToSelect('*')
 ->addAttributeToFilter('entity_id', array('neq' => $product_id))
 ->addAttributeToFilter('status', 1)
 ->addStoreFilter();
$conditions = array();
foreach ($category_ids as $categoryId) {
 if (is_numeric($categoryId)) {
   
  $conditions[] = "{{table}}.category_id = $categoryId";
 }
}
$collection->distinct(true)
->joinField('category_id', 'catalog/category_product', null, 'product_id = entity_id', implode(" OR ", $conditions), 'inner');
$collection->joinField(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left'
);
 foreach ($collection as $_item) {
   
    //echo $_item->getName()."</br>";
     $product_qty = $_item->getQty();
     $product_qty = round($product_qty);
    $product_qty = $product_qty/100;
    if($product_qty >= 0.5){
       
        echo $_item->getName()."</br>";
       
        }
       
 }

Please Below code it help getting products.