如何收取Magento 2支付费用

92dk7w1h  于 2023-10-19  发布在  其他
关注(0)|答案(3)|浏览(159)

我想收取额外费用,根据所选择的付款方式。我用下面的模块在Magento 1中做了这件事。
https://github.com/manishiitg/excellence_magento_blog/tree/master/Fee%20Module/app
是否有一个类似的模块Magento 2.

9vw9lbht

9vw9lbht1#

我们可以在Magento 2中使用特定费用模块付款:https://github.com/mrkhoa99/Boolfly_payment_fee
此模块将创建一个带有特定费用的货到付款模块。我们可以按照this guide创建自己的模块。

omtl5h9j

omtl5h9j2#

首先,您需要在此链接中探索如何将费用添加到magento 2中的订单总额的完整过程:https://magento.stackexchange.com/questions/92774/how-to-add-fee-to-order-totals-in-magento2之后:创建di.xml并推送exmaple:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Sales\Model\Order">
        <plugin name="update_payment_fee_order" type="Ibnab\PF\Plugin\UpdateFeeForOrder"/>
    </type>
    <type name="Magento\Paypal\Model\Cart">
        <plugin name="update_paypal_fee_order" type="Ibnab\PF\\Plugin\UpdateFeeForOrder"/>
    </type>
</config>

用于创建插件并注入到afterGetAmounts($cart,$result)和beforeGetAllItems($cart)

<?php
namespace Ibnab\PF\Plugin;
class UpdateFeeForOrder
{
    /**
     * @var \Magento\Quote\Model\QuoteFactory
     */
    protected $quote;
    protected $logger;
    protected $_checkoutSession;
    protected $_registry;
    const AMOUNT_Payment = 'payment_fee';
    const AMOUNT_SUBTOTAL = 'subtotal';
    public function __construct(
        \Magento\Quote\Model\Quote $quote,
        \Psr\Log\LoggerInterface $logger,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Framework\Registry $registry
    ) {
        $this->quote = $quote;
        $this->logger = $logger;
        $this->_checkoutSession = $checkoutSession;
        $this->_registry = $registry;
    }
    /**
     * Get shipping, tax, subtotal and discount amounts all together
     *
     * @return array
     */
    public function afterGetAmounts($cart,$result)
    {
        $total = $result;
        $quote = $this->_checkoutSession->getQuote();
        $paymentMethod = $quote->getPayment()->getMethod();
        $paypalMehodList = ['payflowpro','payflow_link','payflow_advanced','braintree_paypal','paypal_express_bml','payflow_express_bml','payflow_express','paypal_express'];

        if(in_array($paymentMethod,$paypalMehodList)){
        $total[self::AMOUNT_SUBTOTAL] = $total[self::AMOUNT_SUBTOTAL] + $quote->getFeeAmount();

        }

        return  $total;
    }
    /**
     * Get shipping, tax, subtotal and discount amounts all together
     *
     * @return array
     */
    public function beforeGetAllItems($cart)
    {
        $paypalTest = $this->_registry->registry('is_paypal_items')? $this->_registry->registry('is_paypal_items') : 0;
        $quote = $this->_checkoutSession->getQuote();
        $paymentMethod = $quote->getPayment()->getMethod();

        $paypalMehodList = ['payflowpro','payflow_link','payflow_advanced','braintree_paypal','paypal_express_bml','payflow_express_bml','payflow_express','paypal_express'];
        if($paypalTest < 3 && in_array($paymentMethod,$paypalMehodList)){
        if(method_exists($cart , 'addCustomItem' ))
        {
        $cart->addCustomItem(__("Payment Fee"), 1 ,$quote->getFeeAmount());
        $reg = $this->_registry->registry('is_paypal_items');
        $current = $reg + 1 ;
        $this->_registry->unregister('is_paypal_items');
        $this->_registry->register('is_paypal_items', $current);
        }
        }
    }
}

这里的完整修改,我们测试和添加费用贝宝金额和添加费用作为自定义项目要求贝宝,竞争过程中的Magento 2 Paypal Fee (charge) and life cycle

lb3vh1jj

lb3vh1jj3#

您需要通过使用Magento_Checkout/js/action/select-payment-method选择付款方式来重新计算总额

define(
[
    'jquery',
    'ko',
    'Magento_Checkout/js/model/quote',
    'Mageprince_Paymentfee/js/action/checkout/cart/totals'
],
function($, ko ,quote, totals) {
    'use strict';
    var isLoading = ko.observable(false);

    return function (paymentMethod) {
        quote.paymentMethod(paymentMethod);
        totals(isLoading, paymentMethod['method']);
    }
});

这里是我的存储库中的完整源代码,具有很多功能,如添加运费/折扣小计,添加任何数量的付款方式费用,按特定税种计算税款等。

https://github.com/mageprince/magento2-paymentfee

相关问题