Custom Block to Product Page with selection attribute is not showing
-
15-04-2021 - |
Question
I am trying to create an option where I can select yes or no in the backend, and when it's set to yes, it adds a block with an image in the product page. Here's what I have so far:
- I created a
Yes/No
Attribute called "samsung_5_vjet_garanci
" and made sure its Visible on Catalog Pages on Storefront. Added it on a sample product, and set it to YES - Created a CMS Block with just the "
<div class="samsung"><img src="{{media url="wysiwyg/image.png"}}" alt="" /></div>
" with identifier "samsung-garanci
" - Created a .phtml file and uploaded it
/app/design/frontend/vendor/themename/Magento_Catalog/templates/product/view/samsung.phtml
<?php $_product = Mage::getSingleton('catalog/product')
->load($_product->getId());
if ($_product->getAttributeText('samsung_5_vjet_garanci') == "Yes"): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('samsung-garanci')->toHtml(); ?>
<?php endif; ?>
Added a block in catalog_product_view.xml
<referenceContainer name="product.info.main"> <block class="Magento\Catalog\Block\Product\View" name="samsung" as="samsung" after="-" template="Magento_Catalog::product/view/samsung.phtml" /> </referenceContainer>
But it's not working, what am I doing wrong, it's been 3 days figuring this out, and still nothing. Thank you very much if you help me
La solution
It's mentioned that you use Magento 1.9.x
( as per the tags added), but I can see that you have magento2 style of code
and magento2 directory structure
in your layout XML (catalog_product_view.xml
). In short there is a mixup of code. Check and Confirm your Magento version at first and then add the respective style of code
In Magento 1,
Create and register a custom theme
The layout and template paths should be
app/design/frontend/vendor/theme/layout/catalog.xml
better create a local.xml
for layout overrides
app/design/frontend/vendor/theme/catalog/product/view/samsung.phtml
and in your layout XML it should be
<catalog_product_view>
<reference name="product.info">
<block type="catalog/product_view" name="samsung_brand" as="samsung.brand" template="catalog/product/view/samsung.phtml"/>
</reference>
</catalog_product_view>
So confirm your Magento version, learn the theme creation, directory structure and layout directives to be used according to the version of Magento.
In case of Magento2, it should be
app/design/frontend/Vendor/theme/Magento_Catalog/layout/catalog_product_view.xml
<?xml version="1.0"?>
<!--
/**
* @copyright (c) 2020
* @category StackExchange
* @package StackExchange_Answers
* @version 1.0.0
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="content">
<referenceBlock name="product.info.main">
<block class="Magento\Catalog\Block\Product\View" name="samsung" as="samsung" after="-" template="Magento_Catalog::product/view/samsung.phtml" />
</referenceBlock>
</referenceContainer>
</body>
</page>
app/design/frontend/Vendor/theme/Magento_Catalog/templates/product/view/samsung.phtml
<?php /** @var $block \Magento\Catalog\Block\Product\AbstractProduct */ ?>
<?php
$_product = $block->getProduct();
// just to print your attribute value for your confirmation, you can comment or remove the print statement below if you want
echo '<br/>Sku : ' . $_product->getSku() .' | samsung_5_vjet_garanci: '. $_product->getData('samsung_5_vjet_garanci') .' | samsung_5_vjet_garanci: '.$_product->getSamsung5VjetGaranci();
if ($_product->getData('samsung_5_vjet_garanci')) {
echo $block->getLayout()->createBlock(\Magento\Cms\Block\Block::class)->setBlockId('samsung-garanci')->toHtml();
}
?>
Autres conseils
First of all you don't need to get getAttributeText('.....) if you are using a yes/no attribute. It's better to use
<?php $_product = $this->getProduct(); // you don't need to load product its already present.
if ($_product->getAttribute('samsung_5_vjet_garanci')): ?>
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('samsung-garanci')->toHtml(); ?>
<?php endif; ?>