Magento - related product color swatches

时间:2016-07-11 21:45:14

标签: php html magento

I'm working with Magento EE v1.14 and i'm looking for a solution for when a user is viewing a product page to then drop swatches of related product colors if they are out of stock.

Screenshot: Highlighted out of stock related product color

Screenshot of HTML

PHP + HTML code:

<?php

$_base_product = $this->getProduct();
$base_product = Mage::getModel('catalog/product')->load($_base_product->getId());
$base_product_id = $base_product->getId();
$base_name = $base_product->getName();
$base_url = Mage::getBaseUrl();

$product_colors = Mage::getModel('catalog/product')->getCollection();
$product_colors->addAttributeToFilter('status',1); // 1 or 2
$product_colors->addAttributeToFilter('visibility',4); // 1.2.3.4
$product_colors->addAttributeToFilter('name', array('eq' => $base_name));
$product_colors->addAttributeToFilter('sku', array('neq' => $base_product->getSku()));


$product_colors_ids = $product_colors->getAllIds(); // get all products from the category
sort($product_colors_ids);

?>

<?php if(count($product_colors_ids) > 0) : ?>
    <div id="product-color-options-wrapper">
        <div id="product-color-options-container">
        <label><?php echo $this->__('Color') ?> / <span style="font-weight: normal;"><?php echo $base_product->getAttributeText('color'); ?></span></label>
            <div id="color-options-wrapper">
                <?php $_swatch_img = $base_product->getMediaGalleryImages(false)->getItemByColumnValue('label', 'swatch') ?>
                <?php  if($_swatch_img) : ?>
                <div class="current-product-wash-wrapper wash-wrapper">
                    <div class="current-product-wash-container wash-container">
                        <img src="<?php echo $this->helper('catalog/image')->init($base_product, 'small_image', $_swatch_img->getFile())->resize(33,30) ?>" alt="" title="<?php echo $base_product->getAttributeText('color') ?>" />
                    </div>
                </div>
                <?php  else : ?>
                    <!-- <span><?php echo $base_product->getColor() ?></span> -->
                <?php  endif ?>
        <?php foreach($product_colors_ids as $prod_id) : ?>
                <?php $_sister_product = Mage::getModel('catalog/product')->load($prod_id) ?>
                <?php
                $_sister_prod_imgs = $_sister_product->getMediaGallery('images');
                foreach($_sister_prod_imgs as $_sister_prod_img):
                    if($_sister_prod_img['label'] == 'swatch'):
                        $_swatch_img = $_sister_prod_img['file'];
                    endif;
                endforeach;
                ?>
                <?php if($_swatch_img): ?>
                    <div class="sister-product-wrapper wash-wrapper">
                        <div class="sister-product-container wash-container">
                            <a href="<?php echo $base_url ?><?php echo $_sister_product->getUrlKey() ?>">
                                <img src="<?php echo $this->helper('catalog/image')->init($_sister_product, 'small_image', $_swatch_img)->resize(33,30); ?>" alt="" title="<?php echo $_sister_product->getAttributeText('color') ?>">
                            </a>
                        </div>
                    </div>
                <?php endif; ?>
        <?php endforeach ?>
                <div class="clear"></div>
            </div>
        </div>
    </div>
<?php endif ?>

Any help would be appreciated! :D

1 个答案:

答案 0 :(得分:1)

**解决方案:**添加了一个if语句,使用isAvailable()函数检查库存可用性,如屏幕截图所示。

链接到屏幕截图:https://gyazo.com/abf07ba0373877836571858ee129cc22