0

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:

  1. 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
  2. Created a CMS Block with just the "<div class="samsung"><img src="{{media url="wysiwyg/image.png"}}" alt="" /></div>" with identifier "samsung-garanci"
  3. 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; ?>
  1. 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

0

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();
}

?>
4
  • No it's Magento 2.3.3, my bad with the tags. can you please write the answer again with Magento 2 in mind? – JadoBulku Feb 22 at 1:00
  • @JadoBulku updated the answer with Magento2 code. – Haijerome Feb 22 at 3:39
  • 1
    It worked, THANK YOU very much, been a week trying to figure it out but was looking at 1.9 answers I guess. – JadoBulku Feb 22 at 13:05
  • @JadoBulku You are welcome and glad that you are able to sort this out eventually ! – Haijerome Feb 23 at 5:51
0

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; ?>
5
  • have I placed it correctly on catalog_product_view.xml? – JadoBulku Feb 21 at 20:12
  • Yes. It should be placed on catalog_product_view.xml – Mehmet Uygur Feb 21 at 20:23
  • Thanks for helping me, but still doesnt show, maybe I should change the Container name? I have 2 with the same name: – JadoBulku Feb 22 at 1:08
  • Please check the screenshot prntscr.com/1039r1p – JadoBulku Feb 22 at 1:09
  • It thought you are working on magento1 but I understand that you're are using 2. So you are trying wrong code on wrong magento version. – Mehmet Uygur Feb 22 at 2:50

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.