Last active
October 17, 2017 20:42
-
-
Save TheRealJAG/c33ab7bd914e84e2ca7fe7e2bdea147b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Get additional products from a brand in associated categories | |
* @param $brand_id | |
* @param $product_id | |
* @param $category_ids | |
* @return $collection | |
*/ | |
public function getMoreFromBrand($brand_id, $product_id, $category_ids=0, $platform='desktop') { | |
// Determine platform for product list limit | |
if ($platform == 'desktop') $limit = Mage::getStoreConfig('brand/brand_extended_config/desktop_qty'); | |
elseif ($platform == 'mobile') $limit = Mage::getStoreConfig('brand/brand_extended_config/mobile_qty'); | |
// Get product model, return products except $product_id | |
$collection = Mage::getModel('brand/product')->getCollection() | |
->AddFieldToFilter('main_table.brand_id', $brand_id) | |
->AddFieldToFilter('main_table.product_id', array('nin' => $product_id)); | |
// Join the category table | |
$collection->join(array('category_product' => 'catalog/category_product'), 'main_table.product_id = category_product.product_id','category_product.category_id'); | |
$collection->getSelect()->join(array('product_status' => 'catalog_product_entity_int'), 'main_table.product_id = product_status.entity_id', array('product_status.value')); | |
$collection->getSelect()->join(array('product_entity' => 'catalog_product_entity'), 'product_entity.entity_id = product_status.entity_id', array('product_status.value')); | |
$collection->AddFieldToFilter('product_status.attribute_id', array('eq' => array('96'))); | |
$collection->AddFieldToFilter('product_status.value', array('eq' => array('1'))); | |
// A product will contain an array of categories it lives in, use that array to get associated products by the brand | |
if (count($category_ids) > 0) $collection->AddFieldToFilter('category_product.category_id', array('in' => array($category_ids))); | |
$collection->getSelect()->group('main_table.product_id'); | |
$collection->getSelect()->order('RAND()'); | |
$collection->getSelect()->limit($limit); | |
echo '<pre>'; | |
echo $collection->getSelect(); | |
print_r($collection->getData()); | |
exit; | |
//die($collection->getSelect()); | |
return $collection; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment