【问题标题】:Sell in only some cities woocommerce仅在某些城市销售 woocommerce
【发布时间】:2016-10-12 14:46:04
【问题描述】:


我的帐户页面中, 我想使用下面类似的代码在特定的“城市”或“邮政编码”进行销售,下面的代码用于在特定的“州”进行销售:

/**
 * Sell only in Alger & Zeralda
 */

    function wc_sell_only_states( $states ) {
        $states['DZ'] = array(
            'ALG' => __( 'Alger', 'woocommerce' ),
            'ZLD' => __( 'Zeralda', 'woocommerce' ),
        );
        return $states;
    }
    add_filter( 'woocommerce_states', 'wc_sell_only_states' );

我应该使用或修改什么?

我试过了,但它显示“错误代码 500”:

// define the woocommerce_countries_base_city callback 
function filter_woocommerce_countries_base_city( $var ) { 
    // make filter magic happen here... 
    $var['DZ'] = array(
        'ALG' => __( 'Alger', 'woocommerce' ),
        'ZLD' => __( 'Zeralda', 'woocommerce' ),
    );
    return $var; 
}; 

// add the filter 
add_filter( 'woocommerce_countries_base_city', 'filter_woocommerce_countries_base_city', 10, 1 );

谢谢你!

【问题讨论】:

标签: wordpress woocommerce registration


【解决方案1】:

试试这对我有用!

            add_filter( 'woocommerce_form_field_args', 'custom_form_field_args', 10, 3 );
            function custom_form_field_args( $args, $key, $value ) { 
                if ( $args['id'] == 'billing_city' ) {
                 $args = array(
                        'label' => __( 'Town / City', 'woocommerce' ),
                        'required' => TRUE,
                        'clear' => TRUE,
                        'type' => 'select',
                        'options' =>  array(
                            '' => ('Select City' ),
                    'Bangalore' => ('Bangalore' ),
                    'Mysore' => ('Mysore' )
                    ),
                        'class' => array( 'update_totals_on_change' )
                    );
                } // elseif … and go on 

                return $args;
            };

【讨论】:

  • 也许您可以解释一下代码在做什么以及它是如何解决问题的?
【解决方案2】:

有一个更简单的解决方案。该方法还取决于下面列出的一些假设:

假设:如果您希望将 City 字段设置为只有一个城市,那么您可以使用 jQuery。使用 jQuery,您将设置字段的值并禁用字段以避免更改 City 字段值。

一个粗略的例子是:

jQuery('#billing_city').val('USA').attr('disabled','disabled');

您只需添加此行,以便在结帐页面上执行。

【讨论】:

    猜你喜欢
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多