【问题标题】:Show hide payment gateways based on checkout fields in Woocommerce [closed]根据 Woocommerce 中的结帐字段显示隐藏支付网关 [关闭]
【发布时间】:2019-05-03 02:17:40
【问题描述】:

在 Woocommerce 结帐页面中,我试图隐藏一些支付网关,当“运送到其他地址?”时仅保留 “货到付款” 支付方式 (COD)。 被选中。

我已经尝试Conditionally hiding et showing payment gateways 进行了一些更改,但没有成功。我也尝试了不同的答案代码但没有成功。

勾选“Ship to a different address?”时如何只保留货到付款?

感谢任何帮助。

【问题讨论】:

    标签: php ajax wordpress woocommerce checkout


    【解决方案1】:

    以下代码将隐藏除“现金交货时的所有支付网关”(cod),当“发货到另一个地址时?”被检查(和显示运输结帐字段):

    // The jQuery Ajax request
    add_action( 'wp_footer', 'checkout_billing_area_script' );
    function checkout_billing_area_script() {
        // Only checkout page
        if( is_checkout() && ! is_wc_endpoint_url() ):
    
        // Remove "ship_different" custom WC session on load
        if( WC()->session->get('ship_different') ){
            WC()->session->__unset('ship_different');
        }
        // jQuery Ajax code below
        ?>
        <script type="text/javascript">
        jQuery( function($){
            if (typeof wc_checkout_params === 'undefined')
                return false;
    
            var a = '#ship-to-different-address-checkbox', b = '';
    
            // Ajax function
            function triggerSTDA( value ){
                 $.ajax({
                    type: 'POST',
                    url: wc_checkout_params.ajax_url,
                    data: {
                        'action': 'ship_different_address',
                        'ship_different': value,
                    },
                    success: function (result) {
                        $('body').trigger('update_checkout');
                        console.log(result); // For testing (to be removed)
                    }
                });
            }
    
            $(a).on( 'change', function(){
                b = $(this).prop('checked') === true ? 'yes' : 'no';
                triggerSTDA( b );
            });
        });
        </script>
        <?php
        endif;
    }
    
    // The Wordpress Ajax PHP receiver
    add_action( 'wp_ajax_ship_different_address', 'get_ajax_ship_different_address' );
    add_action( 'wp_ajax_nopriv_ship_different_address', 'get_ajax_ship_different_address' );
    function get_ajax_ship_different_address() {
        if ( isset($_POST['ship_different']) ){
            WC()->session->set('ship_different', esc_attr($_POST['ship_different']));
            echo $_POST['ship_different'];
        }
        die();
    }
    
    // Show/hide payment gateways
    add_filter( 'woocommerce_available_payment_gateways', 'conditionally_hide_payment_gateways', 100, 1 );
    function conditionally_hide_payment_gateways( $available_gateways ) {
        if( WC()->session->get('ship_different') == 'yes' ) {
            foreach( $available_gateways as $gateways_id => $gateways ){
                if( $gateways_id !== 'cod' ) {
                    unset($available_gateways[$gateways_id]);
                }
            }
        }
        return $available_gateways;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试和工作。

    【讨论】:

    • 它破坏了 wordpress 中的菜单系统
    • @JonnyKapula 这是因为您的其他自定义或主题自定义或插件。此代码有效且不会破坏任何内容。
    • 很奇怪,因为我使用的是全新安装的 WP 和 Woo,没有任何插件。菜单系统我的意思是在 wp admin 中,菜单是“灰色的”。
    • @JonnyKapula 我已经使用 WP 5.2 WC3.6.3 和 Last storefront 主题版本在全新安装中对其进行了测试,并且效果很好。因此,在您的情况下还有其他一些问题。另外,我谨记您,当您使用任何现有的 stackOverFlow 答案时,请不要忘记对其进行投票(对于作者来说,这是一个非常小的奖励,花费了您一秒钟的点击时间)。
    猜你喜欢
    • 1970-01-01
    • 2016-02-09
    • 2019-04-07
    • 2020-12-12
    • 2021-06-10
    • 2019-07-22
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多