检查当前URL/页面是否等于结账页面

时间:2018-08-13 作者:Jan

我试着hide 当我在woocommerce安装的结账页面上时,“货到付款”-支付网关[cop]。当我回显代码时,它会执行两次。一次正确,另一次截断url的结尾:

功能。php

// Disable gateway on checkout page
function payment_gateway_disable_cash_on_delivery_on_checkout( $available_gateways ) {

    global $woocommerce;
    global $wp;

    // Helper
    echo(" || get_page_link: ");
    echo(get_page_link());
    echo(" || wp->request: ");
    echo(home_url( $wp->request));
    echo(" || get_checkout_url: ");
    echo($woocommerce->cart->get_checkout_url());
    echo(" || end. ");

    if ( isset( $available_gateways[\'cop\'] ) && (get_page_link() == $woocommerce->cart->get_checkout_url())) {
        unset(  $available_gateways[\'cop\'] );
    }
    return $available_gateways;
}

add_filter( \'woocommerce_available_payment_gateways\', \'payment_gateway_disable_cash_on_delivery_on_checkout\' );
签出页面上的输出

  || get_page_link: https://mypage.com/en/checkout/ 
  || wp->request: https://mypage.com/en/checkout/ 
  || get_checkout_url: https://mypage.com/en/checkout/ || end.
  || get_page_link: https://mypage.com/en/ 
  || wp->request: https://mypage.com/en/
  || get_checkout_url: https://mypage.com/en/checkout/ || end.
为什么会发生这种情况,或者如何解决?

以下是屏幕截图:

enter image description here

2 个回复
SO网友:Peter HvD

要回答本文标题中的问题,您无需检查页面的URL以查看您是否在签出页面上,因为Woocommerce对此有一个条件-is_checkout()

SO网友:Jan

这是我的解决方案:

我不得不禁用woocommerce签出ajax脚本。这会导致重新加载并添加一些参数,从而更改url。这导致我的对手失败。

/* hide payment method "cash on delivery on cart view (it should only available in POS) */

// Disable gateway on checkout page
function payment_gateway_disable_cash_on_delivery_on_checkout( $available_gateways ) {

    global $woocommerce;

    // we need to dequeue the checkout script, otherwise the ajax call will change our url and the cop option is shown
    wp_dequeue_script( \'wc-checkout\' );

    if ( isset( $available_gateways[\'cop\'] ) && (get_page_link() == $woocommerce->cart->get_checkout_url())) {
        unset(  $available_gateways[\'cop\'] );
    }

    return $available_gateways;
}

add_filter( \'woocommerce_available_payment_gateways\', \'payment_gateway_disable_cash_on_delivery_on_checkout\' );

结束

相关推荐