6

I have a data module that gets information for a product and its attributes associated with it like color, sizes etc. In here there is an image that needs to be taken and put into an array in particular the thumbnail images.

Here is the code that I have put together/grabbed:

if (!in_array($size_name, $available_sizes)) {

    $available_sizes[] = array(
        'id'                    => $asc_product->getId(), // Product ID
        'color'                 => $this->getColorName($asc_product), // Color name used for reference?
        'size'                  => $size_name, // Ring size
        'product_type'          => $attribute_set_name,
        'product_image'         => $asc_product->getImageUrl(),
        'product_image'         => $imageThumbnail,
        'product_image_label'   => 'labelle' // Main product image
    );
}

Inside the array I need to get the thumbnail of that product. This is using 'product_image' => $asc_product->getImageUrl(),

Currently I use this in most of my product pages to get the image url for each image size:

$imageThumbnail = Mage::getModel('catalog/product_media_config')->getMediaUrl( $_product->getThumbnail() );

Does anyone know how I can get the correct image size the in the array at the top?

====== Added the data helper file content here =======

<?php


class Mawi_Products_Helper_Data extends Mage_Core_Helper_Abstract {


    // --------------------------------------------------------------------

    /**
     * Tests to see if a product has a comparison image
     */
    function hasComparisonImage($_product)
    {
        return ((strlen($_product->getComparisonImage()) > 0) && ($_product->getComparisonImage() != "no_selection")) ? true : false;
    }


    // --------------------------------------------------------------------

    /**
     * Gets all ring sizes
     */
    function getAllRingSizes()
    {   
        $all_ring_sizes     = array();

        $get_ring_sizes  = Mage::getModel('eav/config')->getAttribute('catalog_product', 'ring_size_uk'); 

        foreach ($get_ring_sizes->getSource()->getAllOptions(true, true) as $ring_size) {

            if (strlen($ring_size['label']) > 0) {

                $all_ring_sizes[] = $ring_size['label'];

            }

        }

        return (count($all_ring_sizes) > 0) ? $all_ring_sizes : false;

    }

    // --------------------------------------------------------------------

    /**
     * Gets all bracelet sizes
     */
    function getAllBraceletSizes()
    {
        $get_bracelet_sizes  = Mage::getModel('eav/config')->getAttribute('catalog_product', 'bracelet_size_uk'); 

        foreach ($get_bracelet_sizes->getSource()->getAllOptions(true, true) as $bracelet_size) {

            if (strlen($bracelet_size['label']) > 0) {

                // Remove the "One Size" bracelet size
                if ($bracelet_size['label'] !== 'One Size') {

                  $all_bracelet_sizes[] = $bracelet_size['label'];

                }

            }

        }

        return (count($all_bracelet_sizes) > 0) ? $all_bracelet_sizes : false;

    }

    // --------------------------------------------------------------------

    /**
     * Gets all sizes for the product type
     */
    function getAllSizes($_product)
    {

        // Attr set name 
        $attribute_set_name         = $this->getAttributeSetName($_product);

        switch ($attribute_set_name) {
          case 'rings':
            $all_sizes    = $this->getAllRingSizes();
            break;

          case 'bracelets':
            $all_sizes    = $this->getAllBraceletSizes();
            break;

          default:
            $all_sizes    = false;
            break;
        }

        return $all_sizes;

    }



    // --------------------------------------------------------------------

    /**
     * Gets available sizes for a given product
     */
    function getAvailableSizes($available_sizes, $asc_product, $attribute_set_name)
    {   

        if ($attribute_set_name == 'bracelets') {

            $attr          = $asc_product->getResource()->getAttribute("bracelet_size_uk");
            $size_name     = $attr->getSource()->getOptionText($asc_product->getBraceletSizeUk());


        } else if ($attribute_set_name == 'rings') {

            $attr          = $asc_product->getResource()->getAttribute("ring_size_uk");
            $size_name     = $attr->getSource()->getOptionText($asc_product->getRingSizeUk());

        }


        if (!in_array($size_name, $available_sizes)) {

            $available_sizes[] = array(
                'id'                      => $asc_product->getId(), // Product ID
                'color'                   => $this->getColorName($asc_product), // Color name used for reference?
                'size'                    => $size_name, // Ring size
                'product_type'            => $attribute_set_name,
                'product_image'           => $asc_product->getImageUrl(),
                'product_image_label'     => 'labelle' // Main product image
            );

        }

        return $available_sizes;
    }



    // --------------------------------------------------------------------

    /**
     * Gets the available colours for a product
     */
    function getAvailableColors($available_colors, $asc_product)
    {

        $color_name         = $this->getColorName($asc_product);

        if (!in_array($color_name, $available_colors)) {

            $available_colors[] = $color_name;

        }

        return (count($available_colors) > 0) ? $available_colors : false;

    }



    // --------------------------------------------------------------------

    /**
     * Gets a colour name string for a particular simple product
     */
    function getColorName($asc_product)
    {

        $color_attr         = $asc_product->getResource()->getAttribute("color");
        $color_name         = $color_attr->getSource()->getOptionText($asc_product->getColor());

        return $color_name;

    }

    // --------------------------------------------------------------------

    /**
     * Gets an attribute set name for a product
     */
    function getAttributeSetName($_product)
    {

        $attribute_set_model    = Mage::getModel("eav/entity_attribute_set");

        $attribute_set_model->load($_product->getAttributeSetId());

        $attribute_set_name     = strtolower($attribute_set_model->getAttributeSetName());

        return $attribute_set_name;

    }










}

======= end =======

I am struggling to think of what it is, even though it is a simple idea.

Cheers

11

Do you mean resizing it in the array? In that case you can use the Magento catalog image helper.

'product_image' => (string)Mage::helper('catalog/image')->init($asc_product, 'thumbnail')->resize(100);

This would return you the url to the resized image.

  • I don't want to resize it in the array though,it should still work right? – M dunbavan Sep 16 '13 at 19:47
  • Hm yup, omit the resize function and you should be good to go – Sander Mangel Sep 16 '13 at 19:48
  • Hmmm, that gives me a blank page for some reason 'product_image' => (string)$this->helper('catalog/image')->init($asc_product, 'thumbnail')->resize(100); It may be worth adding that this is in a class Mawi_Products_Helper_Data extends Mage_Core_Helper_Abstract class name file. – M dunbavan Sep 16 '13 at 19:52
  • The issue probably is that $asc_product isn't and instance of Mage_Catalog_Model_Product, so that would mean you have to load the product model using the associated product. – Sander Mangel Sep 16 '13 at 20:03
  • Would you suggest that I use $_product = Mage::getModel('catalog/product')->load(); and then look for the url on that? – M dunbavan Sep 16 '13 at 20:12
1

The Product image URL can be taken by using product helper class. The same class we are using in the product list page.

Mage::helper('catalog/image')->init($asc_product , 'thumbnail');

Hope this will help you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.