How to get correct product url for multistore magento? - magento

I am running following code in Magento root to get the product urls
<?php
require_once('app/Mage.php');
umask(0);
Mage::app(1);
$collection = Mage::getModel('catalog/product')
->setStoreId(1)
->getCollection();
foreach( $collection as $product )
{
echo $product->getProductUrl();
echo "<br>";
}
?>
I am getting product urls like http://example.com/catalog/product/view/id/5/ , But these urls are invalid.
The product urls are as following in front end http://example.com/product.html
How do I get the correct product urls? I have multi store Magento set-up.

You need to get store url for each product separetly. In other words, you need to use something like this:
$collection = Mage::getModel('catalog/product')
->getCollection();
foreach( $collection as $product )
{
echo $product->setStoreId(5)->getProductUrl();
echo "<br>";
}

Related

For loop is working only once

For the below code, where I am trying to update a custom attribute called branding. The for loop is running only for one iteration and stopping. It is updating only the first product in the list and not looping any further.
Can anyone kindly let me know why this is happening?
<?php
set_time_limit(0);
// require magento core
require_once 'app/Mage.php';
// execute on admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $product){
echo $product->getSku();
$product->setData('branding', 'kib');
$product->save();
}
Instead of saving total product just save attribute for product by using saveAttribute
// require magento core
require_once 'app/Mage.php';
// execute on admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$products = Mage::getModel('catalog/product')->getCollection();
echo $products->getSelect();//this will give sql query just run your db.
foreach($products as $product)
{
$product->setBranding('kib');
$product->getResource()->saveAttribute($product, 'branding');
}
One more solution without loops
$store_id = 0;
$product_ids = Mage::getModel('catalog/product')
->getCollection()
->getAllIds();//you can apply some filters also
Mage::getSingleton('catalog/product_action')->updateAttributes(
$product_ids,//array of ids
array('branding' => 'kib'),//you can add some other attributes also as key value pair
$store_id
);

Magento - Display product count views?

I am using magento 1.9 and i am trying to display the views of the product in the product view page.
Here is what code i am using right now but it's not displaying anything:
<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<?PHP echo $_product->getViews();?>
Is it possible to display the number of the views of the displayed product or where i am doing wrong.
Thanks in advance!
You could create an observer for catalog_product_load_after and add views to the $_data array with
$product = $observer->getEvent()->getProduct();
$resource = Mage::getSingleton('core/resource');
$read = $resource->getConnection('core_read');
$select = $read->select()
->from(
$resource->getTableName('log/url_info_table'),
'COUNT(*) views'
)
->where('url LIKE "%catalog/product/view/id/' . $product->getId() . '"');
$result = $read->query($select)->fetch();
$product->setData('views', $result['views']);

Magento 1.9 - resave all products

I need to re-save all products in magento. I found solution (for 1.7 version):
<?php
set_time_limit(0);
// require magento core
require_once 'app/Mage.php';
// execute on admin store
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$products = Mage::getModel('catalog/product')->getCollection();
foreach($products as $product) {
echo $product->getName() . '<br/>';
// save the product
$product->save();
}
echo 'DONE';
?>
But this not working for magento 1.9.
I need to re-save all configurable products, if it possible.
It works, but you cannot see product name, try use sku field

Product collection by attriubte base in magento

I want to get the product collection in magento.For that i use some code but i think this code is not what i need.I want to get collection on attribute base.I got some products but it didn't match to those products that are filters from advance results for that attribute.Means different results from my collection and advance search results.Also the product url is not valid one.May be someone know where is the problem ? thanks in advance.My code is :
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter(array(
array('attribute'=>'manufacturer','eq'=>'23'),
));
foreach ($collection as $product) {
?>
<div class="brand_name">
<p>Audi</p>
<?php echo substr($product->getName(),0,10);?>
</div>
<?php } ?>
You have used 2 array in the field to filter. Try with one.
<?php $collection = Mage::getModel('catalog/product')
->getCollection()->addAttributeToSelect('*')
->addFieldToFilter('attribute'=>'manufacturer','eq'=>'23');
It should work correctly:
$attrToSelect = '*'; // or Mage::getSingleton('catalog/config')->getProductAttributes();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect($attrToSelect)
->addAttributeToFilter('manufacturer', 23)
;
foreach ($collection as $product) {
echo $product->getProductUrl();
}
Also check in admin - Catalog->Attributes->Manage Attributes - Used in Product Listing set Yes.
please use addAttributeToFilter('manufucture',23)
instead of
adfieldtofilter

Magento 1.5.1 getting product images using php

This code gets contents(images) in a magento store. It is able to fetch images for magento 1.4x - 1.5
I tried it in 1.5.1 and it seems it cannot fetch the images. Is it located in "media/catalog/product" ?
Any help in getting the location of magento 1.5.1 images? Thanks.
<?php
include_once 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->load();
$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$myImage = $baseUrl ."media/catalog/product". $product['image'];
?>
<?php
include_once 'app/Mage.php';
umask(0);
Mage::app();
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('thumbnail');
$products->load();
$product = $products->getData('thumbnail');
$baseUrl = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$myImage = $baseUrl ."media/catalog/product". $product[2]['thumbnail'];
print $myImage;
?>
Obviously you'll want to loop over the collection or specify a product ID in your collection for a specific product, I just used an index of [2] for show.
Also no need to load the entire product attributes collection with * but rather the specific attribute your needing to conserve memory space.

Resources