Guest User

Untitled

a guest
Mar 11th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
  2. {
  3. protected $quoteValidator = null;
  4.  
  5. public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator, \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency)
  6. {
  7. $this->quoteValidator = $quoteValidator;
  8. $this->_priceCurrency = $priceCurrency;
  9. }
  10. public function collect(
  11. \Magento\Quote\Model\Quote $quote,
  12. \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
  13. \Magento\Quote\Model\Quote\Address\Total $total
  14. ) {
  15. if (!count($shippingAssignment->getItems())) {
  16. return $this;
  17. }
  18. parent::collect($quote, $shippingAssignment, $total);
  19.  
  20. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  21. $checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
  22. $fee = $checkoutSession->getServiceFee();
  23. $grandTotal = $checkoutSession->getAnyBaseSubtotal();
  24. $storeObj=$objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore();
  25. $baseCurrencyCode = $storeObj->getBaseCurrencyCode();
  26. $currentCurrencyCode = $storeObj->getCurrentCurrencyCode();
  27. if($baseCurrencyCode==$currentCurrencyCode){
  28. $total->setTotalAmount('fee', $fee);
  29. $total->setBaseTotalAmount('fee', $fee);
  30. $total->setFee($fee);
  31. $total->setBaseFee($fee);
  32. $total->setSubtotal($grandTotal + $fee);
  33. $total->setBaseSubtotal($grandTotal + $fee);
  34. $total->setGrandTotal(0);
  35. $total->setBaseGrandTotal(0);
  36. }else{
  37. $rate =($fee)?($this->_priceCurrency->convert($fee, $storeObj->getStoreId()) / $fee):0;
  38. $calfee = ($rate)?($fee / $rate):0;
  39. $total->setTotalAmount('fee', $fee);
  40. $total->setBaseTotalAmount('fee', $calfee);
  41. $total->setFee($fee);
  42. $total->setBaseFee($calfee);
  43. $total->setSubtotal($grandTotal + $fee);
  44. $total->setBaseSubtotal($grandTotal + $calfee);
  45. $total->setGrandTotal(0);
  46. $total->setBaseGrandTotal(0);
  47. }
  48. return $this;
  49. }
  50.  
  51. protected function clearValues(Address\Total $total)
  52. {
  53. $total->setTotalAmount('subtotal', 0);
  54. $total->setBaseTotalAmount('subtotal', 0);
  55. $total->setTotalAmount('tax', 0);
  56. $total->setBaseTotalAmount('tax', 0);
  57. $total->setTotalAmount('discount_tax_compensation', 0);
  58. $total->setBaseTotalAmount('discount_tax_compensation', 0);
  59. $total->setTotalAmount('shipping_discount_tax_compensation', 0);
  60. $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
  61. $total->setSubtotalInclTax(0);
  62. $total->setBaseSubtotalInclTax(0);
  63. $total->setGrandTotal(0);
  64. $total->setBaseGrandTotal(0);
  65. }
  66. /**
  67. * @param \Magento\Quote\Model\Quote $quote
  68. * @param Address\Total $total
  69. * @return array|null
  70. */
  71. /**
  72. * Assign subtotal amount and label to address object
  73. *
  74. * @param \Magento\Quote\Model\Quote $quote
  75. * @param Address\Total $total
  76. * @return array
  77. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  78. */
  79. public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
  80. {
  81. $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
  82. $checkoutSession = $objectManager->get('\Magento\Checkout\Model\Session');
  83. return [
  84. 'code' => 'fee',//extra fee you want to add
  85. 'title' => 'Fee',//title of the extra fee you want to add
  86. 'value' => $checkoutSession->getServiceFee()
  87. ];
  88. }
  89.  
  90. /**
  91. * Get Subtotal label
  92. *
  93. * @return \Magento\Framework\Phrase
  94. */
  95. public function getLabel()
  96. {
  97. return __('Fee');
  98. }
  99. }
Add Comment
Please, Sign In to add comment