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

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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制