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:
- without using the GUI to create and store attribute but create it dynamically
- display the dynamically created attributes as options
Here are unanswered questions:
After selecting the options
- what to do with the cart and product?
- 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.