Magento 1.5.1 getting product images using php - magento

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.

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

How to get correct product url for multistore 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>";
}

Why are products in the category with Mage::app('default') but not with Mage::app('admin')?

I wrote an extension and in this extension I need the count of active products of a category.
I used this snipped to determine active products by category:
$return = Mage::getModel('catalog/category')
->load($category->getId())
->getProductCollection()
->addAttributeToSelect('entity_id')
->addAttributeToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
->addAttributeToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
->count();
If I call the function to get the count of active products in my test.php
header('Content-Type: text/html; charset=utf-8');
require_once "app/Mage.php";
Mage::reset();
Mage::app('default');
var_dump(myDummyFunction($categoryObject));
I get 3 as the result. If I use the same function within the same magento in the extension (which is used in the admin area) the result is 0.
Has somebody an idea why this happens and how to fix this?
Btw: If I change the Mage::app('default'); in the test.php to Mage::app('admin'); it also displays 0...
Use this
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Just Before this
Mage::app('default');

Resources