Magento get product special price not working on homepage - php

I'm editing a premium magento template right now (Hellodisplay) which has a featured product section on it's homepage. That section works by calling a specific category defined in a static block. My problem with this section is I couldn't show the special price of the products in any way. Althought the special product showed up on the product detail page. It also runs normal on the default template.
I've tried both function getSpecialProduct and getFinalProduct. GetSpecialProduct return nothing and GetFinalProduct return the normal price. I've also tried to use default theme price child html (price.phtml). It also doesn't works.
Then I check the print_r() output of $_product variable both on the homepage and also on the product page.I noticed the differences. Special array value exist only on the product pages's $_product variable. So how can I make this special price value appear on the homepage too?
This is my featured.phtml code
<?php
/**
* Product list template
*
* #see Mage_Catalog_Block_Product_List
*/
?>
<?php
$product_limit = 3;
$i = 1;
$_productCollection=$this->getLoadedProductCollection();
$cat_id = $this->category_id;
$_productCollection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToSelect(array('name', 'price', 'small_image', 'status'), 'inner')
->addCategoryFilter(Mage::getModel('catalog/category')->load($cat_id));
?>
<?php if(!$_productCollection->count()): ?>
<div class="note-msg">
<?php echo $this->__('There are no products matching the selection. Please provide a category ID.') ?>
</div>
<?php else: ?>
<ul class="frontgrid">
<?php $_collectionSize = $_productCollection->count() ?>
<?php foreach ($_productCollection as $_product): ?>
<?php if($i >= $product_limit+1){
break;
} else{
$a = $i % 3;
$i++;
} ?>
<li class="<?php echo "col".$a; ?>">
<a class="imglink" href="<?php echo $_product->getProductUrl() ?>">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(200, 200); ?>" width="200" height="200" alt="<?php echo $this->htmlEscape($_product->getName()); ?>" />
</a>
<h4><?php echo $this->htmlEscape($_product->getName()); ?></h4>
<div class="boxbar">
<span class="oldprice">
<?php if($this->htmlEscape($_product->getSpecialPrice())){ ?>
€ <?php echo number_format($this->htmlEscape($_product->getPrice()), 2) ?>
<? } ?>
</span>
<span class="price">
<?php if($this->htmlEscape($_product->getSpecialPrice())){ ?>
€ <?php echo number_format($this->htmlEscape($_product->getSpecialPrice()), 2) ?><br/>
<? } else { ?>
€ <?php echo number_format($this->htmlEscape($_product->getPrice()), 2) ?>
<? } ?>
</span>
<a class="moreinfo" href="<?php echo $_product->getProductUrl() ?>">Meer Info »</a>
</div>
</li>
<?php endforeach ?>
</ul>
<?php endif; ?>
Thanks before :)

You have to add "special_price", "special_from_date" and "special_to_date" on addAttributeToSelect array.

Related

Variable not being formatted with CSS - Magento

When I echo my own variable to a div container it comes out as plaint text (unformatted). I don't understand why echoing a Magento variable comes out formatted but mine doesn't? Here's my code, in particular the <?php if(!isset($specialPrice)): { ?> section which I created. Here is the code:
<div class="product">
<a href="<?php echo $_item->getProductUrl() ?>"
title="<?php echo $this->escapeHtml($_item->getName()) ?>" class="product-image"><img
src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail') ?>"
alt="<?php echo $this->escapeHtml($_item->getName()) ?>"/></a>
<div class="product-details">
<p class="product-name">
<?php echo $this->escapeHtml($_item->getName()) ?>
</p>
<?php $specialPrice = $productToCheck->getData('special_price');
$orignalPrice = $productToCheck->getData('price');
?>
<?php if(!isset($specialPrice)): { ?>
<?php echo $product['price'] ?>
<?php } else: { ?>
<?php echo $specialPrice ?>
<?php } endif ?>
</div>
</div>
Echoing $product['price'] shows up with its CSS like this:
but if it enters the ELSE statement to display my variable it shows like this:
Does anyone know what could be going wrong?
$product['price'] returns you a unformatting price value.
Maybe you can call a block function, for example $block->getPrice() and in getPrice() you need to format the price by your custom preferences.
If you want to add styles to your price value then you need to use a magento tag class

Get category name from Magento

I'm using Luxury theme in Magento. I'm trying to display current category name in the catalog/category/view.phtml file.
What I have done so far:
<div class="custom">
<?php if($crumbs && is_array($crumbs)): ?>
<div class="container">
<div class="col-md-12">
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class="<?php echo $_crumbName ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemscope itemtype="http://data-vocabulary.org/Breadcrumb" <?php endif ?>>
<?php if($_crumbInfo['link']): ?>
<a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>" <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="url" <?php endif ?>><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></a>
<?php else: ?>
<strong><span <?php if(Mage::getStoreConfig('mgs_theme/general/snippets') == 1): ?> itemprop="title" <?php endif ?>><?php echo $this->escapeHtml($_crumbInfo['label']) ?></span></strong>
<?php endif; ?>
<?php if(!$_crumbInfo['last']): ?>
<span>| </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
</div>
<?php endif ?>
</div>
I have taken this code from page/html/breadcrumbs.phtml.
I am totally new to Magento/PHP. It doesn't show any error but it's not displaying the name of the category, while it's visible in breadcrumbs header. What I am doing wrong here?
If you want to show category name on category page. You can achieve this by 2 ways.
<?php echo $this->getCurrentCategory()->getName(); ?>
Or
<?php echo Mage::registry('current_category')->getName() ?>
On our site (magento 1.9) we wanted to show the first parent category of the current product on our product pages and provide a link to it. I achieved this as follows - you should be able to reverse engineer my code to your own ends.
At first I did it by adding the following code directly to the catalog/product/view.phtml but have since migrated it into a custom helper in my own module.
Here's the code, see if it works for you.
//get an array of the IDs of every category to which the product belongs.
$categoryIds = $_product->getCategoryIds();
//set CatID to the second element of the array since the first element
//is the base category of which all others are children.
$_catID = $categoryIds[1];
//load the correct model
$_category = Mage::getModel('catalog/category')->load($_catID);
//get the level of the current category
$level = $_category->getLevel();
//This if statement prevents the function from trying to run on products
//which are not assigned to any category.
if($_category->getName()){
// we want the second level category, since the first is the base category.
// ie if we call the default category 'base' and the product is in category
//base->foo->bar->baz we want to return the link to foo.
// while current category is deeper than 2 we ask it for it's parent
//category until we reach the one we want
while ($level > 2){
$parent = $_category->getParentId();
$_category =Mage::getModel('catalog/category')->load($parent);
$level = $_category->getLevel();
}
//Now we can build the string and echo it out to the page
$caturl = $_category->getUrl_key();
$_linkstring = 'http://www.yourwebsite.com/' . $caturl . '.html';
echo 'in category:';
echo '<a href="' . $_linkstring . '" title="'. $_category->getName() .'">';
echo ' '. $_category->getName();
echo '</a>';
}
Get the category name, image, description from category id in magento
$category = Mage::getModel('catalog/category')->load(category_id);
echo $category->getName();
echo $category->getImageUrl();
echo $category->getDescription();
Please put the category id in the load function

Change Magento's Upsell Slider to use Related Products

I have a client who has accidentally added the products in the Related Products section rather than the Upsell Section. I have a file named upsell_slider.phtml that spits out the following:
<?php if(count($this->getItemCollection()->getItems())): ?>
<div class="box-collateral box-up-sell upsell-slider">
<h2><?php echo $this->__('Fashion Statement') ?></h2>
<?php $this->setColumnCount(4); // uncomment this line if you want to have another number of columns. also can be changed in layout ?>
<?php $this->resetItemsIterator(); ?>
<?php
$products_count = 0;
while($this->getIterableItem()){
$products_count++;
}
?>
<div <?php if ($products_count > 1): ?>id="block-upsell-slider"<?php else:?> class="no-slider" <?php endif; ?>>
<ul class="products-grid carousel-ul" id="upsell-product-table">
<?php $this->resetItemsIterator(); while ($_item=$this->getIterableItem()) : ?>
<li class="item grid_3 alpha">
<a class="product-image" href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_item->getName()) ?>"><img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->constrainOnly(TRUE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(420, null); ?>" <?php echo MAGE::helper('ThemeOptions/Retina')->getRetinaData('upsell', $_item); ?> alt="<?php echo $this->htmlEscape($_item->getName()) ?>" /></a>
<div class="product-details">
<h3 class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></h3>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php if ($products_count > 1): ?>
<div class = 'next'></div>
<div class = 'prev unselectable'></div>
<?php endif; ?>
</div>
<script type="text/javascript">
/* Upsell Block Slider */
if(jQuery('#block-upsell-slider').length) {
jQuery('#block-upsell-slider').iosSlider({
responsiveSlideWidth: true,
snapToChildren: true,
desktopClickDrag: true,
infiniteSlider: true,
/* navSlideSelector: '.sliderNavi .naviItem', */
navNextSelector: '.box-up-sell .next',
navPrevSelector: '.box-up-sell .prev'
});
}
function upsell_set_height(){
var upsell_height = 0;
jQuery('#block-upsell-slider li.item').each(function(){
if(jQuery(this).height() > upsell_height){
upsell_height = jQuery(this).height();
}
})
jQuery('#block-upsell-slider').css('min-height', upsell_height+2);
}
setTimeout(function(){
upsell_set_height();
}, 1000);
jQuery(window).resize(function(){upsell_set_height();});
/* Upsell Block Slider */
</script>
<?php endif ?>
And I can't for the life of me get it to look at the related products, rather than the upsell products.
I have a better solution. You can transform the related products in upsells with one query.
UPDATE `catalog_product_link` SET `link_type_id` = 4 WHERE `link_type_id` = 1;
Note. This will make move all the related products to upsells.
...and back up the table before trying it.

Wrong product URLs magento

I've been working on custom up sells related products script, which is displaying 4 random products.
Problem is:
normal product url is:
/shop/$productname
This sometimes is generating url like: /$productname/
or url like: /catalog/product/view/id/$productID/4/s/$productname/category/$categoryid/
I want to have all my URLs the same so: /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()); ?>
<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) ?>" />
<h3 class="product-name"><?php echo $_helper->productAttribute($product, $product->getName(), 'name') ?></h3>
<?php echo $this->getPriceHtml($product, true) ?>
</td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
</div>
Try
$this->getUrl('shop').$product->getUrlPath()
instead of
$product->getProductUrl()
for both image and product title.
If your indexes are up to date then
$product->getProductUrl()
Should give you the correct product url.
Do not try to build the url yourself from pieces like the url_key, or url_path and the base url. If you move your website to an other serve you're going to have some issues. Also if you change the config settings for categories in product url you can have problems.

Unable to retrieve price information from a Magento product collection

I am trying to output products from some category on an arbitrary page in a fashion similar to that of list.phtml's grid format.
I have the following snippet:
$category = Mage::getModel('catalog/category');
$category->load(17);
$_productCollection = $category->getProductCollection()
->addAttributeToSelect('name');
$_helper = Mage::helper('catalog/output');
That gives me a product collection which I then iterate over with:
foreach ($_productCollection as $_product):
<!-- This works -->
<h2 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>
</h2>
<!-- This does not -->
<?php echo $this->getPriceHtml($_product, true) ?>
<!-- This just returns out of stock -->
<div class="actions">
<?php if($_product->isSaleable()): ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')">
<span><span><?php echo $this->__('Add to Cart') ?></span></span>
</button>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
</div>
endforeach;
The above code except for the call to get the product collection at the top is just borrowed from list.phtml.
Can anyone tell me why the price and is saleable information is not available, hence why the item appears out of stock? Previously when the product name was unavailable, I had to add ->addAttributeToSelect('name'), would I need to add something along those lines?
Please try the code below in your phtml file.
$category = Mage::getModel('catalog/category')->load(3);
$_productCollection = $category->getProductCollection()->addAttributeToSelect('*');
$productBlock=$this->getLayout()->createBlock("catalog/product");
foreach($_productCollection as $_product)
{
//for get the price of product
if($_product->isSaleable()) //this will check if product is in stock
echo $productBlock->getPriceHtml($_product,true);
}
So you're on the right path to look into core, if you want to copy some functional that is similar to the one from the basic Magento - like product listing.
the price function getPriceHtml is a method defined in the abstract class Mage_Catalog_Block_Product_Abstract. So to use it, you need to extend your block from the Mage_Catalog_Block_Product_Abstract one.
isSaleable returned false because you didn't have some of the attributes joined to your collection.
Here's how you should accomplish your goal, if you want to follow Magento's logic.
Create your own module, or just block in local/Mage/Catalog/Block/YourBlock.php. This block should extend Mage_Catalog_Block_Product_Abstract. After that create a method in this block getCustomProductCollection():
pubcli funciton getCustomProductCollection()
{
if (is_null($this->_productCollection)) {
$category = Mage::getModel('catalog/category')->load(17);
$layer = $this->getLayer();
$layer->setCurrentCategory($category);
$this->_productCollection = $layer->getProductCollection();
}
return $this->_productCollection;
}
Now in your phtml file you'll just call for this method:
$productCollection = $this->getCustomProductCollection();
And the rest of the code will work.
$productBlock=$this->getLayout()->createBlock("catalog/product");
echo $productBlock->getPriceHtml($_product,true);
Try this

Categories