Magento: How to get thumbnail? - php

I downloaded a script to create a CSV datafeed of my products and I would also like to include a url to the thumbnail. The code already has the following for the product image url:
$product_data['ImageURL']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getImage();
So I tried to adjust this to:
$product_data['ThumbnailURL']=Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA).'catalog/product'.$product->getThumbnail();
Which displays exactly the same image url (not the thumb). How would I fix this?
I did a var_dump($product); and the result was:
["image"]=> string(18) "/f/i/file_2_18.png" ["small_image"]=> string(18) "/f/i/file_2_18.png" ["thumbnail"]=> string(18) "/f/i/file_2_18.png"
I also need to get the subcategory for the product but I don't know how to call this. How can I see which variables are possible? E.g. $product->getPrice or $product->getName?

I recently needed to do this as well... here's how I got to it:
$_product->getMediaGalleryImages()->getItemByColumnValue('label', 'LABEL_NAME')->getUrl();
Or another example
You should use the catalog product media config model for this purpose.
<?php
//your code ...
echo Mage::getModel('catalog/product_media_config')
->getMediaUrl( $product->getImage() ); //getSmallImage(), getThumbnail()
Edit After your comment.
Update Answer
See below URL
Make all store images the base, small and thumbnail images in Magento?
Try it
$products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
foreach ($products as $product) {
if (!$product->hasImage()) continue;
if (!$product->hasSmallImage()) $product->setSmallImage($product->getImage());
if (!$product->hasThumbnail()) $product->setThumbnail($product->getImage());
$product->save();
}
Hope that helps you!

If you want to see which data you can access you could try this:
<?php var_dump(array_keys($product->getData())); ?>
You can then use two methods to get this data:
<?php $product->getAttributeName() // Use CamcelCase ?>
or
<?php $product->getData('attribute_name') // underscores ?>

Related

Magento return wrong media url on frontend

Im stuck on wired situation. The thing i do is getting magento blog data and show on specific page. The problem is magento return the wrong media URL on front end like this <img alt="" src="{{media url=" wysiwyg="" blogimg1.jpg"}}"="">. All i want it will return image src like this "mydomain.com/images/blogimg1.jpg". I try to find solution but couldnot succeed.
My code is below
<?php
$collections = Mage::getModel('blog/blog')->getCollection()
->addPresentFilter()
->addEnableFilter(AW_Blog_Model_Status::STATUS_ENABLED)
->addStoreFilter()
->joinComments()
->setOrder('created_time', 'desc');
foreach ($collections as $collection) {
echo $collection->title."<br>";
echo $collection->post_content."<br>";
echo $collection->short_content."<br>";
}
?>
Problem solve we have to use the filter , and everything gonna fine
Mage::helper('cms')->getPageTemplateProcessor()->filter($collection->short_content)

how to show “Minimum Qty Allowed in Shopping Cart" in product listing page

I am using Magento 1.9. I want to show "Minimum Qty Allowed in Shopping Cart" in list.phtml page. For example I have set minimum quantity "6" in Product attribute then it should show "6" in front end.
I am trying to write this code but it is throwing an error. Maybe I am writing a wrong code.
<?php
$productQuantity = Mage::getModel("cataloginvetory/stock_item")->loadByProduct($_product->getId());
echo $productQuantity->getMinSaleQty(); ?>
I am getting following error
Fatal error: Call to a member function loadByProduct() on a non-object in //list.phtml
Here some logic
<?php
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
echo $stockItem->getMinSaleQty() && $stockItem->getMinSaleQty() > 0 ? $stockItem->getMinSaleQty() * 1 : null;
?>
Mage::getModel("cataloginvetory/stock_item") is not returning an object. Use var_dump() to find out what Mage's static function is returning.
echo var_dump(Mage::getModel("cataloginvetory/stock_item"));
I presume it will return with NULL or false. If that is the case something is going wrong in getModel() (probably incorrect parameters)
There are two mistakes that I see right off the bat.
First, "cataloginvetory/stock_item" is a typo. It should be "cataloginventory/stock_item."
Second, loadByProduct takes a product - not a productId.
It should look like:
<?php
$productQuantity = Mage::getModel("cataloginventory/stock_item")->loadByProduct($_product);
echo $productQuantity->getMinSaleQty(); ?>
I wasn't able to test the getMinSaleQty() function because I don't have that enabled on my site.

Set Base Image Programmatically

I've got this problem that I can't solve. Partly because I can't explain it with the right terms. I'm new to this so sorry for this clumsy question.
Below you can see an overview of my goal.
I am trying to sell Similar product in my magento for this i wrote some code that every thing is working fine..
But i have only one problem with images..
that is I am getting all images of current product but the base image is not selected, How can i set the very first image as base image Programmatically.
Any ideas ?
Hello You can do as follows :
$image =$imagePath."image.png";
$product->setMediaGallery(array('images'=>array (), 'values'=>array ()));
if(is_file($image))
{
$product->addImageToMediaGallery($image, array ('image', 'small_image', 'thumbnail'), false, false);
}
i.e you need to first set the media gallery, P.S This is a necessary step.
then add all images to gallery using addImageToMediaGallery where 'image' ref to 'base_image'
i.e in above example we are setting image.png to base_image, small_image and thumbnail image in a single call.
hope this helps you.
I've achieved the same result by using:
$product->setSmallImage($path)
->setThumbnail($path)
->setImage($path)
->save();
Works better for the case where your media gallery has one or more pictures in it.
I'm doing
$product->load();
$gallery = $product->getMediaGalleryImages();
$paths = array();
foreach ($gallery as $image) {
$paths[] = $image->getFile();
}
sort($paths);
$path = array_shift($paths);
try {
$product->setSmallImage($path)
->setThumbnail($path)
->setImage($path)
->save();
} catch (Exception $e) {
echo $e->getMessage();
}
Which gets all the product images, sort's them by file name and sets the first to the product primary image. As I ran a import and added all my pictures, but didn't set the primary image.
To grab a set or "broken" products I used:
$collection = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToFilter('small_image', array('eq' => ''));

Get base product image in Magento

I want to get base product image in Magento to resize it and display in cart sidebar.
Unfortunatelly this:
echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38);
prints Magento placeholder image.
Base image is set for this product properly. Small image and thumbnail works great.
No idea what's going on.
EDIT:
Solution:
Get full product data this way:
$_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
and then use it as you wish:
echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38);
I think you are looking for this:
echo Mage::getModel('catalog/product_media_config')
->getMediaUrl( $product->getImage() ); //getSmallImage(), getThumbnail()
Credit should be given to BenMarks who gave this answer.
Try:
$this->helper('catalog/image')->init($_product, 'image')->keepFrame(false)
->constrainOnly(true)->resize(38,38);
BE AWARE!
$this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38);
is object, not url string it self. Yes, you can use it directly with echo, but shouldn't assign it to var. For example this wont work:
$images = array();
foreach($products as $_product){
$images[]=$this->helper('catalog/image')->init($_product, 'small_image')
->resize(38, 38);
}
After foreach, you will have only one last image url saved. Simple way is to get truly string url is:
$images = array();
foreach($products as $_product){
$images_obj = $this->helper('catalog/image')->init($_product, 'small_image')
->resize(38, 38);
$images[] = (string)$images_obj;
}
The reason this is happening is because the image attribute is not loaded in the product listing. Normally you can change this while editing the attribute, but you can't edit those settings for this attribute. I think that's because it is a stock attribute.
TLDR;
UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;
** Warning, you should not execute this ^^^ query until you are certain your image attribute_id for the catalog_product entity is 106!
Some answers are suggesting this method:
$_product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
echo $this->helper('catalog/image')->init($_product, 'image')->resize(38, 38);
You should not do this! This is because you will do a full product load! This is not efficient, and is most likely being done inside of a loop which is even worse! Sorry for screaming!
I usually do not condone direct DB edits, but in this case it was the easiest solution for me:
# First make sure we are using all the right IDs, who knows, I have seen some fubar'ed deployments
SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product';
# 10
SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'image' AND entity_type_id = 10;
# 106
# Now that we know the exact attribute_id....
UPDATE catalog_eav_attribute SET used_in_product_listing = 1 WHERE attribute_id = 106;
Now the image attribute data will be automagically loaded on the product listing pages, then you can access it like this:
echo $this->helper('catalog/image')->init($_product, 'image');
The best part is that you are not loading the entire product in a loop! DO NOT EVER DO THAT EVER
** Also, because I know I am going to get people saying that this is not the Magento way, the alternative would be to create a module that has an SQL setup script that runs the command.
Small image and thumbnail works great.
Then try small_image instead of image, like this:
echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(38, 38);
<img src='.$this->helper('catalog/image')->init($product, 'small_image')->resize(225, 225).' width=\'225\' height=\'225\'/>
Magento Product image assigning to Variable
$product_image = Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height).'';
Magento Product image assigning to Object
$products[] = Mage::helper('catalog/image')->init($productmodel,'small_image')->keepFrame(true)->resize(width,height).'';
It Works for me....

Grabbing a custom product image label in Magento?

I have a block on my front page that is grabbing the two newest products with a custom product image called image_feature_front_right.
I use this code as the query:
$_helper = $this->helper('catalog/output');
$_productCollection = Mage::getModel("catalog/product")->getCollection();
$_productCollection->addAttributeToSelect('*');
$_productCollection->addAttributeToFilter("image_feature_front_right", array("notnull" => 1));
$_productCollection->addAttributeToFilter("image_feature_front_right", array("neq" => 'no_selection'));
$_productCollection->addAttributeToSort('updated_at', 'DESC');
$_productCollection->setPageSize(2);
I am able to get:
the image: echo $this->helper('catalog/image')->init($_product, 'image_feature_front_right')->directResize(230,315,4)
the product url: echo $_product->getProductUrl()
the product name: $this->stripTags($_product->getName(), null, true)
The only thing I am unable to get is the custom images label. I have tried echo $this->getImageLabel($_product, 'image_feature_front_right'), but that seems to do nothing.
Can anyone point me in the right direction?
Thanks!
Tre
I imagine this is some sort of magento bug. The issue seems to be that the Magento core is not setting the custom_image_label attribute. Whereas for the default built-in images [image, small_image, thumbnail_image] it does set these attributes - so you could do something like:
$_product->getData('small_image_label');
If you look at Mage_Catalog_Block_Product_Abstract::getImageLabel() it just appends '_label' to the $mediaAttributeCode that you pass in as the 2nd param and calls $_product->getData().
If you call $_product->getData('media_gallery'); you'll see the custom image label is available. It's just nested in an array. So use this function:
function getImageLabel($_product, $key) {
$gallery = $_product->getData('media_gallery');
$file = $_product->getData($key);
if ($file && $gallery && array_key_exists('images', $gallery)) {
foreach ($gallery['images'] as $image) {
if ($image['file'] == $file)
return $image['label'];
}
}
return '';
}
It'd be prudent to extend the Magento core code (Ideally Mage_Catalog_Block_Product_Abstract, but I don't think Magento lets you override Abstract classes), but if you need a quick hack - just stick this function in your phtml file then call:
<?php echo getImageLabel($_product, 'image_feature_front_right')?>

Categories