Show Product on Search Page - Magento 1.7 - magento

Currently I have a snippet of code that will forward a user to the product page if they search for a term and only 1 product is associated with that keyword.
<?php if($this->getResultCount() == 1): ?>
<?php $prodId = $this->_productCollection->getAllIds() ?>
<?php $singleProduct = Mage::getModel('catalog/product')->load($prodId) ?>
<?php header('Location: ' . $singleProduct->getProductUrl()) ?>
<?php exit; ?>
<?php elseif($this->getResultCount()): ?>
However, what I want to do now is actually serve up the product and all its details on the results page itself if its the only one with that tag/search term INSTEAD of redirecting to the product page. Im pretty new to php so please bear with me.

Block template is bad place for this. Good place - controller.
Maybe you need rewrite controller for this functionality.
For example/app/code/core/Mage/CatalogSearch/controllers/ResultController.php
In controller your code looks like:
$this->getResponse()->setRedirect($_product->getProductUrl());

Related

How to compare current_url with site_url in codeigniter?

I have used something like that-
<?php if(current_url() == site_url()): ?>
...
...
<?php endif; ?>
But it's not working. I think there's something that I missed.
If you simply want to check if you're on the homepage of your site try this:
<?php if(strcmp(uri_string(),'')==0) : ?>
We are on the home page.
<?php endif; ?>
This solution will still work regardless of whether the base url is set correctly or not, unlike comparing base_url() and current_url().
Ensure that you have loaded the url helper either in your constructor or via autoloading.

Magento Active Filters on Search Page

I want to implement active filters on my magento ecommerce site.
I have been successful in implementing it, but the issue is, the code works on only category pages and not search page
Here's the code that I'm using
<?php /*Create filter dependencies*/
$_activeFilters[] = array();
$_filters = Mage::getSingleton(‘Mage_Catalog_Block_Layer_State’)->getActiveFilters();
foreach ($_filters as $_filter):?>
<?php echo $this->stripTags($_filter->getLabel()) ?><a href=”<?php echo $_filter- >getRemoveUrl() ?>” title=”<?php echo $this->__(‘Remove This Item’) ?>”><?php echo $this->__(‘Remove This Item’) ?></a>
<?php endforeach; ?>
I'm using this code in toolbar.phtml. Any clue as in why its not working on search page. Any Solutions would be of great help.
Thanks,
Sushil
You can use this code for fetching filters on either category list page or search results page
<?php
if(Mage::registry('current_category')) {
$_filters = Mage::getSingleton('catalog/layer')->getState()->getFilters();
} else {
$_filters = Mage::getSingleton('catalogsearch/layer')->getState()->getFilters();
}
?>
I have used this code in toolbar.phtml, to show removable filters below the toolbar, like flipkart does.
The problem is with this line:
$_filters = Mage::getSingleton(‘Mage_Catalog_Block_Layer_State’)->getActiveFilters();
This gets a singleton which only contains the necessary data when on a category page.
See this question for more details: Magento - How to add Layered Navigation to Advanced Search?

Magento - registry and current category

I have a question about Mage::registry and categories: I'm a on a category page, I retrieve current category by Mage::registry('current_category'). I've noticed that it works only for root categories, in fact if I visit a subcategory page I retrieve always the root category with Mage::registry('current_category'). So the question is: is something about backend configuration, cache or something else?
If you are in a template (e.g. catalog/category/view.phtml) you can get the current category with
$this->getCurrentCategory();
If you are in a model, controller or else, try this (found here):
Mage::getModel('catalog/layer')->getCurrentCategory();
However, Mage::registry('current_category') is the normal way to go.
OOB, current_category is set in Mage_Catalog CategoryController::_initCategory() (ref here) and will always be equal to the category currently being viewed.
If your data is different then your app has non-standard functionality or you are seeing cached results.
For all categories:-
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
For Current Category
<?php $currentCategory = Mage::registry('current_category') ?>
Works for me.

Magento Page Title - include SKU as well as product name

How do I include the SKU as well as the product name in the page title (when looking at the product details page)?
Cheers!
In app/design/frontend/default/{your theme Folder}/template/page/html/head.phtml you could try
<?php if ($_product = Mage::registry('current_product')) { ?>
<title><?php echo $_product->getName() . ' ' . $_product->getSku(); ?></title>
<?php }else{ ?>
<title><?php echo $this->getTitle() ?></title>
<?php } ?>
You could also do this using an observer for catalog_controller_product_view see Magento Change Product Page Titles to Include Attributes
If you have a collection already loaded (ie. on product view page)
$_product->getSku();
If for some reason you need to call this from a different template higher up in the chain that isn't already loading the product/collection, you can drop this one:
$_product = Mage::registry('current_product');
$_product->getSku();
look at your design template in catalog/product/view.phtml file you should be able to simply do $_product->getData('sku');

Magento - Unable to Refresh Product Stock Status on the Product Page

One of our Vendors has a real time inventory system and we would like to implement it into our site. When a person clicks on the product, it should check the inventory and update as necessary. This works ok at best. The problem is when the product switches to in/out of stock. It updates properly in the backend, but I am unable to get the addtocart button to be added/removed. This is my code for updating the stock:
//$_stockQTY is the realtime inventory result
$stockData = Mage::getModel('cataloginventory/stock_item');
$stockData->loadByProduct($_product->getId());
$stockData->setData('qty', $_stockQTY);
$stockData->setData('is_in_stock',($_stockQTY > 0) ? 1 : 0);
if ($stockData->dataHasChangedFor('qty')) {
$stockData->save();
$_product = Mage::getModel('catalog/product')->load($_product->getId());
}
As you can see, I am force reloading the product when qty is changed. This seems to work for everything but the addtocart button. It shows the previous result (In stock or out of stock before the reload.)
I have 2 questions:
Is there a better way to reload a product other than reassigning it as I am doing above:
$_product = Mage::getModel('catalog/product')->load($_product->getId());
And why is it that everything is updating properly, but the addtocart which uses the same
$_product->isSaleable()
call that our availability, etc uses.
Compare:
<?php if($_product->isSaleable()): ?>
<p class="availability in-stock"><img src="<?php echo $this->getSkinUrl('images/stock.png') ?>"> <span><?php echo $this->__('In stock') ?></span>
...
?>
To
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php endif; ?>
<?php echo $this->getChildHtml('alert_urls') ?> //Only Shows up if addtocart does not.
Refreshing the page will update the product properly, but doing a meta refresh or anything of the sorts is out of the question. I appreciate any advice that could be given as I would like to get this resolved and on to the next task.
Unless I'm misunderstanding your question, it appears the thorn in your side is the stock status index.
Try this:
Mage::getSingleton('cataloginventory/stock_status')->updateStatus($_product->getId());
(I haven't tested this, but it looks like it ought to work)

Resources