Get Product Url return 404 in Magento - magento

I want to get product's url so i use getProductUrl() in result its return a url that seems be correct but its not ,When I want to open it ,the store return 404 not found error.
$product = Mage::getModel('catalog/product')->load($productId);
$url => $product->getProductUrl();
//It's return
// http://mg1.dev/index.php/catalog/product/view/id/905/s/plaid-cotton-shirt-royal-blue-l/

I got same error with you, here is my sollotion.
Add following line to your Query:
$collection->addFieldToFilter(array(array('attribute'=>'visibility', 'neq'=>"1" )));
IN my case that solved the 404/wrong url.

Check your catalog product model is really returning values or not...
If it is returning values, still you are not getting product url, then try with sku...
try this,
$sku = 'test'; // SKU you want to load.
$url = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku)->getProductUrl();
echo $url;

Related

Magento getAttribute not working with listing product

echo $_product->getResource()->getAttribute($attribute)->getFrontend()->getValue($_product);
This code is not working fine in view.phtml it is not return first attribute code value.
when i write this code on view page, it is not showing first product attribute and all after first in loop are showing fine.
This is my all code
<?php
$productAttributeTh = array('Color','Item','Size');
$configurableProduct = Mage::getModel('catalog/product')->load($_product->getId());
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$configurableProduct);
foreach($childProducts as $child) {
$product_id = $child->getId();
$obj = Mage::getModel('catalog/product');
$_childProduct = $obj->load($product_id); // Enter your Product Id in $product_id
foreach ($productAttributeTh as $key => $productAttributeValue){
$productAttribute = $_childProduct->getResource()->getAttribute($productAttributeValue)->getFrontend()->getValue($_childProduct);
echo $productAttribute;
}
} ?>
You need to make sure your attribute is set to be used in list. Go to;
Catalog > Attributes > Manage Attributes
Find your attribute, and open it. Scroll down to the option 'used in product listing' and set it 'yes'. Save and then reindex attributes.
Try this.
Mage::getResourceModel('catalog/product')->getAttributeRawValue($productId, 'attribute_code', $storeId);
Or
$attribute_value = $product->getResource()->getAttribute($attribute_code)->getFrontend()->getValue($product);
i hope this will help you.

Add SKU to end of Magento URL

I have searched for days trying to find a way of doing this but cannot find an answer, hopefully someone here can help me.
Essentially we want to insert the SKU into the product page url
Example
SKU = ST500DM002
So
domain.co.uk/seagate-barracuda-500gb-sata3.html
Would become
domain.co.uk/seagate-barracuda-500gb-sata3-ST500DM002.html
I had a look at this How to customize product URL?
It kind of works but it creates a path like this http://www.domain. co.uk/catalog/product/ST500DM002/seagate-barracuda-500gb-sata3.html rather than add it onto the end
Also we have a lot of SKUs with # in them which causes problems and throws 404
Is there a way to programmatically add the SKU and strip out #
Thanks
Copy the code and paste it to the file "script.php" and move it to magento root folder and try to run it in browser by using the following url,
http://localhost.com/script.php
<?php
require 'app/Mage.php';
Mage::app();
$amount = 0;
$model = Mage::getModel('catalog/product');
$products = $model->getCollection();
foreach ($products as $product) {
$model->load($product->getId());
$urlkey = $product->getUrlKey();
$sku = $product->getSku();
$product->setUrlKey($urlkey."/".$sku)->save();
set_time_limit();
$amount++;
}
When you run the file, All the urls will be rewritten with the new url keys.
You can find updated keys everywhere.
Create observer to catch event catalog_product_save_after_handler
$product = $observer->getProduct();
$urlkey = $product->getUrlKey();
$sku = $product->getSku();
if(substr($urlkey, strlen($sku) * (-1)) != $sku){
$product->setUrlKey($urlkey."/".$sku)->save();
}

Magento - getting category custom attribute value

How to get current custom category attribute value in product list view?
I'm trying like this
$attribute = Mage::getModel('catalog/category')->getAttributes();
And I see it's there but how to get it?
My custom attribue name is catalog_pdf
Also tryed in this way, but get nothing:
$attribute = Mage::getModel('catalog/category')->getAttribute('catalog_category','catalog_pdf');
This should work.
If you are int the product list then you should have the current category in
Mage::registry('current_category');
So do like this:
$category = Mage::registry('current_category');
if ($category){ //this is necessary in case you are in a product listing that is's not a category
$value = $category->getData('catalog_pdf');//catalog_pdf is the attribute code
//or
//$value = $category->getCatalogPdf();
}
This should work:
$id = $this->getCurrentCategory()->getId();
$category = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getCode()->getId())->load($id);
echo $category->getData('catalog_pdf');
//or
echo $category->getCatalogPdf();
Edited to include missing get

how to get sku and product name on the head.php page?

how to get sku and product name on the head.php (\app\code\core\Mage\Page\Block\Html\head.php) page? thank you.
when i used $this->_data['sku']; or $this->getSku(); are all not work.
The previous answer is fine, except it doesn't check if product exists in registry. So you will get fatal error on non-product pages. Always make a check if variable exists.
$product = Mage::registry('current_product');
if ($product) //sometimes need check for instanse, use instanseof
{
$product->getSku();
}
You should be able to pull the current product back from the registry and access the values from that.
$product = Mage::registry('current_product');
$product->getSku();
Working Code:
if($_product = Mage::registry('current_product')){
$id = $_product->getId();
$sku = $_product->getSku();
}

Magento external Script and Session

I want to add an external script which gets a sku via GET check the ID and then redirect to the cart if available otherwise it sets en error and also redirecting to the cart.
The script is called from a product page:
http://myhost/scripts/addto.php?sku=12345
Here is the colmplete code
<?php
include_once '../../../../../app/Mage.php';
Mage::app();
$session = Mage::getSingleton('core/session', array('name' => 'frontend'));
$sku = $_GET['sku'];
if (!isset($_GET['qty'])) { $qty = '1'; } else { $qty = $_GET['qty']; }
$id = Mage::getModel('catalog/product')->getIdBySku($sku);
if ($id == '') {
$id = $sku;
Mage::getSingleton('checkout/session')->addError("Product not found!");
}
Works fine, but after logout and relogin the error message is missing. I found out that's because of a cookie which is set. After deleting that cookie the error message is working again after relogin.
What does not work?
You logout and login and then the script stop working? So the session is not found? The product ist not found? The product is not loaded? The user is not forwarded? :-)
Sure it does not. The Message is a Notice. If it is shown once, it is deleted.
What behaviour do you want?
use Mage::getSingleton('core/session')->addError("Product not found!");, maybe checkout/session is user specific...
cheers

Resources