- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2019
09:04 AM
01-22-2019
09:04 AM
Add custom attributes and set final price 0 in an if statement under price/amount/default.phtml
I am trying to do this globally, and not have to work with this in multiple areas that shows the price. This is the current code I have:
<?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 seems to work fine on a Product Detail page, but it isn't on a Product Listing page or anything that shows the Wishlist sidebar. This is the error I am getting:
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
Labels: