Đoạn code hiển thị phương thức vận chuyển dựa trên phương thức thanh toán:
+ nếu free ship => bắt buộc thanh toán chuyển khoản 100% đơn hàng
+ nếu ship qua GHTK => chọn COD (bỏ phương thức chuyển khoản)
* Đoạn code chi tiết (thêm vào function.php):
//hide payment method based on shipping method selected
add_filter( ‘woocommerce_available_payment_gateways’, ‘payment_gateways_based_on_chosen_shipping_method’ );
function payment_gateways_based_on_chosen_shipping_method( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
// Get chosen shipping methods
$chosen_shipping_methods = (array) WC()->session->get( ‘chosen_shipping_methods’ );
if ( in_array( ‘free_shipping:6’, $chosen_shipping_methods ) )
{
unset( $available_gateways[‘nganluong’] );
unset( $available_gateways[‘cod’] );
}
elseif ( in_array( ‘ghtk_shipping_methodroad’, $chosen_shipping_methods ) )
{
unset( $available_gateways[‘bacs’] );
}
elseif ( in_array( ‘ghtk_shipping_methodfly’, $chosen_shipping_methods ) )
{
unset( $available_gateways[‘bacs’] );
}
return $available_gateways;
}