Show fatal error when click to product detail page from wishlist page - magento

I am trying to display category name in product detail page. For that I am using
$cat_name=Mage::registry('current_category')->getName();
It shows category name.
But when I went to wishlist page & click to product image then it give error:-
Fatal error: Call to a member function getName() on a non-object in /opt/lampp/htdocs/dominie/app/design/frontend/default/dominie/template/catalog/product/view.phtml.
Please help me how can I solve this issue.

Just tested the code below and it work in v1.7 when added to template/catalog/product/view.phtml.
However Mage::registry('current_category'); is only available if when coming to a product page from a category page (not tested, but may also dependent on if you have seo url that contain the category names within the url)
<?php
$_helper = $this->helper('catalog/output');
$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName(); //gives current category name
echo $_category_detail->getId(); //gives current category id
?>
See http://vinayp.com.np/how-to-get-category-name-and-id-on-product-page-on-magento/
To display all the categories that a product belong to do
<?php $categories = $_product->getCategoryIds(); ?>
<?php foreach($categories as $k => $_category_id): ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category_id) ?>
<?php echo $_category->getName() ?>
<?php endforeach; ?>
See http://www.magentocommerce.com/boards/viewthread/27720/

Try to define $cat_name in your wishlist module so that collection from wishlist module will also contain category name from which it was added to wishlist. This will surely reduce your overhead.

Related

Magento 2: Switch the price for 'Sold' label after product is out of stock

I'm trying to to switch the price for a 'Sold' label on the product page of products that are out of stock.
If a product is sold out, the price should be hidden and in its stead should be a 'Sold' label.
I figured out that the price is placed in catalog_product_view.xml and it calls upon the vendor/magento/module-catalog/view/base/templates/product/price/final_price.phtml file, but I could not figure out where and how to bring in a condition to check whether product is sold out or not.
Can someone help here?
Thanks in advance.
Yuan
As I understand you have two parts to this issue
1) Hide price on Product-detail page if product is Out-of-stock
Prices are defined in vendor/magento/module-catalog/view/frontend/layout/catalog_product_view.xml
called at class: Magento\Catalog\Pricing\Render and method: _toHtml()
You can override the method using DI to below
protected function _toHtml()
{
/** #var PricingRender $priceRender */
$priceRender = $this->getLayout()->getBlock($this->getPriceRender());
if ($priceRender instanceof PricingRender) {
$product = $this->getProduct();
if ($product instanceof SaleableInterface && $product->isAvailable()) {
$arguments = $this->getData();
$arguments['render_block'] = $this;
return $priceRender->render($this->getPriceTypeCode(), $product, $arguments);
}
}
return parent::_toHtml();
}
$product->isAvailable() is the new condition that we have added
2) Show Sold label
outofstock label is shown by default, but if you still want to show create your block & template in vendor/magento/modul-catalog/view/frontend/layout/catalog_product_view.xml
and $product->isAvailable() function to check product's availability
Hope this helps
Hiding price for out of stock products in Magento 1.
RWD Theme
app/design/frontend/rwd/template/catalog/product/view.phtml
Change
<div class="price-info">
<?php echo $this->getPriceHtml($_product); ?>
<?php echo $this->getChildHtml('bundle_prices') ?>
<?php echo $this->getTierPriceHtml() ?>
</div>
To:
<?php if($_product->isSaleable()): ?>
<div class="price-info">
<?php echo $this->getPriceHtml($_product); ?>
<?php echo $this->getChildHtml('bundle_prices') ?>
<?php echo $this->getTierPriceHtml() ?>
</div>
<?php endif; ?>
Default Theme:
\app\design\frontend\base\default\template\catalog\product\view\type\default.phtml
Change
<?php echo $this->getPriceHtml($_product) ?>
To:
<?php if($_product->isSaleable()): ?>
<?php echo $this->getPriceHtml($_product) ?>
<?php endif; ?>
OR
Hiding price for out of stock products in Magento 2.
On admin page, Click on Stores, then under the Setting section, choose Configuration.
In this page, you find the Inventory section under Catalog. Expand the Stock Options section and you can start to set the custom status of the product.
Before coming to the Out of stock product section, you need to enter this field.
Set Items’ Status to be in Stock When Order in Canceled: You choose YES when you want to return items to your stock if an order is canceled.
Decrease Stock When Order is Placed: if you want to adjust the quantity on hand when the order is placed, you choose Yes
Then we can come to the part that allows you to display or disable the product out of stock in Magento 2.
You want to display the product out of stock, you set the Display Out of Stock Products section is Yes. In contrast, set No if you want to disable it.

Get Meta Keyword By ID in Magento

I want to display Meta Keyword of a Product in view.phtml file.
What function do i need to call upon to display meta keyword of that particular product in Magento.
To return the meta keyword of the rendered product, use following within your view.phtml:
<?php echo $_product->getMetaKeyword(); ?>
You can get meta keyword by product id with:
<?php $_product = Mage::getModel('catalog/product')->load($productId); ?>
<?php echo $_product->getMetaKeyword(); ?>

How to show the value of an attribute in shopping cart?

I added an attribute 'items' having input type textbox in admin.I want to show the value of that attribute in shopping cart.For that i have added this code in template/checkout/cart/sidebar-top.phtml which is below:
<?php
$productId = $_item->getId();
$productInfo = Mage::getModel("catalog/product")->load($productId);
echo $productInfo->getAttributeText('product_type');
?>
but when i am adding this above code showing
Fatal error: Call to a member function getId() on a non-object
if anyone knows this,please help me out.thanks!
Use the below code
$productId = $_item->getProduct()->getId();
$productInfo = Mage::getModel("catalog/product")->load($productId);
echo $productInfo->getAttributeText('product_type');
Are you talking about custom option or simple attribute?
For Simple attribute (text) and if you are on product page try belo code:
<?php $_item = $this->getItem()?>
<?php $_product= Mage::getSingleton('catalog/product')->load($_item->getProductId()) ?>
<?php echo $_product->getResource()->getAttribute('attribute_code')->getFrontend()->getValue($_product); ?>
If you are not on the product page then you can use the below code, load the product by sku and get your attribute value
$product = Mage::getModel('catalog/product')->loadByAttribute('sku', $_item->getSku());
echo $product->getResource()->getAttribute('attribute_code')->getFrontEnd()->getValue($product);

Magento - Filter Category list by category attribute

I have a list of store categories, and everything is working well except for one thing. I would like the list of categories to omit any that are set as 'Include in Navivation Menu = No'.
I can tell at this point that this attribute is not being loaded, but I'm having a difficult time figuring out where to place the filter. Currently, I am getting my category list via:
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
Followed by:
<?php foreach ($_categories as $_category) : ?>
<?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
...
...
At this point, I have my category objects how I want them. But if I run a debug on these objects, the 'include_in_menu' attribute is not listed.
So for some reason:
<?php $_subCategory->getIncludeInMenu() ?>
Was not working for me after I ran:
<?php foreach($_parentCategory->getChildrenCategories() as $_subCategory) : ?>
The object was still the same model, but ['include_in_menu'] was no longer part of the object. I don't like this solution, but I just reconverted the object back:
<?php $_subCategory = Mage::getModel('catalog/category')->load($_subCategory->getId()) ?>
And then it worked fine. Not sure why getting the children would strip down the object, but it does.

Magento: Catalog Page 1 different from other pages

I want to setup the category catalog pages in Magento such that the first page contains the category image and the first three products in that category. Then the following pages contain six products per page without the category image.
I can't figure out how this can be done.
Unfortunately I don't think you can do this without a lot of work. You'll need to rewrite the logic of the pagination, change the page size depending on which page it is, & offset the returned collection.
However you can easily only have the category image displayed on the first page.
This line returns the current page number:
Mage::getBlockSingleton('page/html_pager')->getCurrentPage();
So in template/catalog/category/view.phtml you can just do a conditional around the category image display, find the section:
<?php if($_imgUrl): ?>
<?php echo $_imgHtml ?>
<?php endif; ?>
and replace it with:
<?php if($_imgUrl): ?>
<?php if(Mage::getBlockSingleton('page/html_pager')->getCurrentPage()==1):?>
<?php echo $_imgHtml ?>
<?php endif; ?>
<?php endif; ?>

Resources