0

I have a magento2 based website and I am wanting to show a custom attribute on the category listing page. My issue seems to be that on desktop and tablet the products are shown 3 across and on mobile they are shown 2 across.

Now if the last item in the row shouldn't show the custom attribute (or has no value set for the custom attribute) it still shows the prior products custom attribute value.

Below is my code. I know using Object Manager in the php file is not the ideal. the php file edited is

  Magento_Catalog/templates/product/price/final_price.phtml

//THIS CODE WILL HELP US WRITE TEXT UNDER THE PRICE SUCH AS BOGO, ETC
  $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  $_productdata = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
  unset($promo_text);
  $promo_text = $_productdata->getData('promo_text');
  $promo_text_start_date = $_productdata->getData('promo_text_start_date');
  $promo_text_end_date = $_productdata->getData('promo_text_end_date');
  $todays_datefor_promo_text = date('Y-m-d h:i:s');

  if($promo_text !='')
  {

      if($todays_datefor_promo_text >= "$promo_text_start_date 00:00:00" && $todays_datefor_promo_text <= "$promo_text_end_date 11:59:59")
       {
          echo "<p class='bogotextcolor'>$promo_text</p>";
       }
  }

Has you can see I even tried using the unset() function before I call in the custom attribute. However no matter what I do the very next product after the product that has a value for the custom attribute if the net product does not have a value it shows the prior products value(s).

Its odd because if I do

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_productdata = $objectManager->get('Magento\Catalog\Model\Product')->load($productId);
$theitemskunumber_is = $_productdata->getData('sku');

echo "$theitemskunumber_is"; 

all the products that are last in each row show the correct sku.

Yet if I use a the same code but call in a custom attribute it doesnt show . correctly on the last item in the row?

  • You can use by js because this is highly cached page. – Sohel Rana Apr 24 at 16:03
  • I am not sure what use by js means? – Jayreis Apr 24 at 16:07
  • javascript = js – Sohel Rana Apr 24 at 16:10
1

Try following way:


$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_productdata = $objectManager->create('Magento\Catalog\Model\Product')->load($productId);
unset($promo_text);
  $promo_text = $_productdata->getData('promo_text');
  $promo_text_start_date = $_productdata->getData('promo_text_start_date');
  $promo_text_end_date = $_productdata->getData('promo_text_end_date');
  $todays_datefor_promo_text = date('Y-m-d h:i:s');

  if($promo_text !='')
  {

      if($todays_datefor_promo_text >= "$promo_text_start_date 00:00:00" && $todays_datefor_promo_text <= "$promo_text_end_date 11:59:59")
       {
          echo "<p class='bogotextcolor'>$promo_text</p>";
       }
  }

Note: Avoid to use object manager directly and avoid to load product that will impact your page speed.

  • Thank you Sohel Rana. So I guess i will look up the difference between $objectManager->create and $objectManager->get – Jayreis Apr 24 at 16:45

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.