2017-04-13 49 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的方式嗎?

相關問題