2017-04-13 52 views
1

我是Magento 2的新手。 我在购物车页面上自定义桌面费率功能。 最终运费率计算包括计算燃油征费,重量与目的地。如何在Magento 2的购物车页面上获取税率?

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

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

表名是'mgic_tax_calculation_rate'。 如何获取这些记录还是有任何其他方式来做到这一点?

回答

0

首先,对于该表中创建的模型:

\卖方\模块\模型\ MgicTaxCaculationRate.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'); 
    } 
} 

\卖方\模块\模型\资源\ MgicTaxCaculationRate.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'); 
    } 
} 

\ Vendor \ Module \ Model \ Resource \ MgicTaxCaculationRate \ 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的方式吗?

相关问题