添加 COD 费用,除非在 WooCommerce 结账时选择本地取货
Add fee for COD, unless local pickup is chosen on WooCommerce checkout
我目前在 WooCommerce 中有有效代码,并且运行良好。当客户选择“COD”付款方式时,该代码允许我申请额外费用,+ € 1.7
我想了解的是:
当客户选择从我们仓库提货时,是否可以禁用额外费用?
代码如下:
// Add a custom fee based o cart subtotal
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee', 10, 1 );
function custom_handling_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( 'cod' === WC()->session->get('chosen_payment_method') ) {
$fee = 1.7;
$cart->add_fee( 'Extra per pagamento alla consegna', $fee, true );
}
}
// jQuery - Update checkout on methode payment change
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}
cod
是付款方式,local_pickup
是送货方式。
WC()->session->get( 'chosen_payment_method' );
WC()->session->get( 'chosen_shipping_methods' );
所以要回答你的问题,你必须结合使用 2.
所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Chosen payment method
$chosen_payment_method = WC()->session->get( 'chosen_payment_method' );
// Chosen shipping method
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_method = substr( $chosen_shipping_methods[0], 0, strpos( $chosen_shipping_methods[0], ':' ) );
// Compare payment method & shipping method
if ( $chosen_payment_method == 'cod' && $chosen_shipping_method != 'local_pickup' ) {
// Fee
$fee = 1.7;
// Add fee
$cart->add_fee( __( 'Extra per pagamento alla consegna', 'woocommerce'), $fee, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
// jQuery - Update checkout on method payment change
function custom_checkout_jqscript() {
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery( function($){
$( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function(){
$( document.body ).trigger( 'update_checkout' );
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'custom_checkout_jqscript' );
我目前在 WooCommerce 中有有效代码,并且运行良好。当客户选择“COD”付款方式时,该代码允许我申请额外费用,+ € 1.7
我想了解的是:
当客户选择从我们仓库提货时,是否可以禁用额外费用?
代码如下:
// Add a custom fee based o cart subtotal
add_action( 'woocommerce_cart_calculate_fees', 'custom_handling_fee', 10, 1 );
function custom_handling_fee ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( 'cod' === WC()->session->get('chosen_payment_method') ) {
$fee = 1.7;
$cart->add_fee( 'Extra per pagamento alla consegna', $fee, true );
}
}
// jQuery - Update checkout on methode payment change
add_action( 'wp_footer', 'custom_checkout_jqscript' );
function custom_checkout_jqscript() {
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery( function($){
$('form.checkout').on('change', 'input[name="payment_method"]', function(){
$(document.body).trigger('update_checkout');
});
});
</script>
<?php
endif;
}
cod
是付款方式,local_pickup
是送货方式。
WC()->session->get( 'chosen_payment_method' );
WC()->session->get( 'chosen_shipping_methods' );
所以要回答你的问题,你必须结合使用 2.
所以你得到:
function action_woocommerce_cart_calculate_fees( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
// Chosen payment method
$chosen_payment_method = WC()->session->get( 'chosen_payment_method' );
// Chosen shipping method
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
$chosen_shipping_method = substr( $chosen_shipping_methods[0], 0, strpos( $chosen_shipping_methods[0], ':' ) );
// Compare payment method & shipping method
if ( $chosen_payment_method == 'cod' && $chosen_shipping_method != 'local_pickup' ) {
// Fee
$fee = 1.7;
// Add fee
$cart->add_fee( __( 'Extra per pagamento alla consegna', 'woocommerce'), $fee, true );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'action_woocommerce_cart_calculate_fees', 10, 1 );
// jQuery - Update checkout on method payment change
function custom_checkout_jqscript() {
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script type="text/javascript">
jQuery( function($){
$( 'form.checkout' ).on( 'change', 'input[name="payment_method"]', function(){
$( document.body ).trigger( 'update_checkout' );
});
});
</script>
<?php
endif;
}
add_action( 'wp_footer', 'custom_checkout_jqscript' );