1

this is my current final_price.phtml file. the line

$category = $product->getCategoryIds(); is causing error

I'm trying to get the category_id of the current product, and check if it's in $hiddenCategories array. If it is in the hiddenCategories, i would like to hide the price for that item.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>

<?php
/** @var \Magento\Catalog\Pricing\Render\FinalPriceBox $block */

/** ex: \Magento\Catalog\Pricing\Price\RegularPrice */
/** @var \Magento\Framework\Pricing\Price\PriceInterface $priceModel */
$priceModel = $block->getPriceType('regular_price');

/** ex: \Magento\Catalog\Pricing\Price\FinalPrice */
/** @var \Magento\Framework\Pricing\Price\PriceInterface $finalPriceModel */
$finalPriceModel = $block->getPriceType('final_price');
$idSuffix = $block->getIdSuffix() ? $block->getIdSuffix() : '';
$schema = ($block->getZone() == 'item_view') ? true : false;

?>

<?php 
/*get category id*/
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $categoryCollection = $objectManager->get('\Magento\Catalog\Model\ResourceModel\Category\CollectionFactory');
    $productRepository = $objectManager->get('\Magento\Catalog\Model\ProductRepository');
    $product = $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product
    //$productId = $product->getId();
    $category = $product->getCategoryIds(); /*will return category ids array*/   //this is the line causing problem
    $hiddenCategories = array('375','376','377','378','379','380','381','382','383','384','385','386','387','388','389');
    //$category = $block->getCategoryIds();
        //if ($categories AS $category) {
        //if (in_array($category, $hiddenCategories)) {
            /*dont' show price*/
        //  break;
        //   }
?>
<?php if (!in_array($category, $hiddenCategories)): /*only show price if category is not in array*/?>
<?php if ($block->hasSpecialPrice()): ?>
    <span class="special-price">
        <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
            'display_label'     => __('Special Price'),
            'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
            'price_type'        => 'finalPrice',
            'include_container' => true,
            'schema' => $schema
        ]); ?>
    </span>
    <span class="old-price">
        <?php /* @escapeNotVerified */ echo $block->renderAmount($priceModel->getAmount(), [
            'display_label'     => __('Regular Price'),
            'price_id'          => $block->getPriceId('old-price-' . $idSuffix),
            'price_type'        => 'oldPrice',
            'include_container' => true,
            'skip_adjustments'  => true
        ]); ?>
    </span>
<?php else: ?>
    <?php /* @escapeNotVerified */ echo $block->renderAmount($finalPriceModel->getAmount(), [
        'price_id'          => $block->getPriceId('product-price-' . $idSuffix),
        'price_type'        => 'finalPrice',
        'include_container' => true,
        'schema' => $schema
    ]); ?>
<?php endif; ?>
<?php if ($block->showMinimalPrice()): ?>
    <?php if ($block->getUseLinkForAsLowAs()):?>
        <a href="<?= /* @escapeNotVerified */ $block->getSaleableItem()->getProductUrl() ?>" class="minimal-price-link">
            <?= /* @escapeNotVerified */ $block->renderAmountMinimal() ?>
        </a>
    <?php else:?>
        <span class="minimal-price-link">
            <?= /* @escapeNotVerified */ $block->renderAmountMinimal() ?>
        </span>
    <?php endif?>
<?php endif; ?>
<?php endif; /*end don't show price*/?>
  • what error you are facing ?? – Narendra Vyas Mar 24 at 6:51
  • it show the page out of content. Am I suppose able to get the category id this way? thanks – Kris Wen Mar 24 at 7:15
  • I think yes you are doing right but there is some fatal error, can check by ctrl+u . Enable development mode and uncomment this line ini_set('display_errors', 1); in (app/bootstrap.php) – Narendra Vyas Mar 24 at 7:29
  • can you help me check my update code. i think it is not returning error now, but not making any effect on hiding price. – Kris Wen Mar 24 at 7:58
  • do you know how can i resolve this "1 exception(s): Exception #0 (Exception): Notice: Undefined property: Magento\Catalog\Pricing\Render\FinalPriceBox\Interceptor::$_productFactory in /public_html/vendor/magento/framework/View/TemplateEngine/Php.php on line 110" – Kris Wen Mar 24 at 8:22
1

You can try doing this way:

$hiddenCategories = array('375','376','377','378','3','379','380','381','382','383','384','385','386','387','388','389');
$categories = $block->getSaleableItem()->getCategoryIds();

Change your condition like this:

<?php if (!empty(array_intersect($categories, $hiddenCategories))): ?>

You can not use in_array as both are array so you need to find if there is any intersection between the two arrays.

Try using this way and let me know if you face any issue.

Also there can be more than one final_price.phtml called for various product types so make sure you check in all cases.

  • Thank you so much! this works wonderfully. I made it too complicated then it is. Thanks! – Kris Wen Mar 24 at 9:05
  • Glad that you made it done :) – Narendra Vyas Mar 24 at 9:05
  • do you happen to know if i can update individual custom attribute value through csv? magento.stackexchange.com/questions/266887/… – Kris Wen Mar 24 at 9:23
  • I have reverted please check and let me know if that works for you. – Narendra Vyas Mar 24 at 9:49

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.