How do I change button text from “Choose an option” in Woocommerce?

后端 未结 8 1468
你的背包
你的背包 2021-02-08 16:28

How would I go about changing this PHP code to to change Choose an option based on the id of the select element in the Woocommerce plugin for WordPress? I believe I have found t

8条回答
  •  醉酒成梦
    2021-02-08 16:50

    This is a perfect use case for a custom filter! I am first going to describe a method that is not the quickest way, but it is probably the cleanest and easiest to understand for anyone else who might have to read your code. I will also describe a 'dirtier' way that should do the job if you are in a time crunch.

    Quick Way:

    The place to find where this is displayed is in the file:

    /wp-content/plugins/woocommerce/templates/single-product/add-to-cart/variable.php
    

    Around line 27, depending on your version of WooCommerce, you will see something like this line:

    
    

    The __() function is running the first parameter through WordPress's translation system using the 'woocommerce' text domain. It is best to preserve the possibility for translation, so we will want to change this text before we send it through the translation function.

    This line of code happens during a loop that outputs all of the product variation attributes. This allows us to easily see which attribute is being output by looking at the $name variable.

    We will need to make a function that takes in the $name variable and outputs a string based on it. It would look something like this:

    function get_text_for_select_based_on_attribute($attribute) {
    
    // Find the name of the attribute for the slug we passed in to the function
    $attribute_name = wc_attribute_label($attribute);
    
    // Create a string for our select
    $select_text = 'Select a ' . $attribute_name;
    
    // Send the $select_text variable back to our calling function
    return $select_text;
    }
    

    Now, before the code on line 27 of variable.php, we can put something like this:

    
    

    Then, simply swap out 'Choose an option' with your $select_text variable:

    
    

    Don't forget to do this all in a template override or your customization will be lost on the next update!

    http://docs.woothemes.com/document/template-structure/

    Cleaner Way:

    A better and more extensible way of doing this is to add a custom filter to pass this through. It's a few extra steps, but allows you to easily add further custom logic if you want to override the functionality on a case-by-case basis depending on your product.

    First, make a custom filter with a semantically-meaningful name, and put it somewhere in your functions.php file for the theme:

    add_filter('variable_product_select_text', 'get_text_for_select_based_on_attribute', 10, 1);
    

    Then, in the variable.php file, instead of just calling the function directly, pass it through your new filter:

    $select_text = apply_filters('variable_product_select_text', $name);
    

    Setting up custom filters for things like this does take a little bit longer, but you get the advantage of maintainability, since you can stack or turn off functions down the road without needing to further modify your existing code.

    Update for WC 2.4

    Version 2.4 of WooCommerce introduces a different way of getting attributes and their associated selects. Since they still have not provided a filter for this, I would recommend overriding the wc_dropdown_variation_attribute_options function using the methods described above. So copy and paste the entire function into your theme's functions.php file starting at the declaration, and add a variable for the select text if it's not a color or size:

    //Don't include the if(!function_exists(...) part.
    
    wc_dropdown_variation_attribute_options($args = array()) {
      // Uses the same function as above, or optionally a custom filter
      $select_text = get_text_for_select_based_on_attribute($args['attribute']);
    
      wc_dropdown_variation_attribute_options( $args = array() ) {
        $args = wp_parse_args( $args, array(
            'options'          => false,
            'attribute'        => false,
            'product'          => false,
            'selected'         => false,
            'name'             => '',
            'id'               => '',
            'class'            => '',
            'show_option_none' => __( $select_text, 'woocommerce' ),
            'show_option_color' => __( 'Choose a color', 'woocommerce' ),
            'show_option_size' => __( 'Choose a size', 'woocommerce' )
        ) );
    // Put the rest of the function here
    

提交回复
热议问题