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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?