产品网址问题?

时间:2011-06-20 07:25:53

标签: url magento product

我使用以下代码显示类别和点击专利类别的产品:

<?php 
$obj = new Mage_Catalog_Block_Navigation();
$cat = Mage::getModel('catalog/category')->load($obj->getCurrentCategory()->getId()); //get current cat

$subcats = $cat->getChildren(); // Get sub cats
// loop on it


<div class="page-title">
<h1><?php echo $cat['name'];?></h1>
</div>
<div id="accordion2">

 <dl class="accordion2" id="slider2">

<?php
function sortCategories($a, $b) {
return strcmp($a['name'], $b['name']);
}
?>
<?php

$categoryArray = array();
foreach(explode(',',$subcats) as $subCatid){ // split up the mage data for use
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()){
$id = $_category->getId();
$caturl  = $_category->getURL(); // get link to image
$catname = $_category->getName(); // get the name
$catdesc = $_category->getDescription(); // get the name
if($_category->getImageUrl()){
$catimg = $_category->getImageUrl(); // hey, we got image
}else{
$catimg=null;
continue;
} // this is the basic testing data, format as desired & good luck!
$catdescTwo = substr(strip_tags($catdesc), 0, 70);
$categoryArray[] = array(
'id' => $id,
'name' => $catname,
'url' => $caturl,
'desc' => $catdescTwo,
'img' => $catimg
);
}
}
usort($categoryArray, "sortCategories");
//echo  $subcats = $cat->getChildren();
$i=0;
foreach ($categoryArray as $categoryItem) {
    $i++;
//print_r($categoryItem);
$catid=$categoryItem['id'];
$catid=$catid;
echo '<dt><h2 class="productbg_'.$i.'">'.$categoryItem['name'].'<span></span></h2></dt>
<dd>';

$category = new Mage_Catalog_Model_Category();
$category->load($catid); //My cat id is 10


$prodCollection = $category->getProductCollection();

foreach ($prodCollection as $product) {
 $productid = $product->getId();
$model = Mage::getModel('catalog/product');
$product = $model->load($productid);
//echo $name = $product->getName(); ///Store all th eproduct id in $prdIds array
//echo $product->getImageUrl();

  $available = $product->getResource()->getAttribute('available')->getFrontend()->getValue($product); 
?>
<ol class="products-list" id="products-list">
<li class="item<?php if( ++$_iterator == sizeof($productCollection) ): ?> last<?php endif; ?>">

            <?php // Product Image ?>

                <?php 

$strImagePath = '';

$wtImageSize = '';

$wtProdouct = Mage::getModel('catalog/product')->load($product->getId()); 

$wtProductImages = $wtProdouct->getMediaGalleryImages(); 



// Loop through all of the images of the current product.

foreach($wtProductImages as $wtImage) {



// If we found one with the label of 'popup', thats the one we want.

if($wtImage->getLabel() == 'popup') {



// Get the image and resize it.

$strImagePath = '/media/catalog/product' . $wtImage->getFile(); 

$wtImageSize = getimagesize($_SERVER['DOCUMENT_ROOT'] . $strImagePath);

}} ?>

            <a href="<?php echo $product->getProductUrl() ?>"  class="product-image"><img id="wt-popup-<?php echo $product->getId() ?>" src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(113); ?>"  alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>"  /></a>

                <?php if(!empty($strImagePath)) { ?>

<script defer="defer" type="text/javascript">WT.showPreview("<?php echo $this->htmlEscape($product->getName()) ?>"

, "<?php echo($strImagePath); ?>", "wt-popup-<?php echo $product->getId() ?>", <?php echo $wtImageSize[0] ?>, 

<?php echo $wtImageSize[1] ?>);</script><?php } ?>

            <?php // Product description ?>

            <div class="product-shop">

                <div class="f-fix">

                    <h2 class="product-name"><a href="<?php echo $product->getProductUrl() ?>"><?php echo $name = $product->getName();?> <span> <?php  if(isset($available) && $available!=""){?>- AVAILABLE IN <?php echo $available?><?php } ?></span> </a> </h2>

                    <?php if($product->getRatingSummary()): ?>

                    <?php echo $this->getReviewsSummaryHtml($product) ?>

                    <?php endif; ?>

                    <?php echo $this->getPriceHtml($product, true) ?>

                    <?php if($product->isSaleable()): ?>


                    <?php else: ?>

                        <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>

                    <?php endif; ?>

                    <div class="desc std">

                        <?php echo $product->getShortDescription(); ?>

                        <a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($product->getName(), null, true) ?>" class="link-more"><?php echo $this->__('Learn More') ?></a>

                    </div>

                    <ul class="add-to-links">

                        <?php if ($this->helper('wishlist')->isAllow()) : ?>

                        <?php endif; ?>

                        <?php if($_compareUrl=$this->getAddToCompareUrl($product)): ?>

                            <li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare') ?></a></li>

                        <?php endif; ?>

                    </ul>

                </div>

            </div>

        </li>

</ol>
<?php }?>


<?php  }
?>


</dl>
</div>

<script type="text/javascript">
var slider2=new accordion.slider("slider2");
slider2.init("slider2","open");
</script>

但点击产品图片时,产品网址如:http://purelygourmets.com/test/index.php/catalog/product/view/id/189/s/ormeaux-et-tacaud-120-克/类别/ 4 /

我想要正确的网址:http://purelygourmets.com/test/index.php/caviar/caviar-accessories/ormeaux-et-tacaud-120-g.html

网站网址:http://purelygourmets.com/test/index.php/

由于

3 个答案:

答案 0 :(得分:0)

我自己还在学习Magento,但你试过$ product-&gt; getUrlPath();?

答案 1 :(得分:0)

尝试从后端启用seo url System-&gt; Condifuration-&gt; Web-&gt;搜索引擎优化,并从System-&gt; Configuration-&gt; Catalog-&gt; Search Engine Optimizations

答案 2 :(得分:0)

// Store root cat
$root_cat = Mage::app()->getStore()->getRootCategoryId();
$category_model = Mage::getModel('catalog/category');
$_category = $category_model->load($root_cat); // Load category model
$all_child_categories = $category_model->getResource()->getAllChildren($_category); // Get all children IDs
// Search product category IDS (all stores) for only current store IDs
$prod_cat_result = array_intersect($all_child_categories, $_product->getCategoryIds());
$prod_cat_result = array_values($prod_cat_result); // Reset array index
$_category = Mage::getModel('catalog/category')->load($prod_cat_result[0]);
echo "/".$_product->getUrlPath($_category);

这将返回完整的产品网址,但请注意,如果您的产品已分配到多个类别,则会在上面的类别/产品数组搜索中返回其匹配的第一个类别网址。

希望这会有所帮助。 干杯