我一直在开发自定义向上销售相关产品脚本,该脚本显示4个随机产品。
问题是:
正常产品url为:
/店铺/$productname
这有时会生成url,如:/$productname/
或类似于以下url的url:/cocatalog/product/view/id/$productID/4/s/$productname/category/$categoryid/
我想让我的所有URL都一样,所以:/shop/$productname
<div class="upsell">
<h2>You might be interested in</h2>
<?php // List mode ?>
<?php $_iterator = 0; ?>
<?php // Grid Mode ?>
<table class="products-grid upsell" id="upsell-product-table">
<tbody>
<tr>
<?php $_collectionSize = $_productCollection->count() ?>
<?php $_columnCount = $this->getColumnCount(); $_columnCount=4; ?>
<?php $i=0; foreach ($_productCollection as $_product): ?>
<?php if ($i++%$_columnCount==0): ?>
<?php endif ?>
<td>
<?php // Initiate product model
$product = Mage::getModel('catalog/product');
// Load specific product whose tier price want to update
$product ->load($_product->getId()); ?>
<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); ?>" width="125" height="125" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" /></a>
<h3 class="product-name"><a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($product, $product->getName(), 'name') ?></a></h3>
<?php echo $this->getPriceHtml($product, true) ?>
</td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
</div>
尝试
$this->getUrl('shop').$product->getUrlPath()
而不是
$product->getProductUrl()
用于图像和产品标题。
如果您的索引是最新的,则
$product->getProductUrl()
应该给你正确的产品网址
不要试图自己从url_key、url_path和基本url等部分构建url。如果你把你的网站转移到其他服务,你会遇到一些问题。此外,如果您更改产品url中类别的配置设置,您可能会遇到问题。