Getting a custom attribute image












5















Magento 2 question here:



I've made a custom attribute (media type) with the name of 'bike_range_image', and tried googling and searching in the core and everything I could to get the image with a custom size and such, but can't find the right approach.



I can get it as text, but obviously that's not enough, so this won't cut it:



$productImage = $product->getCustomAttribute( 'bike_range_image' );



Now, some examples in themes that are out there suggest I do this:



$this->helper('MagentoCatalogHelperImage')->init($product, 'bike_range_image');



But this gives me an enormous MagentoCatalogHelperImage object that doesn't seem to have the right data. (I've got the right product if you're wondering).



Does anyone have some pointers for me?










share|improve this question



























    5















    Magento 2 question here:



    I've made a custom attribute (media type) with the name of 'bike_range_image', and tried googling and searching in the core and everything I could to get the image with a custom size and such, but can't find the right approach.



    I can get it as text, but obviously that's not enough, so this won't cut it:



    $productImage = $product->getCustomAttribute( 'bike_range_image' );



    Now, some examples in themes that are out there suggest I do this:



    $this->helper('MagentoCatalogHelperImage')->init($product, 'bike_range_image');



    But this gives me an enormous MagentoCatalogHelperImage object that doesn't seem to have the right data. (I've got the right product if you're wondering).



    Does anyone have some pointers for me?










    share|improve this question

























      5












      5








      5


      1






      Magento 2 question here:



      I've made a custom attribute (media type) with the name of 'bike_range_image', and tried googling and searching in the core and everything I could to get the image with a custom size and such, but can't find the right approach.



      I can get it as text, but obviously that's not enough, so this won't cut it:



      $productImage = $product->getCustomAttribute( 'bike_range_image' );



      Now, some examples in themes that are out there suggest I do this:



      $this->helper('MagentoCatalogHelperImage')->init($product, 'bike_range_image');



      But this gives me an enormous MagentoCatalogHelperImage object that doesn't seem to have the right data. (I've got the right product if you're wondering).



      Does anyone have some pointers for me?










      share|improve this question














      Magento 2 question here:



      I've made a custom attribute (media type) with the name of 'bike_range_image', and tried googling and searching in the core and everything I could to get the image with a custom size and such, but can't find the right approach.



      I can get it as text, but obviously that's not enough, so this won't cut it:



      $productImage = $product->getCustomAttribute( 'bike_range_image' );



      Now, some examples in themes that are out there suggest I do this:



      $this->helper('MagentoCatalogHelperImage')->init($product, 'bike_range_image');



      But this gives me an enormous MagentoCatalogHelperImage object that doesn't seem to have the right data. (I've got the right product if you're wondering).



      Does anyone have some pointers for me?







      magento2 image product-images






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 10 '16 at 17:20









      TommyKTommyK

      3414




      3414






















          3 Answers
          3






          active

          oldest

          votes


















          14














          I guess you want to do it in .phtml file. Try this way



          <?php
          $productImageAttr = $product->getCustomAttribute( 'bike_range_image' );
          $productImage = $this->helper('MagentoCatalogHelperImage')
          ->init($product, 'bike_range_image')
          ->setImageFile($productImageAttr->getValue());
          ?>


          and then in img tag



          <img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />


          The image size can be defined in xml file, for example view.xml



          <media>
          <images module="Magento_Catalog">
          <image id="bike_range_image" type="thumbnail">
          <width>100</width>
          <height>100</height>
          </image>
          </images>
          </media>





          share|improve this answer
























          • It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it!

            – TommyK
            Jul 15 '16 at 15:10













          • $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection()

            – LucScu
            Oct 18 '17 at 9:33



















          2














          Use the following code:



          $value = $_product->getCustomImage();
          if(isset($value) && $value != 'no_selection'):
          $imageUrl = Mage::getHelper('catalog/image')->init($_product,'custom_image');
          echo "<img src='".$imageUrl."' />";
          endif;





          share|improve this answer
























          • That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'MagentoCatalogHelperImage' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea?

            – TommyK
            Mar 14 '16 at 14:51













          • can anyone shed a light to how I can call an image for a drop-down attribute? Thank you

            – roger
            Oct 18 '16 at 16:55



















          1














          You can do this trick mentioned here:



          https://stackoverflow.com/questions/34082459/magento2-add-product-attribute-as-media-image



          but with one thing that isn't mentioned there is you will be able to just do: $_product->getAttributeCode() it will work and give you the value.



          As doing this



          $product->getCustomAttribute( 'attribute_code' )->getValue() 


          will return null;






          share|improve this answer

























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f105677%2fgetting-a-custom-attribute-image%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            14














            I guess you want to do it in .phtml file. Try this way



            <?php
            $productImageAttr = $product->getCustomAttribute( 'bike_range_image' );
            $productImage = $this->helper('MagentoCatalogHelperImage')
            ->init($product, 'bike_range_image')
            ->setImageFile($productImageAttr->getValue());
            ?>


            and then in img tag



            <img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />


            The image size can be defined in xml file, for example view.xml



            <media>
            <images module="Magento_Catalog">
            <image id="bike_range_image" type="thumbnail">
            <width>100</width>
            <height>100</height>
            </image>
            </images>
            </media>





            share|improve this answer
























            • It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it!

              – TommyK
              Jul 15 '16 at 15:10













            • $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection()

              – LucScu
              Oct 18 '17 at 9:33
















            14














            I guess you want to do it in .phtml file. Try this way



            <?php
            $productImageAttr = $product->getCustomAttribute( 'bike_range_image' );
            $productImage = $this->helper('MagentoCatalogHelperImage')
            ->init($product, 'bike_range_image')
            ->setImageFile($productImageAttr->getValue());
            ?>


            and then in img tag



            <img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />


            The image size can be defined in xml file, for example view.xml



            <media>
            <images module="Magento_Catalog">
            <image id="bike_range_image" type="thumbnail">
            <width>100</width>
            <height>100</height>
            </image>
            </images>
            </media>





            share|improve this answer
























            • It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it!

              – TommyK
              Jul 15 '16 at 15:10













            • $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection()

              – LucScu
              Oct 18 '17 at 9:33














            14












            14








            14







            I guess you want to do it in .phtml file. Try this way



            <?php
            $productImageAttr = $product->getCustomAttribute( 'bike_range_image' );
            $productImage = $this->helper('MagentoCatalogHelperImage')
            ->init($product, 'bike_range_image')
            ->setImageFile($productImageAttr->getValue());
            ?>


            and then in img tag



            <img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />


            The image size can be defined in xml file, for example view.xml



            <media>
            <images module="Magento_Catalog">
            <image id="bike_range_image" type="thumbnail">
            <width>100</width>
            <height>100</height>
            </image>
            </images>
            </media>





            share|improve this answer













            I guess you want to do it in .phtml file. Try this way



            <?php
            $productImageAttr = $product->getCustomAttribute( 'bike_range_image' );
            $productImage = $this->helper('MagentoCatalogHelperImage')
            ->init($product, 'bike_range_image')
            ->setImageFile($productImageAttr->getValue());
            ?>


            and then in img tag



            <img src="<?php echo $productImage->getUrl() ?>" alt="<?php echo $block->escapeHtml($product->getTitle()) ?>" />


            The image size can be defined in xml file, for example view.xml



            <media>
            <images module="Magento_Catalog">
            <image id="bike_range_image" type="thumbnail">
            <width>100</width>
            <height>100</height>
            </image>
            </images>
            </media>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jul 14 '16 at 12:26









            Jaanek LiiskmaaJaanek Liiskmaa

            18114




            18114













            • It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it!

              – TommyK
              Jul 15 '16 at 15:10













            • $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection()

              – LucScu
              Oct 18 '17 at 9:33



















            • It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it!

              – TommyK
              Jul 15 '16 at 15:10













            • $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection()

              – LucScu
              Oct 18 '17 at 9:33

















            It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it!

            – TommyK
            Jul 15 '16 at 15:10







            It was a while ago, but yes, ended up doing it like this. I can't vote it up yet, so please someone else do it!

            – TommyK
            Jul 15 '16 at 15:10















            $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection()

            – LucScu
            Oct 18 '17 at 9:33





            $product->getCustomAttribute('bike_range_image') is NULL in catalog category pages when $product is loaded from $block->getLoadedProductCollection()

            – LucScu
            Oct 18 '17 at 9:33













            2














            Use the following code:



            $value = $_product->getCustomImage();
            if(isset($value) && $value != 'no_selection'):
            $imageUrl = Mage::getHelper('catalog/image')->init($_product,'custom_image');
            echo "<img src='".$imageUrl."' />";
            endif;





            share|improve this answer
























            • That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'MagentoCatalogHelperImage' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea?

              – TommyK
              Mar 14 '16 at 14:51













            • can anyone shed a light to how I can call an image for a drop-down attribute? Thank you

              – roger
              Oct 18 '16 at 16:55
















            2














            Use the following code:



            $value = $_product->getCustomImage();
            if(isset($value) && $value != 'no_selection'):
            $imageUrl = Mage::getHelper('catalog/image')->init($_product,'custom_image');
            echo "<img src='".$imageUrl."' />";
            endif;





            share|improve this answer
























            • That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'MagentoCatalogHelperImage' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea?

              – TommyK
              Mar 14 '16 at 14:51













            • can anyone shed a light to how I can call an image for a drop-down attribute? Thank you

              – roger
              Oct 18 '16 at 16:55














            2












            2








            2







            Use the following code:



            $value = $_product->getCustomImage();
            if(isset($value) && $value != 'no_selection'):
            $imageUrl = Mage::getHelper('catalog/image')->init($_product,'custom_image');
            echo "<img src='".$imageUrl."' />";
            endif;





            share|improve this answer













            Use the following code:



            $value = $_product->getCustomImage();
            if(isset($value) && $value != 'no_selection'):
            $imageUrl = Mage::getHelper('catalog/image')->init($_product,'custom_image');
            echo "<img src='".$imageUrl."' />";
            endif;






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 13 '16 at 1:07









            Mr. LewisMr. Lewis

            1,288710




            1,288710













            • That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'MagentoCatalogHelperImage' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea?

              – TommyK
              Mar 14 '16 at 14:51













            • can anyone shed a light to how I can call an image for a drop-down attribute? Thank you

              – roger
              Oct 18 '16 at 16:55



















            • That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'MagentoCatalogHelperImage' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea?

              – TommyK
              Mar 14 '16 at 14:51













            • can anyone shed a light to how I can call an image for a drop-down attribute? Thank you

              – roger
              Oct 18 '16 at 16:55

















            That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'MagentoCatalogHelperImage' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea?

            – TommyK
            Mar 14 '16 at 14:51







            That is Magento 1 code, but still I managed to get it like that: $value = $product->getBikeRangeImage(); However, this is not working yet: $value = $product->getBikeRangeImage(); if( isset($value) && $value != 'no_selection' ): echo $this->helper( 'MagentoCatalogHelperImage' )->init( $product, 'bike_range_image' )->resize( 310 ); endif; That gives me an error saying it returns an object rather than an url as it should. Any idea?

            – TommyK
            Mar 14 '16 at 14:51















            can anyone shed a light to how I can call an image for a drop-down attribute? Thank you

            – roger
            Oct 18 '16 at 16:55





            can anyone shed a light to how I can call an image for a drop-down attribute? Thank you

            – roger
            Oct 18 '16 at 16:55











            1














            You can do this trick mentioned here:



            https://stackoverflow.com/questions/34082459/magento2-add-product-attribute-as-media-image



            but with one thing that isn't mentioned there is you will be able to just do: $_product->getAttributeCode() it will work and give you the value.



            As doing this



            $product->getCustomAttribute( 'attribute_code' )->getValue() 


            will return null;






            share|improve this answer






























              1














              You can do this trick mentioned here:



              https://stackoverflow.com/questions/34082459/magento2-add-product-attribute-as-media-image



              but with one thing that isn't mentioned there is you will be able to just do: $_product->getAttributeCode() it will work and give you the value.



              As doing this



              $product->getCustomAttribute( 'attribute_code' )->getValue() 


              will return null;






              share|improve this answer




























                1












                1








                1







                You can do this trick mentioned here:



                https://stackoverflow.com/questions/34082459/magento2-add-product-attribute-as-media-image



                but with one thing that isn't mentioned there is you will be able to just do: $_product->getAttributeCode() it will work and give you the value.



                As doing this



                $product->getCustomAttribute( 'attribute_code' )->getValue() 


                will return null;






                share|improve this answer















                You can do this trick mentioned here:



                https://stackoverflow.com/questions/34082459/magento2-add-product-attribute-as-media-image



                but with one thing that isn't mentioned there is you will be able to just do: $_product->getAttributeCode() it will work and give you the value.



                As doing this



                $product->getCustomAttribute( 'attribute_code' )->getValue() 


                will return null;







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited 32 mins ago









                Teja Bhagavan Kollepara

                2,98641847




                2,98641847










                answered Oct 18 '17 at 18:44









                Juliano VargasJuliano Vargas

                527522




                527522






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f105677%2fgetting-a-custom-attribute-image%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto