Php 仅在结帐时禁用特定发货方式的支付网关
我在WooCommerce遇到了一个问题,我想知道是否还有其他人也经历过 我销售的某些产品太易碎,无法通过UPS/DHL/FedEx发货。所以我必须通过托盘运输这些产品。为了解决我的问题,我创建了一个“请求报价”发货方法,允许我的客户选择BACS作为付款方式,请求报价作为发货方式,并提交他们的订单。在我计算了运输成本后,我更新了订单(将运输方式更改为N/A),并将状态从“暂停”更改为“待付款”,以允许客户通过信用卡付款(如果他们愿意) 这就是我遇到这个问题的地方。我注意到,如果我取消设置了多个支付网关,并且选择了这些特定的发货方式,则即使我从订单中删除了发货方式,客户在“订单付款”端点(网站/my account/orders/)中也无法使用这些支付网关 有办法解决这个问题吗 这是我用来禁用特定运输方式的支付网关的代码Php 仅在结帐时禁用特定发货方式的支付网关,php,wordpress,woocommerce,orders,payment-method,Php,Wordpress,Woocommerce,Orders,Payment Method,我在WooCommerce遇到了一个问题,我想知道是否还有其他人也经历过 我销售的某些产品太易碎,无法通过UPS/DHL/FedEx发货。所以我必须通过托盘运输这些产品。为了解决我的问题,我创建了一个“请求报价”发货方法,允许我的客户选择BACS作为付款方式,请求报价作为发货方式,并提交他们的订单。在我计算了运输成本后,我更新了订单(将运输方式更改为N/A),并将状态从“暂停”更改为“待付款”,以允许客户通过信用卡付款(如果他们愿意) 这就是我遇到这个问题的地方。我注意到,如果我取消设置了多个支
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;
}
}
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( 'paymentgateway1', 'paymentgateway2', 'paymentgateway3' );
$shipping_methods = array( 'shippingmethod1', 'shippingmethod2', 'shippingmethod3' );
$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;
}
}