在Magento产品页使用ColorBox 【magento二次开发】


ColorBox是一款非常流行的基于jQuery的Lightbox插件,非常适用于Magento的产品页面的图片展示,下面将介绍怎样把ColorBox集成到Magento中。

1 加载jQuery
首先在Magento的页面中加载jQuery库,并下载ColorBox并放在网站的根目录的js目录下,比如/js/colorbox

2 建立模块
在/app/code/local/MagentoBoy/Colorbox目录下新建一个模块MagentoBoy_Colorbox,并添加模块文件:
/app/etc/modules/MagentoBoy_Colorbox.xml
<?xml version="1.0"?>
<config>
    <modules>
        <MagentoBoy_Colorbox>
            <active>true</active>
            <codePool>local</codePool>
        </MagentoBoy_Colorbox>
    </modules>
</config>
并添加配置文件
/app/code/local/MagentoBoy/Colorbox/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MagentoBoy_Colorbox>
            <version>0.1.0</version>
        </MagentoBoy_Colorbox>
    </modules>
</config>

3 添加Layout文件
/app/design/frontend/default/default/layout/colorbox.xml
<?xml version="1.0"?>
<layout>
    <catalog_product_view>
        <reference name="head">
            <action method="addItem"><type>js</type><name>colorbox/jquery.colorbox-min.js</name></action>
            <action method="addItem"><type>js_css</type><name>colorbox/colorbox.css</name></action>
        </reference>
        <reference name="product.info.media">
            <action method="setTemplate"><template>colorbox/media.phtml</template></action>
        </reference>
    </catalog_product_view>
    <review_product_list>
        <reference name="head">
            <action method="addItem"><type>js</type><name>colorbox/jquery.colorbox-min.js</name></action>
            <action method="addItem"><type>js_css</type><name>colorbox/colorbox.css</name></action>
        </reference>
        <reference name="product.info.media">
            <action method="setTemplate"><template>colorbox/media.phtml</template></action>
            <action method="disableGallery"/>
        </reference>
    </review_product_list>
</layout>

并在config.xml中添加layout文件

<config>
    <frontend>
        <layout>
            <updates>
                <colorbox>
                    <file>colorbox.xml</file>
                </colorbox>
            </updates>
        </layout>
    </frontend>
</config>

4 修改template文件
/app/design/frontend/default/default/template/colorbox/media.phtml

<?php
    $_product = $this->getProduct();
    $_helper = $this->helper('catalog/output');
?>
<?php if ($_product->getImage() != 'no_selection' && $_product->getImage()): ?>

<p class="product-image product-image-zoom">
    <a href="<?php echo $this->helper('catalog/image')->init($_product, 'image');?>" class="mustang-gallery" title="<?php echo $this->htmlEscape($this->getImageLabel());?>">
        <img id="image" src="<?php echo $this->helper('catalog/image')->init($_product, 'image');?>" alt="<?php echo $this->htmlEscape($this->getImageLabel());?>" title="<?php echo $this->htmlEscape($this->getImageLabel());?>" style="width:265px;" />
    </a>
</p>

<p class="zoom-notice" id="track_hint"><?php echo $this->__('Click on above image to view full picture') ?></p>

<?php else: ?>
<p class="product-image">
    <?php
        $_img = '<img src="'.$this->helper('catalog/image')->init($_product, 'image')->resize(265).'" alt="'.$this->htmlEscape($this->getImageLabel()).'" title="'.$this->htmlEscape($this->getImageLabel()).'" />';
        echo $_helper->productAttribute($_product, $_img, 'image');
    ?>
</p>
<?php endif; ?>

<?php if (count($this->getGalleryImages()) > 0): ?>
<div class="more-views">
    <h2><?php echo $this->__('More Views') ?></h2>
    <ul>
    <?php foreach ($this->getGalleryImages() as $_image): ?>
        <li>
            <a href="<?php echo $this->helper('catalog/image')->init($_product, 'image', $_image->getFile());?>" class="mustang-gallery" title="<?php echo $this->htmlEscape($this->getImageLabel());?>"><img src="<?php echo Mage::helper('catalog/image')->init($_product, 'thumbnail', $_image->getFile())->resize(56);?>" width="56" height="56" alt="<?php echo $this->htmlEscape($_image->getLabel()) ?>" /></a>
        </li>
    <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>

<script type="text/javascript">
//<![CDATA[
var $j = jQuery.noConflict();
$j(document).ready(function(){
    $j(".mustang-gallery").colorbox({
        opacity :0.5,
        rel :'mustang-gallery',
        ransition :"fade",
        height :"75%"
    });
});
//]]>
</script>

清除缓存,刷新前台页面,点击产品的图片,将弹出图片的放大图片,如果需要其他效果,可以根据ColorBox的其他参数进行设置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值