pronamic / pronamic-payment-gateways-fees-for-woocommerce
此WordPress插件为所有WooCommerce网关添加设置,以添加固定和/或可变(百分比)费用。
v1.0.2
2024-03-14 11:38 UTC
Requires
- php: >=8.0
- automattic/jetpack-autoloader: ^3.0
- pronamic/pronamic-wp-updater: ^1.0
- pronamic/wp-number: ^1.3
Requires (Dev)
README
Pronamic Payment Gateways Fees for WooCommerce
此WordPress插件为所有WooCommerce网关添加设置,以添加固定和/或可变(百分比)费用。
简介
此WordPress插件为所有WooCommerce网关添加设置,以添加固定和/或可变(百分比)费用。
安装
composer require pronamic/pronamic-payment-gateways-fees-for-woocommerce
\Pronamic\WooCommercePaymentGatewaysFees\Plugin::instance()->setup();
截图
流程
WooCommerce建议使用woocommerce_cart_calculate_fees
钩子来添加费用
我们建议使用
woocommere_cart_calculate_fees
钩子来添加费用。
此钩子在调用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 ); }
在创建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(); }
这里可以看到,最终总计是在计算费用总计之后计算的。这意味着在woocommerce_cart_calculate_fees
钩子中,$cart->get_total( '' )
的结果始终是0
。
这很不方便,因为支付网关费用通常基于应付总额。这就是为什么我们挂钩到woocommerce_after_calculate_totals
钩子,并重新计算总计。这种额外的计算看似重复,但似乎是最容易可靠地获取购物车总计的方法。