0

I've searched for this answer, and no one seems to have answered it. Does anyone know how to handle this?

Here is my current code:

<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$storeInformation = $objectManager->create('Magento\Store\Model\Information');
$store = $objectManager->create('Magento\Store\Model\Store');
$storeInfo = $storeInformation->getStoreInformationObject($store);
$myBlock = $objectManager->get('Freshley\GetProduct\Block\CurrentProduct');
$product = $myBlock->getCurrentProduct();
?>

<?php if($_product->getData('show_price')): ?>
    <?php if($_product->getPriceInfo()->getPrice('final_price')->getValue() == 0): ?>
        <span class="price-call"><a href="tel:+1<?php echo $storeInfo->getPhone(); ?>">Call for Price!</a></span>
    <?php else: ?>
        <span class="price-container <?= /* @escapeNotVerified */ $block->getAdjustmentCssClasses() ?>"
                <?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
            <?php if ($block->getDisplayLabel()): ?>
                <span class="price-label"><?= /* @escapeNotVerified */ $block->getDisplayLabel() ?></span>
            <?php endif; ?>
            <?php if($_product->getData('priced_starting_at')): ?>
                <div class="price-starting-at">Starting at:</div>
            <?php else: ?>
                <div class="price-regular">Our Price:</div> 
            <?php endif; ?>
            <span <?php if ($block->getPriceId()): ?> id="<?= /* @escapeNotVerified */ $block->getPriceId() ?>"<?php endif;?>
                <?= ($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
                data-price-amount="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>"
                data-price-type="<?= /* @escapeNotVerified */ $block->getPriceType() ?>"
                class="price-wrapper <?= /* @escapeNotVerified */ $block->getPriceWrapperCss() ?>"
            ><?= /* @escapeNotVerified */ $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?></span>
            <?php if ($block->hasAdjustmentsHtml()): ?>
                <?= $block->getAdjustmentsHtml() ?>
            <?php endif; ?>
            <?php if ($block->getSchema()): ?>
                <meta itemprop="price" content="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>" />
                <meta itemprop="priceCurrency" content="<?= /* @escapeNotVerified */ $block->getDisplayCurrencyCode() ?>" />
            <?php endif; ?>
        </span>
    <?php endif; ?>
<?php endif; ?>

I also try to extend $block with getCurrentProduct using this code:

<?php
namespace Freshley\GetProduct\Block;
class CurrentProduct extends \Magento\Framework\View\Element\Template
{
    protected $_registry;

    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    )
    {
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    public function _prepareLayout()
    {
        return parent::_prepareLayout();
    }

    public function getCurrentProduct()
    {
        return $this->_registry->registry('current_product');
    }
}

This works just fine on Product Detail pages, but everywhere else that shows the price - I am getting this error:

Fatal error: Uncaught Error: Call to a member function getData() on null in /chroot/home/davidfai/davidfairclough.com/html/app/design/frontend/Freshley/ultimo_child/Magento_Catalog/templates/product/price/amount/default.phtml

1 Answer 1

1

Please try this code and make sure you are on product page other wise you need load product by Id.

<?php
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $storeInformation = $objectManager->create('Magento\Store\Model\Information');
    $store = $objectManager->create('Magento\Store\Model\Store');
    $storeInfo = $storeInformation->getStoreInformationObject($store);
    $product = $block->getCurrentProduct() ? $block->getCurrentProduct() : $objectManager->get('Magento\Framework\Registry')->registry('current_product');//get current product

?>
<?php if($product): ?>
    <?php if($_product->getData('show_price')): ?>
        <?php if($_product->getPriceInfo()->getPrice('final_price')->getValue() == 0): ?>
            <span class="price-call"><a href="tel:+1<?php echo $storeInfo->getPhone(); ?>">Call for Price!</a></span>
        <?php else: ?>
            <span class="price-container <?= /* @escapeNotVerified */ $block->getAdjustmentCssClasses() ?>"
                    <?= $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
                <?php if ($block->getDisplayLabel()): ?>
                    <span class="price-label"><?= /* @escapeNotVerified */ $block->getDisplayLabel() ?></span>
                <?php endif; ?>
                <?php if($_product->getData('priced_starting_at')): ?>
                    <div class="price-starting-at">Starting at:</div>
                <?php else: ?>
                    <div class="price-regular">Our Price:</div> 
                <?php endif; ?>
                <span <?php if ($block->getPriceId()): ?> id="<?= /* @escapeNotVerified */ $block->getPriceId() ?>"<?php endif;?>
                    <?= ($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
                    data-price-amount="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>"
                    data-price-type="<?= /* @escapeNotVerified */ $block->getPriceType() ?>"
                    class="price-wrapper <?= /* @escapeNotVerified */ $block->getPriceWrapperCss() ?>"
                ><?= /* @escapeNotVerified */ $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?></span>
                <?php if ($block->hasAdjustmentsHtml()): ?>
                    <?= $block->getAdjustmentsHtml() ?>
                <?php endif; ?>
                <?php if ($block->getSchema()): ?>
                    <meta itemprop="price" content="<?= /* @escapeNotVerified */ $block->getDisplayValue() ?>" />
                    <meta itemprop="priceCurrency" content="<?= /* @escapeNotVerified */ $block->getDisplayCurrencyCode() ?>" />
                <?php endif; ?>
            </span>
        <?php endif; ?>
    <?php endif; ?>
<?php endif; ?>
5
  • I have it working for the product detail page, but I'd like it to work on the Product Listing page, and any other pages using the sidebar Wishlist. How would I go about loading the product ID?
    – user12555
    Commented Jan 22, 2019 at 17:57
  • Can I know exactly where you want to show above code because above code needs current product and if you want to make same code work on another page then need product id so that you your code can get product by id. And also may I know on which page you are unable show above code. Commented Jan 22, 2019 at 18:21
  • Anywhere the price is supposed to show. So the Product Detail page, the Product Listing page, the Wishlist page and any page that shows the sidebar with Wishlist in the sidebar. Which means product pages and account pages. Why I want to edit the price/amount/default.phtml file as it needs to affect any page showing the price of the product. Right now the product detail page works just fine. Nothing else is working. All of my statements work. I actually have edited the code and added in a class extender. I will edit my previous code.
    – user12555
    Commented Jan 22, 2019 at 18:24
  • You can now see my full code I am using. The extended class is working, as I have successfully activated it. And as I said, it's working on the product detail page. But anywhere else is having an issue right now.
    – user12555
    Commented Jan 22, 2019 at 18:29
  • Any luck with this? It would be greatly appreciated. Would save me so much time.
    – user12555
    Commented Jan 23, 2019 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.