Skip to content

Instantly share code, notes, and snippets.

@leo-leung
Created March 18, 2014 08:44
Show Gist options
  • Save leo-leung/9616030 to your computer and use it in GitHub Desktop.
Save leo-leung/9616030 to your computer and use it in GitHub Desktop.
<?php
$_product = $this->getProduct();
$attribute_set_id = $_product->getAttributeSetId();
$attribute_set_name = Mage::getModel('eav/entity_attribute_set')->load($attribute_set_id)->getAttributeSetName();
$product_id = $_product->getId();
//echo $product_id;
//if it is a printer, directly get printer's related product (cartridge)
if ($attribute_set_name=='printers'){
$compatible_target_product = $this->__('Cartridges');
$related_collection = $_product->getRelatedProductCollection();
$related_collection
->addAttributeToFilter('status',
array('eq'=>1)
)
->addAttributeToFilter('visibility',
array('neq'=>1)
);
$related_collection->addAttributeToSelect(array('name','small_image','detail_page_url','url_key'))
->addAttributeToFilter('attribute_set_id',10)
//->setPageSize(20)
->getSelect()->order(new Zend_Db_Expr('RAND()'))
;
/*foreach ($related_collection as $r){
Zend_debug::dump($r->debug());
}*/
}else {
$compatible_target_product = $this->__('Printers');
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->distinct(true);
//set if printer
$collection->addAttributeToFilter('attribute_set_id',13);
//$collection->addAttributeToFilter('related_ids',array('in' => $product_id));
$collection->getSelect()->join(array('links' => 'catalog_product_link'), 'e.entity_id = links.product_id');
$collection->getSelect()->where('links.linked_product_id IN (?)', $product_id);
//echo $collection->getSelect();
//echo $collection->count();
//$related_cartridges = array();
$related_printers = array();
foreach($collection as $c){
$printer_id = $c->getId();
$related_printers[] = $printer_id;
}
?>
<!--<div class="hide"><?php //Zend_debug::dump($related_printers);?></div>-->
<?php
//Zend_debug::dump($related_cartridges);
//print_r($related_printers);
//for printer, get related cartridges
$related_collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id',
array('in'=>$related_printers)
)
->addAttributeToFilter('entity_id',
array('neq'=>$product_id)
)
->addAttributeToFilter('status',
array('eq'=>1)
)
->addAttributeToFilter('visibility',
array('neq'=>1)
)
//->setPageSize(20)
;
$related_collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
}
// print_r($related_cartridges);
?>
<?php if ($related_collection->count()>0){?>
<div class="block-compatible">
<div class="block-title-wrapper">
<div class="block-compatible-title compatible-<?php echo strtolower($compatible_target_product);?>">
<strong>
<span class="title">Suitable</span>
<span class="target_title"><?php echo $compatible_target_product;?></span>
</strong>
</div>
<span class="target_desc"><?php echo $this->__('Suitable for your %s',str_replace("s","",$attribute_set_name));?></span>
</div>
<div class="block-compatible-content tinyscrollbar">
<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>
<div class="promotion-products viewport">
<div class="overview">
<?php
$count = 0;
foreach ($related_collection as $key=>$_item){
$count++;
//Zend_debug::dump($_item->getId());
?>
<?php
$is_printer = false;
if ($_item->getAttributeSetId() == 13) { // when it is a printer, load the new detail page url(ie. quickfinder one). Otherwise, load the orginal one
$is_printer = true;//Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku());
}
?>
<div class="promotion-products-related <?php if ($count<=2){ echo "static-item"; }else {echo "hide";}?>">
<div class="item-info">
<a href="<?php if ($is_printer) {echo Mage::getBaseUrl().$_item->getDetailPageUrl().".html";} else {echo $_item->getUrlKey().".html";}?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(80) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
<div class="product-details">
<h3 class="product-name"><a href="<?php if ($is_printer) {echo Mage::getBaseUrl().$_item->getDetailPageUrl().".html";} else {echo $_item->getUrlKey().".html";}?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></h3>
<?php //echo $this->getPriceHtml($_item, true, '-related') ?>
</div>
</div>
</div>
<?php } ?>
</div>
</div><!--promotion-products-->
</div>
<?if ($related_collection->count()>=3): ?>
<div class="block-compatible-more">
<a id="full-promotion-products" class="various fancybox.inline" href="#promotion-products">
<button class="button btn-info btn-view-all-compat" title="Info" type="button">
<span><span><?php echo $this->__('View all compatible %s',$compatible_target_product);?></span></span>
</button>
</a>
</div>
<?endif;?>
</div>
<?php }?>
<script>
/*jQuery(document).ready(function(){
jQuery('.btn-view-all-compat').live('click',function(){
if (jQuery('.product-essential .promotion-products-related.hide').length>=1){
//collapse mode, expand it
jQuery('.product-essential .promotion-products-related').slideDown().removeClass('hide');
jQuery('.block-compatible-more span span').html('Hide');
} else {
//expand mode , collapse it
jQuery('.product-essential .promotion-products-related').addClass('hide').slideUp();
jQuery('.product-essential .promotion-products-related.static-item').removeClass('hide');
jQuery('.block-compatible-more span span').html('View all compatible <?php echo $compatible_target_product;?>');
}
})
})*/
</script>
<style>
.product-essential .promotion-products{max-height:600px; overflow: auto;}
</style>
<!--ALL compatible products lightbox-->
<script type="text/javascript">
jQuery("#full-promotion-products").fancybox({
'scrolling' : true,
'type' : 'inline',
'autoScale': true,
'width' : 820,
'height' : 480
});
</script>
<div class="promotion-products viewport hide">
<div class="block-compatible" id="promotion-products">
<div class="block-title-wrapper">
<div class="block-compatible-title compatible-<?php echo strtolower($compatible_target_product);?>">
<strong>
<span class="title">Suitable</span>
<span class="target_title"><?php echo $compatible_target_product;?></span>
</strong>
</div>
<span class="target_desc"><?php echo $this->__('Suitable for your %s',str_replace("s","",$attribute_set_name));?></span>
</div>
<div class="overview" >
<?php
$count = 0;
foreach ($related_collection as $key=>$_item){
$count++;
//Zend_debug::dump($_item->getAttributeSetId());
?>
<?php
$is_printer = false;
if ($_item->getAttributeSetId() == 13) { // when it is a printer, load the new detail page url(ie. quickfinder one). Otherwise, load the orginal one
$is_printer = true;//Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku());
}
?>
<div class="promotion-products-related static-item" style="<?php if ($count%2 != 0){echo "float:left";}?>">
<div class="item-info">
<a href="<?php if ($is_printer) {echo Mage::getBaseUrl().$_item->getDetailPageUrl().".html";} else {echo $_item->getUrlKey().".html";}?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(80) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
<div class="product-details">
<h3 class="product-name"><a href="<?php if ($is_printer) {echo Mage::getBaseUrl().$_item->getDetailPageUrl().".html";} else {echo $_item->getUrlKey().".html";}?>"><?php echo $this->htmlEscape($_item->getName()) ?></a></h3>
<?php //echo $this->getPriceHtml($_item, true, '-related') ?>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment