如何显示购物车中产品的缩略图 - Magento
How to display the thumbnails of the products in the cart - Magento
过去一个小时我一直在努力解决这个问题。希望你们能帮忙。我正在 magento 网站 1.9.0.1 上工作,并试图将当前购物车中的产品显示为其他地方的小部件。我能够显示产品名称、价格、数量和毕业总数。但是我无法显示产品图片。显示默认的 magento 占位符图像而不是产品图像。
这是我正在使用的代码块:
<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName();?>" />
</div>
<div class="cart-content-details">
<?php
$maxLength = 50;
$productName = $item->getName();
echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>';
?>
</div>
</div>
<?php }
?>
我遇到的问题是这一行:
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName();?>" />
我搜索了过去一个小时,并尝试了不同的解决方案,但无法使它们起作用,所以我想我应该显示有问题的代码。
感谢帮助和建议。谢谢
修改代码
<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<img src="<?php echo Mage::helper('catalog/image')->init($item->getProduct(), 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
</div>
<div class="cart-content-details">
<?php
$maxLength = 50;
$productName = $item->getName();
echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>';
?>
</div>
</div>
<?php }
?>
尝试 ...->init($item->getProduct(), 'image')...
,因为该产品是购物车项目 ($item
) 的组成部分 - 请参阅 Mage_Sales_Model_Order_Item::getProduct()
。
Mage_Catalog_Helper_Image::init()
方法需要 Mage_Catalog_Model_Product
的实例,但您将传递 Mage_Sales_Model_Order_Item
(或 Mage_Sales_Model_Quote_Item
)的购物车项目:
# File: app/code/core/Mage/Catalog/Helper/Image.php
/**
* Initialize Helper to work with Image
*
* @param Mage_Catalog_Model_Product $product
* @param string $attributeName
* @param mixed $imageFile
* @return Mage_Catalog_Helper_Image
*/
public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
{
// ...
}
调整后的代码应该可以工作:
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<img src="<?php echo Mage::helper('catalog/image')->init($item->getProduct(), 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
</div>
...
好的,终于可以使用了。在网上看了更多,感谢@Robbie Averill 给我一些想法。我遇到的问题是我需要显示的图像是购物车中已有的产品,因此使用:
$item->getProduct
并没有完全返回图像的 URL,只是给我空白,因此是默认的 magento 缩略图。所以我设法解决了这个问题,这可能不是最好的方法,但它确实有效。对于我使用的所有购物车产品详细信息:
$quote->getItemsCollection() as $item
这让我可以得到产品名称、价格和数量。但是要获取图像,我必须获取产品 ID 并使用它来获取缩略图。由于商店中的所有产品都是可配置产品,我不得不再添加 2 行来查找父 ID,然后将其用于缩略图。通常你只是在管理员端设置它以使用父图像,但没有为这个小部件工作,我现在不在正确的头脑中解决这个问题。所以只要输入代码就可以找到父ID。不管怎样,代码块在下面,以防万一有人遇到这个问题并需要一些东西来构建。
<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<?php
$conid = $item->getProductId();
$simpleProductId = $conid;
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($simpleProductId);
$product = Mage::getModel('catalog/product')->load($parentIds[0]);
?>
<img src="<?php echo Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
</div>
<div class="cart-content-details">
<?php
$maxLength = 50;
$productName = $item->getName();
echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>';
?>
</div>
</div>
<?php }
?>
过去一个小时我一直在努力解决这个问题。希望你们能帮忙。我正在 magento 网站 1.9.0.1 上工作,并试图将当前购物车中的产品显示为其他地方的小部件。我能够显示产品名称、价格、数量和毕业总数。但是我无法显示产品图片。显示默认的 magento 占位符图像而不是产品图像。
这是我正在使用的代码块:
<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName();?>" />
</div>
<div class="cart-content-details">
<?php
$maxLength = 50;
$productName = $item->getName();
echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>';
?>
</div>
</div>
<?php }
?>
我遇到的问题是这一行:
<img src="<?php echo Mage::helper('catalog/image')->init($item, 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName();?>" />
我搜索了过去一个小时,并尝试了不同的解决方案,但无法使它们起作用,所以我想我应该显示有问题的代码。
感谢帮助和建议。谢谢
修改代码
<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<img src="<?php echo Mage::helper('catalog/image')->init($item->getProduct(), 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
</div>
<div class="cart-content-details">
<?php
$maxLength = 50;
$productName = $item->getName();
echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>';
?>
</div>
</div>
<?php }
?>
尝试 ...->init($item->getProduct(), 'image')...
,因为该产品是购物车项目 ($item
) 的组成部分 - 请参阅 Mage_Sales_Model_Order_Item::getProduct()
。
Mage_Catalog_Helper_Image::init()
方法需要 Mage_Catalog_Model_Product
的实例,但您将传递 Mage_Sales_Model_Order_Item
(或 Mage_Sales_Model_Quote_Item
)的购物车项目:
# File: app/code/core/Mage/Catalog/Helper/Image.php
/**
* Initialize Helper to work with Image
*
* @param Mage_Catalog_Model_Product $product
* @param string $attributeName
* @param mixed $imageFile
* @return Mage_Catalog_Helper_Image
*/
public function init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
{
// ...
}
调整后的代码应该可以工作:
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<img src="<?php echo Mage::helper('catalog/image')->init($item->getProduct(), 'image')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
</div>
...
好的,终于可以使用了。在网上看了更多,感谢@Robbie Averill 给我一些想法。我遇到的问题是我需要显示的图像是购物车中已有的产品,因此使用:
$item->getProduct
并没有完全返回图像的 URL,只是给我空白,因此是默认的 magento 缩略图。所以我设法解决了这个问题,这可能不是最好的方法,但它确实有效。对于我使用的所有购物车产品详细信息:
$quote->getItemsCollection() as $item
这让我可以得到产品名称、价格和数量。但是要获取图像,我必须获取产品 ID 并使用它来获取缩略图。由于商店中的所有产品都是可配置产品,我不得不再添加 2 行来查找父 ID,然后将其用于缩略图。通常你只是在管理员端设置它以使用父图像,但没有为这个小部件工作,我现在不在正确的头脑中解决这个问题。所以只要输入代码就可以找到父ID。不管怎样,代码块在下面,以防万一有人遇到这个问题并需要一些东西来构建。
<?php
$quote = Mage::helper('checkout')->getQuote();
foreach ($quote->getItemsCollection() as $item) { ?>
<div class="cart-single">
<div class="cart-content-image">
<?php
$conid = $item->getProductId();
$simpleProductId = $conid;
$parentIds = Mage::getResourceSingleton('catalog/product_type_configurable')->getParentIdsByChild($simpleProductId);
$product = Mage::getModel('catalog/product')->load($parentIds[0]);
?>
<img src="<?php echo Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(50, 50); ?>" alt="<?php echo $item->getName(); ?>" />
</div>
<div class="cart-content-details">
<?php
$maxLength = 50;
$productName = $item->getName();
echo '<div class="cart-content-name-short">'.substr($productName, 0, $maxLength).'...</div>';
echo '<div class="cart-content-name-full">'.$item->getName().'</div>';
echo '<div class="cart-content-price">'. $this->helper('checkout')->formatPrice($item->getPrice(), 2).'</div>';
echo '<div class="cart-content-qty">Qty : '. $item->getQty().'</div>';
?>
</div>
</div>
<?php }
?>