0

I'm trying to hide prices until the customer logs in to our website. To meet this goal, I'm willing to use customer sessions and if session is true, my product detail page shows the price. Or not, just show a label written as 'login to see price'.

When I ran a Magento 1 website, it was much easier to implement this module but it has been tough to me with the same project under a different development environment.

My Website is now:

  1. Use customizable theme from other template company (the vendor name is Alothemes)
  2. Already overwrote default .phtml file that has price information. I mean some snippet code should be inserted to this file.
  3. I created a own block (my company name is Kbethos) functioning that it brings customer session exists or not
  4. Report says:

    a:4:{i:0;s:37:"Object DOMDocument should be created.";i:1;s:11753:"#0 /home/staging/public_html/vendor/magento/framework/View/Element/UiComponent/Config/Reader.php(95): Magento\Framework\View\Element\UiComponent\Config\DomMerger->getDom()

  5. Log says:

    [2017-10-18 18:37:18] main.CRITICAL: Invalid block type: Kbethos\Session\Block [] []

Could you have a look my code?

TEMPLATE CODE

app\design\frontend\Alothemes\default\Magento_Catalog\layout\default.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="before.body.end">
            <block class="Kbethos\Session\Block\Session" before="-" name="my.custome.block" cacheable="false" template="product\price\amount\default.phtml"/>
        </referenceContainer>
    </body>
</page>

app\design\frontend\Alothemes\default\Magento_Catalog\templates\product\price\amount\default.html

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

// @codingStandardsIgnoreFile

?>

<?php /** @var \Magento\Framework\Pricing\Render\Amount $block */ ?>

<!-- USE CUSTOM BLOCK -->
<?php $customerSession = $block->getCustomerSession(); ?>
<!-- IF SESSION EXISTST -->
<?php if(custommerSession) : ?>
    <span class="price-container <?php /* @escapeNotVerified */ echo $block->getAdjustmentCssClasses() ?>"
        <?php echo $block->getSchema() ? ' itemprop="offers" itemscope itemtype="http://schema.org/Offer"' : '' ?>>
        <?php if ($block->getDisplayLabel()): ?>
            <span class="price-label"><?php /* @escapeNotVerified */ echo $block->getDisplayLabel(); ?></span>
        <?php endif; ?>
        <span <?php if ($block->getPriceId()): ?> id="<?php /* @escapeNotVerified */ echo $block->getPriceId() ?>"<?php endif;?>
            <?php echo($block->getPriceDisplayLabel()) ? 'data-label="' . $block->getPriceDisplayLabel() . $block->getPriceDisplayInclExclTaxes() . '"' : '' ?>
            data-price-amount="<?php /* @escapeNotVerified */ echo $block->getDisplayValue(); ?>"
            data-price-type="<?php /* @escapeNotVerified */ echo $block->getPriceType(); ?>"
            class="price-wrapper <?php /* @escapeNotVerified */ echo $block->getPriceWrapperCss(); ?>"
        <?php echo $block->getSchema() ? ' itemprop="price"' : '' ?>>
        <?php /* @escapeNotVerified */ echo $block->formatCurrency($block->getDisplayValue(), (bool)$block->getIncludeContainer()) ?>
    </span>

    <?php if ($block->hasAdjustmentsHtml()): ?>
            <?php echo $block->getAdjustmentsHtml() ?>
        <?php endif; ?>
        <?php if ($block->getSchema()): ?>
           <meta itemprop="priceCurrency" content="<?php /* @escapeNotVerified */ echo $block->getDisplayCurrencyCode()?>" />
        <?php endif; ?>
    </span>
<!-- SESSION DOESN'T EXIST-->
<?php else : ?>
    <span class="price-container">
        <?php /* @escapeNotVerified */ echo 'Login to see price' ?>"
    </span>
<?php endif;?>

CUSTOM BLOCK CODE

app\code\Kbethos\Session\Block\Session.php

<?php
namespace Kbethos\Session\Block;


/**
 * Customer edit form block
 *
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class Session extends \Magento\Framework\View\Element\Template
{

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * DataDummy constructor.
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
     */
    public function __construct (
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        $this->_isScopePrivate = true;
        $this->_customerSession = $customerSession;
        parent::__construct($context, $data);
    }


    /**
     * @return bool
     */
    public function getCustomerSession()
    {

        /** Bug in magento 2 for get session with full page cache enabled */
        return $this->_customerSession->getCustomer();
    }

}

I have etc/di.xml, etc/module.xml, composer.json, registration.php for this custom block.

How can I handle it?

  • session.php should be named as Session.php – subroutines Oct 18 '17 at 22:29
  • Yes i did. By the way, it doesn't work. Do you find any issue in this code? – 3CE Oct 19 '17 at 13:03
0
<?php
namespace Kbethos\Session\Block;


/**
 * Customer edit form block
 *
 * @SuppressWarnings(PHPMD.DepthOfInheritance)
 */
class Session extends \Magento\Framework\View\Element\Template
{

    /**
     * @var \Magento\Customer\Model\Session
     */
    protected $_customerSession;

    /**
     * DataDummy constructor.
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Customer\Model\Session $customerSession
     * @param array $data
     */
    public function __construct (
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
    ) {
        $this->_isScopePrivate = true;
        $this->_customerSession = $customerSession;
        parent::__construct($context, $data);
    }


    /**
     * @return bool
     */
    public function getCustomerSession()
    {

        /** Bug in magento 2 for get session with full page cache enabled */
        return $this->_customerSession->getCustomer();
    }

}

its working for me

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.