我在 WooCommerce 中遇到了一个问题,我想知道是否还有其他人也经历过。
我销售的某些产品太脆弱而无法通过 UPS/DHL/FedEx 运送。所以我必须通过托盘运送这些产品。为了解决我的问题,我创建了一个“请求报价”运输方式,允许我的客户选择 BACS 作为付款方式,请求报价作为运输方式并提交他们的订单。在我计算好运费后,我更新订单(将运输方式更改为 N/A)并将状态从“暂停”更改为“待付款”,以便客户可以通过卡付款,如果他们愿意.
这就是我遇到问题的地方。我注意到,如果我取消设置多个支付网关并且选择了这些特定的运输方式,即使我删除了这些支付网关,客户也无法在“order-pay”端点(网站/我的帐户/订单/)中使用这些支付网关订单中的运输方式。
有没有解决的办法?
这是我用来禁用特定运输方式的支付网关的代码。
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
function filter_woocommerce_available_payment_gateways( $available_gateways ) {
$gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
$shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
$disable_gateways = false;
// Check if we need to disable gateways
foreach ( $shipping_methods as $shipping_method ) {
if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
}
// If so, disable the gateways
if ( $disable_gateways ) {
foreach ( $available_gateways as $id => $gateway ) {
if ( in_array( $id, $gateways_to_disable ) ) {
unset( $available_gateways[$id] );
}
}
}
return $available_gateways;
}
更新在咨询了一些开发人员后,他们建议每次需要支付网关时都会运行此代码,并建议我仅在结帐页面上运行此代码段。
他们建议在我的代码中添加以下内容:
if ( is_checkout_pay_page() ) {
// unset Payment Gateways
}
解决了这是我的尝试,它有效。但不确定我们是否可以更好地表达:
add_filter( 'woocommerce_available_payment_gateways', 'filter_woocommerce_available_payment_gateways', 10, 1 );
function filter_woocommerce_available_payment_gateways( $available_gateways ) {
if ( ! ( is_checkout_pay_page() ) ) {
$gateways_to_disable = array( 'cardgatecreditcard', 'cardgategiropay', 'cardgateideal', 'cardgatesofortbanking' );
$shipping_methods = array( 'flat_rate', 'request_shipping_quote' );
$disable_gateways = false;
// Check if we need to disable gateways
foreach ( $shipping_methods as $shipping_method ) {
if ( strpos( WC()->session->get( 'chosen_shipping_methods' )[0], $shipping_method ) !== false ) $disable_gateways = true;
}
// If so, disable the gateways
if ( $disable_gateways ) {
foreach ( $available_gateways as $id => $gateway ) {
if ( in_array( $id, $gateways_to_disable ) ) {
unset( $available_gateways[$id] );
}
}
}
return $available_gateways;
}
else { return $available_gateways;
}
}