0

目的:在Magento的类别网格视图中实现鼠标悬停时产品图像的自动更改。

我首先道歉,我不是编码员,所以请耐心等待我的愚蠢问题。

我想出了以下代码来实现图像更改功能:

<a href="TARGET URL GOES HERE"><img src="URL OF FIRST IMAGE GOES HERE" onmouseover="this.src='URL OF SECOND IMAGE GOES HERE'" onmouseout="this.src='URL OF FIRST IMAGE GOES HERE'" /></a>

但是,我需要先确定产品是否有第二张图片,如果有,我需要调出第二张图片的 URL 才能使该功能正常工作。

我想它需要一段 php 代码来做到这一点。

我很感激任何有问题的人的帮助。

最好的问候梁

PS:这里有更多关于页面的信息。变量:

<?php
    $_productCollection=$this->getLoadedProductCollection();
    $_helper = $this->helper('catalog/output');
?>

这是调用主/第一个图像的代码位。

    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(170, 255); ?>" width="170" height="255" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
4

2 回答 2

2

For Magento

To get OnMouseOver to change the image and show the default thumbnail in the product grid or list views, add the following code to the list.phtml file of your theme:

onmouseover="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(135,135) ?>';" 
onmouseout="this.src='<?phpecho $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(135,135) ?>';"

The new code will be:

<a href="<?php echo $_product->getProductUrl() ?>"
   title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"
   class="product-image">
     <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(135, 135); ?>"
          width="135" height="135"
          alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"
          onmouseover="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'thumbnail')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(135,135) ?>';"
          onmouseout="this.src='<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(135,135) ?>';"/>
</a>
于 2012-09-06T08:25:12.933 回答
0

这可以获得第二张图像,但不能调整它的大小。

<?php $_productImage = Mage::getModel('catalog/product')->load($_product->getId());foreach ($_productImage->getMediaGalleryImages() as $image) { $_productGallery = $image->getUrl();break;};echo $_productGallery; ?>

祝你好运!

于 2013-01-11T02:45:19.007 回答