pronamic/pronamic-payment-gateways-fees-for-woocommerce

此WordPress插件为所有WooCommerce网关添加设置,以添加固定和/或可变(百分比)费用。

v1.0.2 2024-03-14 11:38 UTC

This package is auto-updated.

Last update: 2024-09-14 12:39:31 UTC


README

Pronamic Payment Gateways Fees for WooCommerce

此WordPress插件为所有WooCommerce网关添加设置,以添加固定和/或可变(百分比)费用。

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

简介

此WordPress插件为所有WooCommerce网关添加设置,以添加固定和/或可变(百分比)费用。

安装

composer require pronamic/pronamic-payment-gateways-fees-for-woocommerce
\Pronamic\WooCommercePaymentGatewaysFees\Plugin::instance()->setup();

截图

Screenshot of the WooCommerce direct bank transfer payment method settings page in the WordPress admin dashboard with the extra fees settings.

流程

WooCommerce建议使用woocommerce_cart_calculate_fees钩子来添加费用

我们建议使用woocommere_cart_calculate_fees钩子来添加费用。

来源: https://github.com/woocommerce/woocommerce/blob/8.1.0/plugins/woocommerce/includes/class-wc-cart-fees.php#L7

此钩子在调用WC()->cart->calculate_totals()函数时被调用,WooCommerce使用WC_Cart_Totals类来计算总计

class-wc-cart.php

/**
 * Calculate totals for the items in the cart.
 *
 * @uses WC_Cart_Totals
 */
public function calculate_totals() {
	$this->reset_totals();

	if ( $this->is_empty() ) {
		$this->session->set_session();
		return;
	}

	do_action( 'woocommerce_before_calculate_totals', $this );

	new WC_Cart_Totals( $this );

	do_action( 'woocommerce_after_calculate_totals', $this );
}

来源: https://github.com/woocommerce/woocommerce/blob/8.1.0/plugins/woocommerce/includes/class-wc-cart.php#L1393-L1411

在创建WC_Cart_Totals实例时,将执行WC_Cart_Totals->calculate()函数

class-wc-cart-totals.php

/**
 * Run all calculation methods on the given items in sequence.
 *
 * @since 3.2.0
 */
protected function calculate() {
	$this->calculate_item_totals();
	$this->calculate_shipping_totals();
	$this->calculate_fee_totals();
	$this->calculate_totals();
}

来源: https://github.com/woocommerce/woocommerce/blob/8.1.0/plugins/woocommerce/includes/class-wc-cart-totals.php#L127-L154

这里可以看到,最终总计是在计算费用总计之后计算的。这意味着在woocommerce_cart_calculate_fees钩子中,$cart->get_total( '' )的结果始终是0

这很不方便,因为支付网关费用通常基于应付总额。这就是为什么我们挂钩到woocommerce_after_calculate_totals钩子,并重新计算总计。这种额外的计算看似重复,但似乎是最容易可靠地获取购物车总计的方法。

链接

Pronamic - Work with us