Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Apptha\Deal\Model\Total;
- use \Magento\Checkout\Model\Session;
- class Customfee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
- {
- protected $quoteValidator = null;
- public function __construct(
- \Magento\Framework\Event\ManagerInterface $eventManager,
- \Magento\Store\Model\StoreManagerInterface $storeManager,
- \Magento\SalesRule\Model\Validator $validator,
- \Magento\Quote\Model\QuoteValidator $quoteValidator,
- \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
- \Apptha\Deal\Helper\Data $dealHelper,
- \Apptha\Customergroup\Helper\Data $customergroupHelper)
- {
- $this->quoteValidator = $quoteValidator;
- $this->setCode('customfee');
- $this->eventManager = $eventManager;
- $this->calculator = $validator;
- $this->storeManager = $storeManager;
- $this->priceCurrency = $priceCurrency;
- $this->dealHelper = $dealHelper;
- $this->customergroupHelper = $customergroupHelper;
- }
- public function collect(
- \Magento\Quote\Model\Quote $quote,
- \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
- \Magento\Quote\Model\Quote\Address\Total $total
- ) {
- if (!count($shippingAssignment->getItems())) {
- return $this;
- }
- parent::collect($quote, $shippingAssignment, $total);
- $label = 'Amount to be paid online';
- $TotalAmount = $total->getSubtotal();
- $buyerCommission = $this->getCommissionAmount($quote);
- $discountAmount = 0;
- if($buyerCommission){
- $discountAmount = "-".$TotalAmount + ($TotalAmount*($buyerCommission/100)); // Set amount to be added
- $appliedCartDiscount = 0;
- }
- $total->setDiscountDescription($label);
- $total->setDiscountAmount($discountAmount);
- $total->setBaseDiscountAmount($discountAmount);
- $total->setSubtotalWithDiscount($discountAmount);
- $total->setBaseSubtotalWithDiscount($discountAmount);
- if(isset($appliedCartDiscount))
- {
- $total->addTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
- $total->addBaseTotalAmount($this->getCode(), $discountAmount - $appliedCartDiscount);
- }
- else
- {
- $total->addTotalAmount($this->getCode(), $discountAmount);
- $total->addBaseTotalAmount($this->getCode(), $discountAmount);
- }
- $total->setCustomfee($discountAmount);
- $total->setBaseCustomfee($discountAmount);
- return $this;
- }
- protected function clearValues(Address\Total $total)
- {
- $total->setTotalAmount('subtotal', 0);
- $total->setBaseTotalAmount('subtotal', 0);
- $total->setTotalAmount('tax', 0);
- $total->setBaseTotalAmount('tax', 0);
- $total->setTotalAmount('discount_tax_compensation', 0);
- $total->setBaseTotalAmount('discount_tax_compensation', 0);
- $total->setTotalAmount('shipping_discount_tax_compensation', 0);
- $total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
- $total->setSubtotalInclTax(0);
- $total->setBaseSubtotalInclTax(0);
- }
- public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
- {
- $TotalAmount = $total->getSubtotal();
- $buyerCommission = $this->getCommissionAmount($quote);
- if(isset($buyerCommission)){
- $discountAmount = $TotalAmount*($buyerCommission/100);
- return [
- 'code' => 'customfee',
- 'title' => 'Custom Fee',
- 'value' => $discountAmount
- ];
- }
- }
- public function getCommissionAmount($quote){
- $groupId = $this->customergroupHelper->getGroupId();
- $groupName = $this->customergroupHelper->getGroupName($groupId);
- $items = $quote->getAllItems();
- foreach($items as $item) {
- $productId = $item->getProductId();
- }
- $buyerCommission = $this->checkCommission();
- if($buyerCommission == 0 && isset($productId)){
- $productData = $this->dealHelper->getProductData($productId);//change code here
- if($groupName == "Merchants"){
- $commissionText = "merchant_commission";
- }else{
- $commissionText = "buyer_commission";
- }
- return $productData->getData($commissionText);
- }else if($buyerCommission != 0){
- return $buyerCommission;
- }
- }
- public function checkCommission(){
- $buyerCommission = 0;
- $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
- $customerSession = $objectManager->get('\Magento\Customer\Model\Session');
- if($customerSession->getCustomer()->getId()){
- $customerDetails = $this->dealHelper->getCustomerData($customerSession->getCustomer()->getId());
- $buyerCommission = $customerDetails->getBuyerCommission();
- }
- return $buyerCommission;
- }
- }
Add Comment
Please, Sign In to add comment