0

I wanted to be able to display a link on a product that will open up a new window showing a Size Guide if one is available. To do this I added a new Media Image attribute called size_guide_media and assigned it the type Media Image and added it to the product attribute group. It now shows as a column within the Images tab. Excellent. I can now assign an uploaded image as a size guide.

So I wrote this code into view.php to manage whether or not to show a link that opens up an assigned size guide:

<?php 
    // Check if size guide media attr has been set for product
    if ($_helper->productAttribute($_product, $_product->getSizeGuideMedia())) { 
    // Assign size guide img url to variable
    $size_guide_url = Mage::helper('catalog/image')->init($_product, 'size_guide_media'); 
?>
<a href="<?php echo $size_guide_url; ?>" onclick="javascript:void window.open('<?php echo $size_guide_url; ?>','1435746173051','width=800,height=600,toolbar=0,menubar=0,location=0,status=0,scrollbars=1,resizable=1,left=30,top=30');return false;" class="size-guide-button"><i class="fa fa-file-text-o"></i><?php echo $this->__('Size Guide'); ?></a>
<?php } ?> 

It works... almost. An issue arises when the following is done:

  • I add a size guide to a product and set it to the size guide attribute
  • Saved product now shows size guide link and works correctly.
  • I now go back to the product editor, and set the Size Guide column under the images tab to no media and save the product.
  • I Refresh the same product on the front end and the screen goes completely white.

I'm assuming this is down to my if statement and how it is checking to see if the variable size_guide_media is in use...

This bit:

  if ($_helper->productAttribute($_product, $_product->getSizeGuideMedia())) { 

My question is what happens when a media image attribute is set to no image? Because this is when my code falls over...

Any thoughts?

1 Answer 1

0

If the screen is going white, you should be able to set Mage::setIsDeveloperMode(true);

Once that is set, you should start seeing some information as to what is causing the problem. I suspect that the problem is as follows

1) Products who's size_guide_media have never been set do not have a value. Therefore, never execute your code. 2) Once you set a value - your code will execute. 3) If you "clear" the value - it's trying to load a URL on an empty string or a string set to "". This causes problems, and crashes the page.

I suspect if you add a 2nd check into your code thusly - things will work:

<?php 
// Check if size guide media attr has been set for product
if ($_helper->productAttribute($_product, $_product->getSizeGuideMedia())) { 
// Assign size guide img url to variable
**if ($_product->getSizeGuideMedia() <> "") {**
     $size_guide_url = Mage::helper('catalog/image')->init($_product, 'size_guide_media'); 
**}**

Give that a try - and let us know.

1
  • I meant to come back to you on this but I forgot - Okay so it nearly works but required another check to ensure the attribute isn't in "no_selection" mode after previously having been set with an image if ($_product->getSizeGuideMedia() <> "" && $_product->getSizeGuideMedia() != "no_selection") {
    – Jake
    Commented Apr 21, 2016 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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