【问题标题】:How to fetch tax class rates on shopping cart page in Magento 2?如何在 Magento 2 的购物车页面上获取税率?
【发布时间】:2017-09-09 08:55:24
【问题描述】:

我是 Magento 2 的新手。 我正在购物车页面上自定义表格费率功能。 最终运费计算包括计算燃油费、重量与目的地。

目前,我可以使用代码中的静态燃油税成本来计算最终值。但是,这个值应该可以从后端配置。

因此,我从后端添加了fuelLevy 作为税类,并希望在购物车页面上获取它以计算运费。

表名是“mgic_tax_calculation_rate”。 如何获取这些记录或者有没有其他方法可以做到这一点?

【问题讨论】:

    标签: magento2 shipping tax


    【解决方案1】:

    首先,为这个表创建模型:

    \供应商\模块\模型\MagicTaxCaculationRate.php

    <?php
    /**
     * Copyright © 2015. All rights reserved.
     */
    
    namespace Vendor\Module\Model;
    
    class MgicTaxCaculationRate extends \Magento\Framework\Model\AbstractModel
    {
        /**
         * Constructor
         *
         * @return void
         */
        protected function _construct()
        {
            parent::_construct();
            $this->_init('Vendor\Module\Model\Resource\MgicTaxCaculationRate');
        }
    }
    

    \供应商\模块\模型\资源\MagicTaxCaculationRate.php

    <?php
    /**
     * Copyright © 2015. All rights reserved.
     */
    
    namespace Vendor\Module\Model\Resource;
    
    class MgicTaxCaculationRate extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
    {
        /**
         * Model Initialization
         *
         * @return void
         */
        protected function _construct()
        {
            $this->_init('mgic_tax_calculation_rate', 'mgic_tax_calculation_rate_id');
        }
    }
    

    \供应商\模块\模型\资源\MagicTaxCaculationRate\Collection.php

    <?php
    /**
     * Copyright © 2015. All rights reserved.
     */
    
    namespace Vendor\Module\Model\Resource\MgicTaxCaculationRate;
    
    class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\AbstractCollection
    {
        /**
         * Define resource model
         *
         * @return void
         */
        protected function _construct()
        {
            $this->_init('Vendor\Module\Model\MgicTaxCaculationRate', 'Vendor\Module\Model\Resource\MgicTaxCaculationRate');
        }
    }
    

    然后,在你的街区

    <?php
    namespace Vendor\Module\Block;
    class Example extends \Magento\Framework\View\Element\Template
    {
        protected $_mgicFactory;
        public function _construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Vendor\Module\Model\MgicTaxCaculationRate $mgicFactory
        ){
            $this->_mgicFactory = $mgicFactory;
            parent::_construct($context);
        }
    
        public function _prepareLayout()
        {
            $mgic = $this->_mgicFactory ->create();
            $collection = $mgic->getCollection();
            foreach($collection as $item){
                var_dump($item->getData());
            }
            exit;
        }
    }
    

    顺便问一下,你为什么不把这些字段添加到 system.xml 中?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-06
      • 2022-01-26
      相关资源
      最近更新 更多