WooCommerce的钩子在update_checkout或update_order_review上运行

Jav*_*rks 3 wordpress woocommerce

因此,结帐页面具有货到付款和直接银行转账付款方式。目标是不提供任何运输方式,或者使自由运输成为payment_method检查COD 无线电时唯一可用的方式。为此,我需要取消现有的jne_shipping送货方式。

我在payment_method无线电更改事件中添加了一个回调:

$('input[name=payment_method]').change(function() {
  // request update_checkout to domain.com/checkout/?wc-ajax=update_order_review
  $('body').trigger('update_checkout');
});
Run Code Online (Sandbox Code Playgroud)

和PHP中的钩子:

add_filter( 'woocommerce_available_shipping_methods', 'freeOnCOD', 10, 1 );
function freeOnCOD($available_methods)
{
    if ( isset( $_POST['payment_method'] ) && $_POST['payment_method'] === 'cod' ) {
        unset( $available_methods['jne_shipping'] );
    }

    return $available_methods;
}
Run Code Online (Sandbox Code Playgroud)

但是此过滤器挂钩甚至无法运行。我也尝试过,woocommerce_package_rates但仍然没有效果。

当然,我也检查了WooCommerce的hooks文档,但无法确定在update_checkout或上运行的正确钩子是什么。update_order_review

任何帮助表示赞赏。

Ale*_*tic 5

触发的动作是woocommerce_checkout_update_order_review

您可以像这样运行自定义逻辑:

function name_of_your_function( $posted_data) {

    global $woocommerce;

    // Parsing posted data on checkout
    $post = array();
    $vars = explode('&', $posted_data);
    foreach ($vars as $k => $value){
        $v = explode('=', urldecode($value));
        $post[$v[0]] = $v[1];
    }

    // Here we collect chosen payment method
    $payment_method = $post['payment_method'];

    // Run custom code for each specific payment option selected
    if ($payment_method == "paypal") {
        // Your code goes here
    }

    elseif ($payment_method == "bacs") {
        // Your code goes here
    }

    elseif ($payment_method == "stripe") {
        // Your code goes here
    }
}

add_action('woocommerce_checkout_update_order_review', 'name_of_your_function');
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助!

  • 请注意,不要杀死自己重新发明东西,您可以使用“parse_str($posted_data, $posted_data)”将查询字符串转换为数组 (4认同)