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