dongtuan8547 2018-10-22 10:16
浏览 89
已采纳

WooCommerce PHP在Dropdown中显示属性名称

I want to show the name of an attribute at the drop-down-menu when no variation is selected. This is the code:

function wc_dropdown_variation_attribute_options( $args = array() ) {
    $args = wp_parse_args( apply_filters( 'woocommerce_dropdown_variation_attribute_options_args', $args ), array(
        'options'          => false,
        'attribute'        => false,
        'product'          => false,
        'selected'         => false,
        'name'             => '',
        'id'               => '',
        'class'            => '',
        'show_option_none' => __( 'Choose:', 'woocommerce' ),
    ) );

It should be: "Choose: attribute-name" e.g. Color or Size etc.

Can anyone help me?

  • 写回答

1条回答 默认 最新

  • dongsutao8921 2018-10-22 11:18
    关注

    modifying the core function directly is not recommended at all because you are going to lose any modification that you are going to do when the plugin is updated however with WordPress you can alter the function args with filter so here is how you can achieve your target goal.

    add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'change_default_choose_text' );
    
    function change_default_choose_text( $args ) {
    
        $term                     = wc_attribute_label( $args['attribute'] ); //Get Attribute label
        $args['show_option_none'] = __( 'Choose ' . $term . '  ', 'woocommerce' ); //Modify the Default option value 
        return $args;
    
    }
    

    Output:

    enter image description here

    Just put the code above in your functions.php

    For More informations about WordPress Filter you can check the below Reference :

    WordPress Codex add_filter

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?