I want to add an option to select a term for a product. For this, I thought it would be a good idea to add a field to the attribute itself to indicate if it should be treated as an option. If the value is 'yes,' I want to display the field on the term.
For the first part I found the code on stackoverflow but I am not able to find the second part.
// Add the dropdown to the product attributes page
function my_edit_wc_attribute_pr_addon_field() {
$id = isset( $_GET['edit'] ) ? absint( $_GET['edit'] ) : 0;
$value = $id ? get_option( "wc_attribute_pr_addon_field-$id" ) : '';
?>
<tr class="form-field">
<th scope="row" valign="top">
<label for="my-pr_addon_field">Product addon</label>
</th>
<td>
<select name="pr_addon_field" id="pr_addon_field">
<option value="no" <?php selected( $value, 'no' ); ?>>No</option>
<option value="yes" <?php selected( $value, 'yes' ); ?>>Yes</option>
</select>
</td>
</tr>
<?php
}
add_action( 'woocommerce_after_add_attribute_fields', 'my_edit_wc_attribute_pr_addon_field' );
add_action( 'woocommerce_after_edit_attribute_fields', 'my_edit_wc_attribute_pr_addon_field' );
function my_save_wc_attribute_pr_addon_field( $id ) {
if ( is_admin() && isset( $_POST['pr_addon_field'] ) ) {
$option = "wc_attribute_pr_addon_field-$id";
update_option( $option, sanitize_text_field( $_POST['pr_addon_field'] ) );
}
}
add_action( 'woocommerce_attribute_added', 'my_save_wc_attribute_pr_addon_field' );
add_action( 'woocommerce_attribute_updated', 'my_save_wc_attribute_pr_addon_field' );
add_action( 'woocommerce_attribute_deleted', function ( $id ) {
delete_option( "wc_attribute_pr_addon_field-$id" );
} );

