how to get product images url for storeid - image

I've created a magento product with no default (Admin) images, but
I've properly setted an image as thumbnail, small_image and imageset for the "italian" store view.
If I use the following code in a phtml files I'get the correct image url:
echo "url = " . Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(95,95)
I get :
media/catalog/product/cache/6/image/80x/9df78eab33525d08d6e5fb8d27136e95/1/9/1901186_10152274267007184_1589016979_n_7.jpg
Inside a controller for an ajax callback fucntion I use the following:
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
echo "url = " . Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(95,95);
but I alwais get the placeholder image
/media/catalog/product/cache/6/thumbnail/95x95/9df78eab33525d08d6e5fb8d27136e95/placeholder/websites/3/LOGO_1.png
I try to set the product storeId before loading the product data but no result:
$product = Mage::getModel('catalog/product');
$product = $product->setStoreId(6);
$product = $product->loadByAttribute('sku',$sku);
echo "vediamo " . Mage::helper('catalog/image')->init($product, 'thumbnail')->resize(95,95);;
Thank for any helps and sorry for my english.
Giuseppe

This is working for me
$cathlpimg = new Mage_Catalog_Helper_Image();
echo $cathlpimg->init($_item, 'image')->resize(200);

I'm surprised to see that didn't work - the key thing for me was to specify the store ID before calling load(), but I can see you already tried that.
Although, strictly speaking, you didn't use load(), you used loadByAttribute(). Maybe it doesn't work in quite the same way. Have you tried:
$product = Mage::getModel('catalog/product');
$product = $product->setStoreId(6);
$product = $product->load($id);
If you only know the sku and not the ID, just use this:
$id = Mage::getModel('catalog/product')->getIdBySku($sku);

Related

How to Create a Category with Custom Module Installation? [duplicate]

I actually can add a category via setup script, the thing is for some reason some of the fields doesn't get set properly. Here's is my code
$this->startSetup();
Mage::register('isSecureArea', 1);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setName('Category Name')
->setUrlKey('category-name')
->setIsActive(0)
->setIncludeInMenu(1)
->setInfinitescroll(1)
->setDisplayMode('PAGE')
->setLandingPage($idToCmsBlock)
->setPageLayout('anotherLayoutThanDefault')
->setCustomUseParentSettings(0)
->setCustomLayoutUpdate('<reference name="head"><action method="addCss"><stylesheet>css/somecss.css</stylesheet></action></reference>')
->save();
$this->endSetup();
After running this script, I have a category created with all my value set in the EAVs table.
However the Flat table will be missing displayMode, landingPage, pageLayout, customLayoutUpdate even if I re-index the flat table.
The weird thing is that if I go in the admin, I can see all those fields properly set but if I go in my frontend most of those fields are ignored. I will have to go to the admin, unset those value and reset them for each of them to work properly.
Also let say I use setEnabled(1), my category will be "enable" in the admin but not show up in the frontend.
PS: I have Flat Category activated, if I disable it seems to work fine but if I re-index it still not working.
I finally found it, I'm not sure why but those fields are not showing up properly because they were inserted for the default store (storeId=1) because my script is running in an update script. You need to use the storeId 0.
With this information you would think that the solution would be something like :
$this->startSetup();
Mage::register('isSecureArea', 1);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
But this code doesn't work either. Indeed after looking into Mage::app() (Mage_Core_Model_App Line 804) I noticed a IF condition that would always return the default store if you're in a setup script.
The trick is to fake that you're not in a setup script, my working solution is:
$this->startSetup();
Mage::register('isSecureArea', 1);
// Force the store to be admin
Mage::app()->setUpdateMode(false);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2') // set parent to be root category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('Category Name')
...
->save();
$this->endSetup();
I ran into the same issue when updating a category via a data install script. The solution provided in the accepted answer did work for updating the category, but can be improved upon as follows:
In the solution, the user that triggers the update script is forced to the admin environment. This can be remedied by saving the current store id and switching back at end of the script.
It doesn't seem that adding isSecureArea to the registry or disabling update mode had any use (at least for the use case of updating a category).
I ended up with the following data install script for updating a category (in this example, a category is loaded by name, after which the name is updated):
<?php
$this->startSetup();
//Switch to admin store (workaround to successfully save a category)
$originalStoreId = Mage::app()->getStore()->getId();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
//update category
$category = Mage::getModel('catalog/category')
->loadByAttribute('name', 'OLD_CATEGORY_NAME');
if ($category) {
$category
->setStoreId(Mage_Core_Model_App::ADMIN_STORE_ID)
->setName('NEW_CATEGORY_NAME')
->save();
}
//Set store to original value
Mage::app()->setCurrentStore($originalStoreId);
$this->endSetup();
?>
Try this
<?php
require_once "../app/Mage.php";
umask(0);
Mage::app('default');
$proxy = new SoapClient("http://127.0.0.1/magento/index.php/api/soap/?wsdl");
$sessionId = $proxy->login($magento_webservices_username, $magento_webservices_passwd);
$data = array('name'=>'Nokia',
'description'=>'',
'meta_description'=>'',
'meta_keywords'=>'',
'default_sort_by'=>'price',
'available_sort_by'=>'price',
'is_active'=>1
);
$newCategoryId = $proxy->call($sessionId, 'category.create', array(3, $data, 1));
echo "Category ID: ".$newCategoryId;
?>
And also have a look Magento create category
Take a look at this. Hope it will help you.
http://inchoo.net/ecommerce/magento/how-to-add-new-custom-category-attribute-in-magento/
I have created multiple categories via installer script.
<?php
$installer = $this;
$installer->startSetup();
Mage::register('isSecureArea', 1);
$category = Mage::getModel('catalog/category');
$category->setPath('1/2/4') // set parent to be root category
->setName('CAT NAME') //Category Name
->setIsActive(1) // Category Status
->setIncludeInMenu(1) // Show in Menu
->setIsAnchor(1) // used for Layered navigation
->setDisplayMode('PAGE') // Product Only
->setPageLayout('one_column') // Page layout
->save();
$installer->endSetup();

how to get default store view label on magento

I have this code:
$options = ($_Item->getProductOptions());
How I can have the default view label name of the option and not of the translated store view one?
Thanks.
this particular code depends on the class of the $item object.
Theoretically, you would get the default view label by loading the product in admin store:
Mage::getModel('catalog/product')->setStoreId(0)->load(PRODUCT_ID);
In your case, it might be something close to:
$productId = $item->getProductId();
$product = Mage::getModel('catalog/product')->setStoreId(0)->load($productId);
$label = $product->getName();
setStoreId(0) before loading the object is the key component.

Magento - Get product items attribute in view.phtml

I am trying to load a custom category in the catalog/category/view.phtml to archive this I use:
<?php
$_category = Mage::getModel('catalog/category')->load(47);
$_productCollection = $_category->getProductCollection();
if($_productCollection->count()) {
foreach( $_productCollection as $_product ):
echo $_product->getProductUrl();
echo $this->getPriceHtml($_product, true);
echo $this->htmlEscape($_product->getName());
endforeach;
}
?>
I can load the URL for example, now I want to load a custom attribute for example color:
$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)
This code does not work, I am 100% sure the color attribute is set to show in the category listing and also that the items in this category have this fields fill. I know this because this code works on list.html.
What I am doing wrong? I am working with 1.7.0.2.
The expected result is to show all COLOR attibutes from a custom cateogory in
catalog/category/view.phtml
I can't believe I just found the answer. Because we are not in a regular category listing we need to add the custom attributes to the collection.
Here is the code:
$_productCollection = $_category->getProductCollection()
->addAttributeToSelect('color');
If "color" is in the flat table you should be able to
$_product->getColor();
If this attribute is not in the collection, you can either add it to the flat table by making the attribute filterable, add it in the PHP collection call
$_productCollection = $_category->getProductCollection()
->addAttributeToSelect('color');
Or load the product model to get all of the attributes
$_product = Mage::getModel('catalog/product')->load($_product->getId());
echo $_product->getColor();
Make this attribute visible in listing/frontend
Run Reindexing
foreach( $_productCollection as $_product ):
echo $_product->getProductUrl();
in this code var_dump $_product->getData(); check if this var_dump results in that specific attribute value getting displayed.
Note:
$_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product)
is not a very efficient way of calling.

How to add Rating of a product in products listing?

I am displaying products of a particular category to the homepage content section.
I have made a separate .phtml file to display my homepage.
Now I want to show the ratings of a product (products are already rated). How do I show it?
If you look at the category listing template it's pretty easy to work out how category pages render out the review summary to show the rating block.
First load the product in question:
$product = Mage::getModel('catalog/product')->load($id);
Then create a product listing block to give access to the correct methods:
$block = Mage::app()->getLayout()->createBlock('catalog/product_list');
Finally run the getReviewsSummaryHtml() method and pass it the product to get the summary HTML.
$html = $block->getReviewsSummaryHtml($product, 'short');
You can do this.
$_product = Mage::getModel('catalog/product')->load($id);
if ($_product->getRatingSummary() && $rating = $this->getReviewsSummaryHtml($_product, 'short')) :
echo $rating;
else:
echo "<a href='$_product->getProductUrl()'>" . $this->__('Be the first to review this') . "</a>";
endif;

Getting image for bundle product child products, currently returning placeholder

My problem:
I need to retrieve the main product image from a cut down product object which is supplied by the class: Mage_Bundle_Model_Resource_Price_Index
My code demonstrating the issue:
$_product = $this->getProduct();
$_options = Mage::helper('core')->decorateArray($this->getOptions());
foreach($_options as $_option):
$_selections = $_option->getSelections();
foreach ($_option->getSelections() as $tmpsel) {
var_dump($tmpsel->getImageUrl());
}
Which returns my placeholder image:
http://dev.dev/media/catalog/product/cache/7/image/265x/0dc2d03fe217f8c83829496872af24a0/placeholder/default/logo_3.jpg
My horrible and hacky work around:
In order to get correct image Url, I have resorted to loading a completely new product object, which is terribly inefficient.
foreach ($_option->getSelections() as $tmpsel) {
$product = Mage::getModel("catalog/product")->load($tmpsel->getId());
$image = Mage::helper('catalog/image')->init($product, 'image');
var_dump($image."");
}
This returns correctly:
http://dev.dev/media/catalog/product/cache/7/image/0dc2d03fe217f8c83829496872af24a0/M/P/MP1428219-107-Main.jpg
What I want to do:
I want to be able to use the catalog/image helper with the selection ($tmpsel), but when I try I end up getting the placeholder image again.
foreach ($_option->getSelections() as $tmpsel) {
$image = Mage::helper('catalog/image')->init($tmpsel, 'image');
var_dump($image."");
}
Extra Info:
Anything I think of that could help I will add here
Cut down product object includes some reference to image
'small_image' => string '/M/P/MP1428219-107-Main.jpg'
'thumbnail' => string '/M/P/MP1428219-107-Main.jpg'
Description of getSelection()
In: ./app/code/core/Mage/Bundle/Model/Resource/Price/Index.php
* Retrieve bundle options with selections and prices by product
The function uses low level SQL to generate the collection, so I can always extend it to add options if needed, not sure which options though.
Thank you for reading, hope someone can give me a good way of doing this, will keep updated.
Init() method require a product object
init(Mage_Catalog_Model_Product $product, $attributeName, $imageFile=null)
Try
<?php echo $this->helper('catalog/image')->init($tmpsel, 'small_image')->resize(40,40); ?>
You do not have 'image' but you do have 'small_image' attribute
See Magento - how to retrieve bundled option images
To get the full web address you can use this code:
Mage::getSingleton('catalog/product_media_config')->getMediaUrl($product->getImage());
The limitation is that bring only the main image.
Oh wow, I guess I can thank you stack overflow for making me write my thoughts down.
After comparing the full product object and the semi product object I can see that I was calling the incorrect attribute name for catalog/image
$image = Mage::helper('catalog/image')->init($tmpsel, 'image');
Should have been
$image = Mage::helper('catalog/image')->init($tmpsel, 'small_image');

Resources