Magenerds / GermanLaw

This module provides general settings for german stores
http://doc.magenerds.com/germanlaw/
20 stars 21 forks source link

Tax Details also on catalog list and grid view page #22

Open chequille opened 6 years ago

chequille commented 6 years ago

Hi,

I tried to find myself, but I was not successfull.

Baseprice is showing as well on the catalog list and grid view page. But the tax-details are not shown. There is a div with class="tax-details" in the html code, but nothing in there. Maybe a configuration fault, but I do not know what it could be.

Any ideas? BR Jürgen

chequille commented 6 years ago

Hi, at least I found the point where the probem is. It is in:

public function getTaxText()
{
    /** @var $product \Magento\Catalog\Model\Product */
    $product = $this->_registry->registry('product');
    $taxText = __($this->_scopeConfig->getValue(
        'germanlaw/price/tax_text',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
        $this->_storeManager->getStore()->getId()
    ));

    $taxRate = 0;

    if ($product) {
        // calculate tax rate
        $taxRate = $this->_taxCalculation->getCalculatedRate(
            $product->getTaxClassId(),
            $this->_session->getCustomerId(),
            $this->_storeManager->getStore()
        );
    }

    if ($taxRate <= 0 || !$product) {
        return '';
    } else {
        $taxRate .= '%';
    }

....

THere is no taxrate found because the products is as well not found. Therefore, it returns always ''

BR Jürgen

chequille commented 6 years ago

Addtional information: $product = $this->_registry->registry('product'); This is not returning a product, therefore eveything else cannot work.

This is working on the product page, but not on the catalog list page.

I Hope you will find a solution for this problem.

BR Jürgen

PS: BasePice is approching the product in a different way. There it is working. Maybe it must be done the same way with GermanLaw.

fstareu commented 5 years ago

Hi any updates here for the tax details in catalog list? i know in magento 1.x works with your extension. thank you

Olsov commented 5 years ago

Hello!

I've found a one solution

Here is what you need to do:

in app\code\Magenerds\GermanLaw\Model\Plugin\Afterprice.php

change $renderHtml .= $this->_getAfterPriceHtml();

to $renderHtml .= $this->_getAfterPriceHtml($productInterceptor);

and `

protected function _getAfterPriceHtml() { if (null === $this->_afterPriceHtml) { $afterPriceBlock = $this->_layout->createBlock('Magenerds\GermanLaw\Block\AfterPrice', 'after_price'); $afterPriceBlock->setTemplate('Magenerds_GermanLaw::price/after.phtml'); $this->_afterPriceHtml = $afterPriceBlock->toHtml(); } return $this->_afterPriceHtml; }

`

to

protected function _getAfterPriceHtml(SaleableInterface $product) { if (null === $this->_afterPriceHtml) { $afterPriceBlock = $this->_layout->createBlock('Magenerds\GermanLaw\Block\AfterPrice', 'after_price',['product' => $product]); $afterPriceBlock->setTemplate('Magenerds_GermanLaw::price/after.phtml'); $this->_afterPriceHtml = $afterPriceBlock->toHtml(); } return $this->_afterPriceHtml; }

in app\code\Magenerds\GermanLaw\Block\AfterPrice.php after protected $_urlBuilder; add `/**

change function construct from

public function construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Tax\Api\TaxCalculationInterface $taxCalculation, \Magento\Customer\Model\Session $session array $data = [] ){ $this->_scopeConfig = $context->getScopeConfig(); $this->_registry = $registry; $this->_taxCalculation = $taxCalculation; $this->_session = $session; $this->_storeManager = $context->getStoreManager(); $this->_urlBuilder = $context->getUrlBuilder(); parent::construct($context, $data); }

to

public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Framework\Registry $registry, \Magento\Tax\Api\TaxCalculationInterface $taxCalculation, \Magento\Customer\Model\Session $session, \Magento\Catalog\Model\Product $product, array $data = [] ){ $this->_scopeConfig = $context->getScopeConfig(); $this->_registry = $registry; $this->_taxCalculation = $taxCalculation; $this->_session = $session; $this->_storeManager = $context->getStoreManager(); $this->_urlBuilder = $context->getUrlBuilder(); $this->_product = $product; parent::__construct($context, $data); }

in getTaxText() after $product = $this->_registry->registry('product'); add

$category = $this->_registry->registry('current_category'); if(!$product && $category){ $product = $this->getProduct(); }

and add function in the end of class

/**

  • Retrieve current product
  • @return \Magento\Catalog\Model\Product */ public function getProduct() { return $this->_product; }
fstareu commented 5 years ago

update:

found bug and for fully working you need to:

in app\code\Magenerds\GermanLaw\Model\Plugin\Afterprice.php

make "protected $_afterPriceHtml = null " to "protected $_afterPriceHtml = []"

and just replace this last function with that

protected function _getAfterPriceHtml(SaleableInterface $product) {

    if (!$product) return '';

    if (!array_key_exists($product->getId(), $this->_afterPriceHtml)) {
        $afterPriceBlock = $this->_layout->createBlock('Magenerds\GermanLaw\Block\AfterPrice', 'after_price_'.$product->getId(),['product' => $product]);
        $afterPriceBlock->setTemplate('Magenerds_GermanLaw::price/after.phtml');
        $this->_afterPriceHtml[$product->getId()] = $afterPriceBlock->toHtml();
    }

    return $this->_afterPriceHtml[$product->getId()];
}
chequille commented 5 years ago

It seems the patch from above does not work anymore under Magento 2.3.1.

Does have somebody a solution for this?

chequille commented 5 years ago

By the way, this is the error I am getting in system.log

Argument 5 passed to Magenerds\GermanLaw\Block\AfterPrice::__construct() must be an instance of Magento\Catalog\Model\Product, array given

chequille commented 5 years ago

sorry, my fault, the comment above was nonsense. I worked on a compiled magento, of course this must generate errors. But, on catalog list page, there is no tax value written, so the patch is not working anymore.

I found that if you use always: $product = $this->getProduct(); in getTaxText() it is working on my site.