Magento: Get Image Gallery in list.phtml - magento

How can I grab the image gallery into the category page in Magento, for a loaded product?
this->getImageGallery($_product)
, won't work..

$product = Mage::getModel('catalog/product')->load($_product->getId());
foreach ($product->getMediaGalleryImages() as $image) {
echo var_export($image->getUrl());
}

If I add the images to a product programmatically then $product->getMediaGalleryImages() has zero images... however
$mediaApi = Mage::getModel('catalog/product_attribute_media_api');
$mediaItems = $mediaApi->items($product->getId());
foreach ($mediaItems as $image) {
error_log('found url: '.$image['file']);
}
works fine

Here's another method which doesn't require loading the entire product object.
<?php $media_gallery = $_product->getMediaGallery(); ?>
<?php foreach ($media_gallery['images'] as $_image):?>
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'image', $_image['file'])->resize(135); ?>" alt="<?php echo $this->escapeHtml($_image['label']) ?>" />
<?php endforeach; ?>

Related

Images does not show up

I wonder what's wrong with my codes that the image does not show up as it suppose to. Nothing appears. Just blank. I expect an image would appears.
views/editgalleries.php
<?php foreach ($pictures as $pictures_item): ?>
<td><br><img src="<?php echo base_url('../uploads/'); ?><?php echo $pictures_item['galleries_picture_name']; ?>" height="300" width="200"></td>
<?php endforeach; ?>
Cpages.php
public function edit_galleries_picture()
{
$gallery_id = $this->uri->segment(3);
$data['pictures'] = $this->Mpages->call_gallery_pictures($gallery_id);
$this->load->view('editgalleries', $data);
}
models/Mpages.php
public function call_gallery_pictures($gallery_id)
{
$this->db->where('gallery_id', $gallery_id);
$query = $this->db->get('galleries_pictures');
return $query->result_array();
}
set uploads folder in root directory and try this code
<?php echo base_url('uploads/'.$pictures_item['galleries_picture_name']); ?>
<img src="<?php echo base_url('uploads'); ?>/<?php echo $pictures_item['galleries_picture_name']; ?>" height="300" width="200">
Note that for the 'base_url('../uploads/')',
the '../' part is not required because it's give your host name.

Change the path of product images path in magento site

In product view page page, images are serving from this path : link1
media/catalog/product/cache/1/image/350x350/9df78eab33525d08d6e5fb8d27136e95/c/h/image-name.jpg :
but I want to serve from this path : link2
`media/cache/images/1/thumbnail/602f0fa2c1f0d1ba5e241f914e856ff9/catalog/product/c/image-name.jpg` :
media.phtml
<?php
$_product = $this->getProduct();
$_helper = $this->helper('catalog/output');
$dexxtz = Mage::helper('productzoom');
$dexxtz->getCss();
$dexxtz->getJs();
?>
<ul id="etalage">
<li>
<img class="etalage_thumb_image" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image')); ?>" />
<img class="etalage_source_image" title="<?php echo $_product->getImageLabel(); ?>" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image'), true); ?>" />
</li>
<?php
foreach ($this->getGalleryImages() as $_image) {
if(Mage::registry('current_product')->getImage() != $_image->getFile()) { ?>
<li>
<img class="etalage_thumb_image" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())); ?>" />
<img class="etalage_source_image" title="<?php echo $_image->getLabel(); ?>" src="<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile()), true); ?>" />
</li>
<?php
}
}
?>
</ul>
You need first to copy app/code/core/Mage/Catalog/Model/Product/Image.php to app/code/local/Mage/Catalog/Model/Product/Image.php.
Then take a look to the file you just copied, l.313-319 :
// build new filename (most important params)
$path = array(
Mage::getSingleton('catalog/product_media_config')->getBaseMediaPath(),
'cache',
Mage::app()->getStore()->getId(),
$path[] = $this->getDestinationSubdir()
);
This "$path" array will build your catalog image path. Change it to whatever you like. In your case :
// build new filename (most important params)
$path = array(
Mage::getBaseDir('media'),
'cache/images',
Mage::app()->getStore()->getId(),
$path[] = $this->getDestinationSubdir()
);
Dont forget to modify clear cache path too, l.686 :
public function clearCache()
{
$directory = Mage::getBaseDir('media') . DS.'catalog'.DS.'product'.DS.'cache'.DS;
... to ...
public function clearCache()
{
$directory = Mage::getBaseDir('media') . DS.'cache'.DS.'images'.DS;
Next, go to your media.phtml file. Change :
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'image')); ?>
...
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'image', $_image->getFile())); ?>
... to ...
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($_product, 'thumbnail')); ?>
...
<?php echo $dexxtz->getImageFeatured($this->helper('catalog/image')->init($this->getProduct(), 'thumbnail', $_image->getFile())); ?>

not getting magento image url from bestseller

I wrote some code for best-seller product in magento but
I m not getting product image url ....
I need small image for the gallery..
I m developing bestseller slider .....the whole thing is good
but only i m not getting image url...
my code
$current_category = Mage::registry('current_category');
$is_category_filter = Mage::getStoreConfig('productslider/product_setting/category_filter');
$collection = Mage::getResourceModel('reports/product_collection')
->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes())
->addOrderedQty()
->addMinimalPrice()
->addTaxPercents()
->addStoreFilter();
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
Mage::getSingleton('catalog/product_visibility')->addVisibleInSearchFilterToCollection($collection);
if($current_category && $is_category_filter == '1'){
$current_category_id = Mage::registry('current_category')->getId();
$currentCategory = Mage::getModel('catalog/category')->load($current_category_id);
$collection->addCategoryFilter($currentCategory);
}
$collection->setPageSize(30);
$this->setProductCollection($collection);
return $collection;
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(160, 225) ?>" width="160" height="225" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" />

Magento - Set category in layered navigation

Whenever I search a product using the search box, on the left side there is a filter by category. The code that creates this filter is this one:
<dl id="narrow-by-list">
<?php $_filters = $this->getFilters() ?>
<?php foreach ($_filters as $_filter): ?>
<?php if($_filter->getItemsCount()): ?>
<?php if($_filter->getName() != "Category"){ ?>
<dt><?php echo $this->__($_filter->getName()) ?></dt>
<dd>
<?php echo $_filter->getHtml() ?>
</dd>
<?php } endif; ?>
<?php endforeach; ?>
</dl>
This shows only the main category in the filter, and I'd like to show its subcategories. I tried to set another category programmatically with this code:
<?php
$layer = Mage::getSingleton('catalog/layer');
$_categ = Mage::getModel('catalog/category')->load(5);
$layer->setCurrentCategory($_categ);
?>
... but nothing changed. Any thoughts?
When you try to set the other category on the layer singleton from a template, it's too late as all the treatments were already applied.
What you can do is to copy the file app/code/core/Mage/CatalogSearch/Model/Layer.php into app/code/local/Mage/CatalogSearch/Model/ and to add a modified version of the base Mage_Catalog_Model_Layer::getCurrentCategory() method, looking like this :
public function getCurrentCategory()
{
$category = $this->getData('current_category');
if (is_null($category)) {
if ($category = Mage::registry('current_category')) {
$this->setData('current_category', $category);
}
else {
$category = Mage::getModel('catalog/category')->load(5); // Put here the ID of the category you want to use as basis for category filtering
$this->setData('current_category', $category);
}
}
return $category;
}

Displaying Specific Magento Categories From Category ID

As of yet, I haven't managed to find anything online that already caters for what I'm trying to achieve. I simply want to call in specific categories to a list but I want to be able to define which categories by ID, so for example, I would like to be able to call them in like something such as the below:-
{{block type="catalog/navigation" name="catalog.category" template="developer/extension/script.phtml" ids="3,6,17,143,57"}}
I'm already displaying a list of sub categories in various places based on the parent category ID but in instances where there are hundreds of sub categories, it isn't always practical to display all of them, so I'm wondering if the existing script can possibly be tweaked to only include specific categories as per above?
<?php
//gets all sub categories of parent category 'cat-id-4'
$cats = Mage::getModel('catalog/category')->load(4)->getChildren();
$catIds = explode(',',$cats);
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
If anyone could advise how I could possibly achieve this please, that would be fantastic - Thanks in advance.
This should work with your given CMS block code:
<?php
$catIds = explode(',', $this->getIds()); //<-- ONLY CHANGE MADE
$categories = array();
foreach($catIds as $catId) {
$category = Mage::getModel('catalog/category')->load($catId);
$categories[$category->getName()] = array(
'url' => $category->getUrl(),
'img' => $category->getImageUrl()
);
}
ksort($categories, SORT_STRING);
?>
<ul>
<?php if($category->getIsActive()): ?>
<?php foreach($categories as $name => $data): ?>
<li>
<?php echo $name; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>

Resources