how i can get category url through product url
i have product url but i want to get category url
i want to redirect into category page when user click on product name
Site Link
my code is :
<?php
if($this->getItems()->getSize()):
$category = new Mage_Catalog_Model_Category();
$category->load($categoryid);
$collection = $category->getProductCollection();
$collection->addAttributeToSelect('*');
?>
<div class="col-lg-7 col-md-7 col-sm-7 cpl-xs-12">
<div class="ndlSimilarTop">
<div class="ndlSimilatProductTitle">RECOMMENDED PRODUCTS</div>
<div id="amazingcarousel-container-1" style="overflow:hidden">
<div id="amazingcarousel-1" style="display:block;position:relative;width:100%;max-width:711px;margin:0px auto 0px;">
<div class="amazingcarousel-list-container" style="overflow:hidden;">
<ul class="amazingcarousel-list">
<?php foreach($this->getItems() as $_item): ?>
<li class="amazingcarousel-item">
<div class="amazingcarousel-item-container">
<div class="amazingcarousel-image">
<img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(217, 173) ?>" alt="product-img" />
<ul class="mask mask1">
<li class="ndlHoverContent ndlHoverContent1">
<img src="<?php echo $this->getSkinUrl() ?>images/wishlist-white-icon.png" alt="">Add to wishlist
<img src="<?php echo $this->getSkinUrl() ?>images/mybag-white-icon.png" alt="">Add to bag
</li>
</ul>
</div>
<?php echo "Product Url:".$_item->getProductUrl();
echo "CAT Url:".$_item->getCategoryUrl();
?>
<div class="ndlListDetail">
<div class="ndlProductListName"><?php echo $this->htmlEscape($_item->getName()) ?>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
</div>
</div>
</li>
<?php endforeach; ?>
One such solution can be below:
<?php
$productUrlPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
$rewrite = Mage::getModel('core/url_rewrite')
->setStoreId(Mage::app()->getStore()->getId())
->loadByRequestPath($productUrlPath);
$productId = $rewrite->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
$categoriesId = $product->getCategoryIds();
foreach($categoriesId as $categoryId){
$category = Mage::getModel('catalog/category')->load($categoryId);
echo $category->getUrl();
}
?>
Note: You can have more than one category of product.
Related
I'm working with Magento EE v1.14 and i'm looking for a solution for when a user is viewing a product page to then drop swatches of related product colors if they are out of stock.
Screenshot: Highlighted out of stock related product color
Screenshot of HTML
PHP + HTML code:
<?php
$_base_product = $this->getProduct();
$base_product = Mage::getModel('catalog/product')->load($_base_product->getId());
$base_product_id = $base_product->getId();
$base_name = $base_product->getName();
$base_url = Mage::getBaseUrl();
$product_colors = Mage::getModel('catalog/product')->getCollection();
$product_colors->addAttributeToFilter('status',1); // 1 or 2
$product_colors->addAttributeToFilter('visibility',4); // 1.2.3.4
$product_colors->addAttributeToFilter('name', array('eq' => $base_name));
$product_colors->addAttributeToFilter('sku', array('neq' => $base_product->getSku()));
$product_colors_ids = $product_colors->getAllIds(); // get all products from the category
sort($product_colors_ids);
?>
<?php if(count($product_colors_ids) > 0) : ?>
<div id="product-color-options-wrapper">
<div id="product-color-options-container">
<label><?php echo $this->__('Color') ?> / <span style="font-weight: normal;"><?php echo $base_product->getAttributeText('color'); ?></span></label>
<div id="color-options-wrapper">
<?php $_swatch_img = $base_product->getMediaGalleryImages(false)->getItemByColumnValue('label', 'swatch') ?>
<?php if($_swatch_img) : ?>
<div class="current-product-wash-wrapper wash-wrapper">
<div class="current-product-wash-container wash-container">
<img src="<?php echo $this->helper('catalog/image')->init($base_product, 'small_image', $_swatch_img->getFile())->resize(33,30) ?>" alt="" title="<?php echo $base_product->getAttributeText('color') ?>" />
</div>
</div>
<?php else : ?>
<!-- <span><?php echo $base_product->getColor() ?></span> -->
<?php endif ?>
<?php foreach($product_colors_ids as $prod_id) : ?>
<?php $_sister_product = Mage::getModel('catalog/product')->load($prod_id) ?>
<?php
$_sister_prod_imgs = $_sister_product->getMediaGallery('images');
foreach($_sister_prod_imgs as $_sister_prod_img):
if($_sister_prod_img['label'] == 'swatch'):
$_swatch_img = $_sister_prod_img['file'];
endif;
endforeach;
?>
<?php if($_swatch_img): ?>
<div class="sister-product-wrapper wash-wrapper">
<div class="sister-product-container wash-container">
<a href="<?php echo $base_url ?><?php echo $_sister_product->getUrlKey() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_sister_product, 'small_image', $_swatch_img)->resize(33,30); ?>" alt="" title="<?php echo $_sister_product->getAttributeText('color') ?>">
</a>
</div>
</div>
<?php endif; ?>
<?php endforeach ?>
<div class="clear"></div>
</div>
</div>
</div>
<?php endif ?>
Any help would be appreciated! :D
**Solution:**Added an if statement to check for stock availability using the isAvailable() function, shown in screenshot.
Link to screenshot: https://gyazo.com/abf07ba0373877836571858ee129cc22
I am creating a online webstore. But i am stuck in calling out the selected value to be used on the next page.
My Product summary page code,
<?php
include 'productData.php';
if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
$selected = $productArr[$_GET['cat']];
}
foreach ($selected as $productName => $productDescriptionArr):
?>
<div>
<div>
<a class="thumbnail" href="productDetailPage.php?cat=<?= $selected; ?>&code=<?= $productName; ?>">
<img src="<?php echo "img/" . $productDescriptionArr['image'] ?>" class="img-rounded" width="250" height="220">
<div><h3><?php echo $productName; ?></h3></div>
</a>
</div>
</div>
<?php endforeach; ?>
</body>
</html>
My Product Detail Page,
<html>
<body>
<?php
include 'productData.php';
if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
$selected = $productArr[$_GET['cat']];
if (isset($_GET['code']) && isset($productName[$_GET['code']])){
$name = $productName[$_GET['code']];
}
}
foreach ($selected as $name => $productDescriptionArr):
foreach ($productDescriptionArr as $key => $value) :
?>
<div>
<div>
<img src="<?php echo "img/" . $productDescriptionArr['image'] ?>" class="img-rounded" width="250" height="220">
<div class="title"><h3><?php echo $name; ?></h3></div>
</a>
</div>
</div>
<?php endforeach; ?>
<?php endforeach; ?>
</body>
</html>
in sort, select "PT", moves to page with all "PT" product, and end at the specific selected "PT" product in the product detail page.
I have edited your code for product detail page.. As you've not defined $productName variable anywhere in page. I've customized your code and hope it'll be good for you..
include 'productData.php';
<?php
if (isset($_GET['cat']) && isset($productArr[$_GET['cat']])) {
$productNames = $productArr[$_GET['cat']]; //Added $productNames here to asign your current product category
}
foreach($productNames as $name=>$details):
if (isset($_GET['code']) && $name==$_GET['code']){
//$name is product name.
//print_r($details);
// now you can use $details['images'], $details['dimension'], $details['price'] as you want...
?>
<div class="category">
<div class="col-md-3">
<a class="thumbnail">
<img src="<?php echo "img/" . $details['image'] ?>" class="img-rounded" width="250" height="220">
<div class="title"><h3><?php echo $name; ?></h3></div>
</a>
</div>
</div>
<?php } ?>
<?php endforeach; ?>
I am getting the error above in a new template I created in order to show the manufacturer's image. I am using this in the view.phtml file and it works just fine. But since this is custom, I figured there would be problems. Hopefully I can figure this one out.
Here is my current code:
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCategoryId = $category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
$product = $this->getProduct();
$brand = $product->getAttributeText('manufacturer');
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<?php echo '<img src="'.$this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '-', strtolower($brand)).'.jpg" alt="'.$brand.'"></a>' ?>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
You may only call getProduct() for a block which supports it. That generally means a descendant of Mage_Catalog_Block_Product_Abstract although there are others. When creating a block you can specify any type that meets your needs.
<block type="catalog/product_view" template="path/to/your/template.phtml" />
These blocks get their product object from the registry, which is only set for product pages by the catalog controller. If you have to use another block type which does not have a getProduct() function then you can access the registry directly.
$product = Mage::registry('current_product');
It seems to me that you need the manufacturer from products in a given category, and there are several categories to inspect. I assume any product from a category will do. This does not require the block to be a catalog/product_view type.
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$children = $category->getChildrenCategories();
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<!-- Load a product brand per category -->
<?php $product = $category->getProductCollection()->getFirstItem() ?>
<?php $brand = $product ? $product->getAttributeText('manufacturer') : 'default' ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".$category->getId()->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<?php echo '<img src="'.$this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '-', strtolower($brand)).'.jpg" alt="'.$brand.'"></a>' ?>
</div>
</a>
</div>
<?php endforeach; ?>
</div>
I figured out what I can do. I just used the product name as they should be matching anyway and used the same string I was trying to use for the Manufacturer. I am posting my answer for anyone who may want an answer down the road:
<?php
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$currentCategoryId = $category->getId();
$children = Mage::getModel('catalog/category')->getCategories($currentCategoryId);
?>
<div class="landing-page nested-container">
<?php foreach ($children as $category): ?>
<div class="vertical-section grid12-4 mobile-grid-half">
<a href="<?php echo $category->getRequestPath(); ?>">
<img class="center-block" alt="<?php echo $category->getName(); ?>" src="<?php echo $this->getBaseUrl()."media/catalog/category/".Mage::getModel('catalog/category')->load($category->getId())->getThumbnail(); ?>" />
<div class="caption category-boxes-logo full-width">
<img src="<?php echo $this->getBaseUrl().'media/wysiwyg/infortis/brands/'.str_replace(' ', '_', strtolower($category->getName())).'.png' ?>" alt="<?php echo $category->getName(); ?>">
</div>
</a>
</div>
<?php endforeach; ?>
</div>
I also just used a normal block type of core/template instead of what was suggested in a previous answer. This worked like a charm for me.
I have written following code to display subcategories
<ul class="">
<?php
$_categories=$this->getCurrentChildCategories();
if($_categories->count()):
$categorycount = 0;
foreach ($_categories as $_category):
if($_category->getIsActive()):
$cur_category=Mage::getModel('catalog/category')->load($_category->getId());
$layer = Mage::getSingleton('catalog/layer');
$layer->setCurrentCategory($cur_category);
$catName = $this->getCurrentCategory()->getName();
?>
<li class="">
<div class="">
<div class="">
<div class=""> <a href="<?php echo $_category->getURL() ?>" title="<?php echo $this->htmlEscape($_category->getName()) ?>" class="">
<?php
$imageUrl = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/".$this->getCurrentCategory()->getThumbnail();
$imageResized = Mage::getBaseDir('media')."/"."catalog"."/"."category"."/"."resize/".$this->getCurrentCategory()->getThumbnail();
if (!file_exists($imageResized) && file_exists($imageUrl))
{
$imageObj = new Varien_Image($imageUrl);
$imageObj->constrainOnly(TRUE);
$imageObj->keepAspectRatio(TRUE);
$imageObj->keepFrame(FALSE);
$imageObj->quality(100);
$imageObj->resize(270, 270);
$imageObj->save($imageResized);
}
?>
<span class=""><img src="<?php echo Mage::getBaseUrl('media').'catalog/category/resize/'.$this->getCurrentCategory()->getThumbnail(); ?>" alt="<?php echo $this->htmlEscape($_category->getName()) ?>"/></span> </a> </div>
</div>
<div class="">
<div class="">
<div class=""> <?php echo $this->htmlEscape($_category->getName()) ?> </div>
</div>
</div>
</div>
</li>
<?php
endif;
endforeach;
endif;
?>
</ul>
I want to add pagination in this page, i have tried solutions like following but it does not worked either
parent::_prepareLayout();
$pager = $this->getLayout()->createBlock('page/html_pager', 'custom.pager');
$pager->setAvailableLimit(array(15=>15));
$pager->setCollection($_categories);
$this->setChild('pager', $pager);
Can anyone have any idea on how to add pagination in subcategory listing page?
Please guide me as i am new to magento development.
Thanks in advance.
Inside Mage_Catalog_Block_Navigation I have custom method :
public function getCurrentChildCategories()
{
if (null === $this->_currentChildCategories) {
$layer = Mage::getSingleton('catalog/layer');
$category = $layer->getCurrentCategory();
$this->_currentChildCategories = Mage::getModel('catalog/category')->getCollection();
$pager = new Mage_Page_Block_Html_Pager();
$pager->setLimit(100)->setCollection($this->_currentChildCategories);
$this->setChild('pager', $pager);
/* #var $collection Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Collection */
$this->_currentChildCategories->addAttributeToSelect('url_key')
->addAttributeToSelect('name')
->addAttributeToSelect('is_anchor')
->addAttributeToSelect('image')
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
->setOrder('name', 'ASC')
->joinUrlRewrite()
->load();
$productCollection = Mage::getResourceModel('catalog/product_collection');
$layer->prepareProductCollection($productCollection);
$productCollection->addCountToCategories($this->_currentChildCategories);
}
return $this->_currentChildCategories;
}
I think it's not the best but it works.
How do I sort products on a category page by subcategory as well as limit the number of products from each subcategory:
For example if the category was Food I would want to display the following:
Drinks Coke 12oz, Orange Juice 8oz, Milk Gallon,
Pasta, Spaghetti 1lb, Pesto 12 pc, Tortellini 1 PC.
And so on, displaying each subcategory name followed 3 products (images etc.)
I currently have a custom template that displays the subcategories but can't figure out the products,
<?php
$_category = $this->getCurrentCategory();
$collection = $_category->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description','image')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($_category->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
$helper = Mage::helper('catalog/category');
?>
<ul>
<?php foreach ($collection as $cat): ?>
<li>
<div class="level1descript">
<a href="<?php echo $helper->getCategoryUrl($cat); ?>">
<img src="<?php echo $cat->getImageUrl(); ?>" class="catlevel1image" />
<h2><?php echo $cat->getName(); ?></h2>
</a>
<p class="level1descript">
<?php
$catdesc = '';
$catdesc = strip_tags($cat->getDescription());
if (strlen($catdesc) > 300) {
$catdesc = substr($catdesc, 0, 300) . ' ...';
}
echo $catdesc;
?>
</p>
</div>
<?php
$childLevel2Category = $cat->getCollection()
->addAttributeToSelect(
array('url_key','name','all_children','is_anchor','description','image')
)
->addAttributeToFilter('is_active', 1)
->addIdFilter($cat->getChildren())
->setOrder('position', 'ASC')
->joinUrlRewrite();
?>
<ul>
<?php foreach ($childLevel2Category as $catLevel2) { ?>
<li class="level2cats">
<a href="<?php echo $helper->getCategoryUrl($catLevel2); ?>">
<img src="<?php echo $catLevel2->getImageUrl(); ?>" class="catlevel2image" />
<h4><?php echo $catLevel2->getName(); ?></h4>
</a>
<p class="level2descript">
<?php
$catdesc = '';
$catdesc = strip_tags($catLevel2->getDescription());
if (strlen($catdesc) > 60) {
$catdesc = substr($catdesc, 0, 60) . ' ...';
}
echo $catdesc;
?>
</li>
<?php } ?>
</ul>
</li>
<?php endforeach;?>
</ul>
Below I documented an Idea, I think this is an idea.. Please excuse my rough throw together style wise. As I just used a project I was working on to throw this together. Any questions please ask. The setPageSize method will pull the first 3 products that display by default in the subcategories.
<!-- Finding Current Category and Finding it's children -->
<?php
$category = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$categories = $category->getCollection()
->addAttributeToSelect(array('name', 'thumbnail'))
->addAttributeToFilter('is_active', 1)
->addIdFilter($category->getChildren())
?>
<div class="subcategories">
<p>Select a category to view products:</p>
<ul class="clearfix">
<!-- Display Each Subcategory Image and Name -->
<?php foreach ($categories as $category): ?>
<li class="grid12-3">
<a href="<?php echo $category->getUrl() ?>" class="clearfix">
<?php if($thumbFile = $category->getThumbnail()): ?>
<img src="<?php echo Mage::getBaseUrl('media') . 'catalog' . DS . 'category' . DS . $thumbFile;?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
<?php endif;?>
<span><?php echo $category->getName() ?></span></a>
</li>
<!-- Load (3) Products from within each subcategory -->
<?php
$_helper = $this->helper('catalog/output');
$products = Mage::getModel('catalog/product')
->getCollection()
->addCategoryFilter($category)
->setPageSize(3)
->addAttributeToSelect(array('name', 'product_url', 'small_image'))
->load();
?>
<!-- Display Each product's detailed info -->
<?php foreach ($products as $product): ?>
<li>
<?php // Product Image ?>
<img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" />
<?php // Product description ?>
<?php $_productNameStripped = $this->stripTags($product->getName(), null, true); ?>
<h2 class="product-name"><?php echo $_helper->productAttribute($product, $product->getName() , 'name'); ?></h2>
</li>
<?php endforeach ; ?>
<?php endforeach; ?>
</ul>
</div>