0

I installed WooCommerce and have 3 variable products, however I'd like to add the variation "language". I don't want to add as meta information for the products (in the gui), but I would like to use a hook, so I can say that every variable product has a language variation - english and german. I tried using the filter woocommerce_display_product_attributes but it did not work:

add_filter('woocommerce_display_product_attributes', function($atts, $product) {
  $product_attributes = $product->get_attributes();
  $languages = new WC_Product_Attribute();
  $languages->set_id(0);
  $languages->set_name('language');
  $languages->set_options(['english','german']);
  $languages->set_position(0);
  $languages->set_visible(1);
  $languages->set_variation(1);
  return [
    $product_attributes,
    $languages
  ];
}, 10, 2);

I also tried this code, which showed a dropdown without options, but the info was displayed under "additional info":

add_filter('woocommerce_product_get_attributes', function($atts, $product) {
  $languages = new WC_Product_Attribute();
  $languages->set_id(0);
  $languages->set_name('language');
  $languages->set_options(['english','german']);
  $languages->set_position(0);
  $languages->set_visible(1);
  $languages->set_variation(1);

  $atts['languages'] = $languages;

  return $atts;
}, 10, 2);

Can someone help me out with this? I would be really grateful!

1
+200

You are actually in the right direction but just mixed up the return value. Here is what I have tried in a 5.3.2 copy and works.

Because the $atts is supposed for override. After override, you need to put it back. So, after generate the new product attribute, need to merge the language info into the $atts and throw back to the function which takes it to generate the template.

add_filter('woocommerce_display_product_attributes', function($atts, $product) {

  $languages = new WC_Product_Attribute();
  $languages->set_id(0);
  $languages->set_name('language');
  $languages->set_options(['english','german']);
  $languages->set_position(0);
  $languages->set_visible(1);
  $languages->set_variation(1);

  $lang_str = implode( ',', $languages->get_options()); // require a string for the result

  // the name here is default convention, not compulsory, the convention is eg. attribute_something
  $atts['attribute_languages'] = array(
    'label' => $languages->get_name(),
    'value' => $lang_str
  );

  return $atts;
}, 10, 2);

Explanations to the variable. $atts contains the product attributes of the $product saved in the GUI already. It is also in the form ready for output. So, you don't have to get the attributes again unless you want to do some manipulations to the original information for other purposes.

Edited 2020/4/1


After communicating with the asker, I think the questions could be refined as:

How to dynamically add options for visitor to create product variations?

AFAIK, the asker would like to do the following:

  1. without using the GUI to create and store attribute but create it dynamically
  2. display the dynamically created attributes as options

Here are unanswered questions:

After selecting the options

  1. what to do with the cart and product?
  2. what is the interaction between the option to the cart or the visitor?

The above method is used the same things to create the options for display in "Additional Information" as attributes.

The following method is for creating section options to visitor above the add to shopping cart button.

// add product attributes as option as an product in the cart option
add_action( 'woocommerce_before_add_to_cart_button', 'q362868_add_attributes_as_options');
function q362868_add_attributes_as_options() {
    global $product;

    // create attribute dynamically
    $languages = new WC_Product_Attribute();
    $languages->set_id(0);
    $languages->set_name('language');
    $languages->set_options(['english','german']);
    $languages->set_position(0);
    $languages->set_visible(1);
    $languages->set_variation(1);

    // options and template for visitor to choose
    $options = $languages->get_options();
    ?>
    <div class="q362868-product-wrapper" id="q362868-product-options" style="padding: 1em 0">
        <div class="fieldset">
            <!-- for javascript to work with -->
            <label for="q362868-option-<?php echo $languages->get_id()?>"><?php echo $languages->get_name() . ': '; ?></label>
            <select name="q362868-option-<?php echo $languages->get_id()?>" id="q362868-option-selector">
                    <option value=""><?php echo esc_html__('-- Options --', 'q362868') ?>
                    </option>
                    <?php foreach($options as $key => $option): ?>   
                        <option value="<?php echo $key; ?>"><?php echo htmlspecialchars($option) ?></option>   

                    <?php endforeach; ?>          
            </select>    
        </div>
    </div>
    <?php

    // Here are some unanswered questions from the developer:
    // what to do after displaying the options? Need to consideer the following points as well
    // eg. 1 after selecting, press Add to Cart, pass to Cart
    // eg. 2 changing price or anything is needed
    // other works like javascript is required to interact with visitor
    // it depends on the business logic, change price and so on
}
?><?php

// After visitor -> selection -> add to cart
add_filter( 'woocommerce_add_to_cart_validation', 'q362868_to_cart_validation', 10, 2 );
function q362868_to_cart_validation( $passed, $product_id ) {
    // validation process
    return $passed;
}

Additional notes about the class WC_Product_Attribute. I think it is a helper class to for interact the attribute with such as saving/reading/displaying meta data in admin mainly with the taxonomy structure of the WordPress. Because product attribute is built on WP Taxonomy system. So this helper abstracted the structure for the ease of handling attribute data.

| improve this answer | |
  • Hi, I tested the code, but am not sure if it works. When I visit the product site, there is no dropdown where I can select the languages. – Кристиян Кацаров Mar 31 at 10:45
  • This filter woocommerce_display_product_attributes is for output in the frontend. I test it in the basic storefront theme. It appears in the Additional Information column. It is not for creating any options. Because it did not save to the database. This effect is only in frontend. Is this what you prefer? – simongcc Mar 31 at 10:49
  • @КристиянКацаров Or you want to create an options to visitors to choose a language? After choose the language, what will happen? What is it link to after the language selection? – simongcc Mar 31 at 10:56
  • am sorry if you understood me wrong, what I want to achieve is to have dynamic product variations for woocommerce products without storing them in the database. The example in my question is with a language, but it can by any variation. This variation and the information from it should be used later for the invoice itself as well, hopefully it is possible. – Кристиян Кацаров Mar 31 at 10:58
  • When I have a variable product, each variation creates a dropdown in the product page, so people can choose it, but only if defined in the admin back-end. I want to define such in my functions.php – Кристиян Кацаров Mar 31 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.