【发布时间】:2017-11-09 11:07:01
【问题描述】:
我有一个 Woocommerce 实例,其中有 3 种不同的付款方式和 9 组不同的运费 - 3 个区域,每个区域有 3 种不同的费率。
每个区域中的 1 个费率应该只允许显示 1 个支付网关,但到目前为止,我只能让网关为每种运输方式隐藏或不显示。
其中 2 个运费是不同的表格费率,所以我希望可以在函数中使用 'table_rate:##' 来隐藏运费。
到目前为止我尝试过的代码如下。如果选择的运费是 id 为 54 的表格运费,则仅用于删除 winbnk 运费选项,但无论选择哪种运输方式,它都会删除网关。
我犯了一个小学生错误或对送货方式功能有误解。
add_filter('woocommerce_available_payment_gateways','remove_payment_gateway_by_shipping_option');
function remove_payment_gateway_by_shipping_option($available_gateways){
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if($chosen_shipping = 'table_rate:54'){
unset( $available_gateways['winbnk'] );
}
return $available_gateways;
}
以下解决方案对我有用,希望对其他人有用。
add_filter( 'woocommerce_available_payment_gateways', 'shipping_disables_payment_gateway' );
function shipping_disables_payment_gateway( $available_gateways ) {
global $woocommerce;
$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping = $chosen_methods[0];
if ( isset( $available_gateways['winbnk'] ) && 0 === strpos( $chosen_shipping, 'table_rate:54' ) ) {
unset( $available_gateways['winbnk'] );
}
return $available_gateways;
}
【问题讨论】:
标签: php wordpress woocommerce