cancel
Showing results for 
Search instead for 
Did you mean: 

Product Page: Pass current product attribute (SKU) to URL string

Product Page: Pass current product attribute (SKU) to URL string

Hi everyone,

Been trying to work this one out for days so hoping someone can help save my sanity.

 

Issue: On the product page I want to grab the SKU attribute of the displayed product, then pass it to a URL string, so an image can be rendered and returned to the product page.

 

Current Approach: I have created a .phtml template called image_viewer.phtml which is basically an iFrame. It has the following code:

 

<?php
// This is hard-coded currently because I can’t replace this with the current product displayed 
$product_sku = '6423223';  
?> 
<!DOCTYPE html>
<html> 
<body>
<iframe width="400" height= "500" src="http://localhost/ImageViewer.html?id=<?php echo $product_sku;?>">
</iframe>
</body>
</html>

I then modified the catalog_product_view.xml file so the image viewer can be displayed on the product page... I added the following line to the xml file under the content container:

 

<block class="CustomTheme\ChildTheme\Block\Template" name="image_viewer"   template="Magento_Catalog::product/view/image_viewer.phtml"/>

The product page successfully renders the image on the product page, using the image viewer. However, I cannot figure out how to grab the SKU attribute for the product currently displayed, so that the PHP variable ($product_sku) can be populated and therefore referenced by the URL.

 

I have tried solutions on this page for PHP and none of them have worked so far. https://stackoverflow.com/questions/6924226/magento-product-attribute-get-value

 

I have also tried to use XML by adding the following code to the catalog_product_view.xml.

<block class="Magento\Catalog\Block\Product\View\Description" name="product.info.sku" i="product.info.sku"  template="product/view/attribute.phtml" after="product.info.type">
<arguments>
	<argument name="at_call" xsi:type="string">getSku</argument>
	<argument name="at_code" xsi:type="string">sku</argument>
	<argument name="css_class" xsi:type="string">sku</argument>
</arguments>			
</block>

 

This successfully displays the current product SKU on the page. However, I couldn’t manage to pass this to the URL string in the way I have done in the image_viewer.phtml file.

 

Either I need a way to grab the SKU using my image_viewer.phtml that works, OR a way to use XML to pass the SKU directly to a URL to achieve the same result. At the moment, I can’t figure out either way, so would really appreciate some help. Thank you.

 

 

2 REPLIES 2

Re: Product Page: Pass current product attribute (SKU) to URL string

Hey all. Anyone manage to look at this yet? Still having problems achieving this. Tried pretty much every suggestion on the web for getting product attributes in PHP. None of them are working. If anyone can help it would be appreciated. Cheers. 

Re: Product Page: Pass current product attribute (SKU) to URL string

Finally managed to fix this. For anyone else who has a similar problem. Everything was setup fine but the code in the catalog_product_view.xml page was the problem. It was calling the wrong template. If you're trying to get custom attributes in the same way I was, make sure to call the correct block class:

 

 <block class="Magento\Catalog\Block\Product\View" name="image_viewer"   template="product/view/image_viewer.phtml"/>

Hope that helps someone out in the future.