Skip to content

Instantly share code, notes, and snippets.

@mp3il mp3il/gist:30d4e33ab04a57e1d650 Secret
Created Aug 21, 2015

Embed
What would you like to do?
<?php
// Get all configurable products for the carousel.
$_productCollection_configurablesOnly = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect('*')
->addAttributeToFilter('type_id','configurable')
->addAttributeToFilter('status', array('neq' => 2));
//->addAttributeToFilter('sku', array('nin' => array("SM106","SM107","SM110","SM107","SM110","SM111","SM116","SM121","SM118","SM119")));
//->addAttributeToFilter('sku', array('in' => array("SMX102")));
foreach ($_productCollection_configurablesOnly as $conf_product) {
if ($conf_product->get_status() != 2) :
// Get all simple products.
// $simple_products = $conf_product->getTypeInstance(true)->getUsedProducts(null, $conf_product);
$simple_products = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $conf_product);
// Remove disabled simple products from the list.
foreach ($simple_products as $k => $simple_product) {
$simple_product_loaded = Mage::getModel('catalog/product')->load($simple_product->getId());
if ($simple_product_loaded->getStatus() == 2) {
// Product is disabled.
unset($simple_products[$k]);
}
}
$simple_products = array_values($simple_products);
// Choose a random one.
$random_simple_product = $simple_products[rand(0, count($simple_products) - 1)];
// Get main image from the random simple product and from configurable product.
if ($random_simple_product->getImage() && $conf_product->getImage()) {
$random_simple_product_main_image = (string)$this->helper('catalog/image')->init($random_simple_product, 'white_bg_image')->keepFrame(false)->resize(500);
$conf_product_main_image = (string)$this->helper('catalog/image')->init($conf_product, 'image')->keepFrame(false)->resize(500);
} else {
$random_simple_product_main_image = "";
$conf_product_main_image = "";
}
// Get color. Replace spaces and ampersands with dashes to support URLs.
$color = str_replace('&', '-', str_replace(' ', '-', $random_simple_product->getAttributeText('color')));
?>
<div class="item">
<a href="<?php echo $conf_product->getProductUrl() . '#' . $color ?>">
<img class="hover" src="<?php echo $random_simple_product_main_image; ?>" alt="<?php echo $conf_product->getName(); ?>">
<img class="no-hover" src="<?php echo $conf_product_main_image; ?>" alt="<?php echo $conf_product->getName(); ?>">
</a>
</div>
<?php endif; ?>
<?php } ?>
</div>
<?php //endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.