在Magento中调用第二个图像URL来实现鼠标悬停时的图像更改功能


Calling a second image URL to realize image change function on mouseover in Magento

目标: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代码来完成。

如果有人提出这个问题,我将不胜感激。

致以最诚挚的问候梁

附言:以下是有关该页面的更多信息。变量:

<?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>

对于Magento

要让OnMouseOver更改图像并在产品网格或列表视图中显示默认缩略图,请将以下代码添加到主题的list.phtml文件中:

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) ?>';"

新代码将是:

<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>

这可以获得第二个图像,但不能调整其大小。

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

祝你好运!