2

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.

enter image description here

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" );
} );

1 Answer 1

1

You are not using the correct hooks to add a custom select field to WooCommerce Attributes terms.

Try the following instead:

// Loop through existing WooCommerce Attribute Taxonomies
foreach ( wc_get_attribute_taxonomies() as $attr_taxonomies ) {
    $attribute_name = $attr_taxonomies->attribute_name; // Get attribute name

    // Add the attribute name to the composite related hooks for each attribute taxonomy
    add_action( "pa_{$attribute_name}_add_form_fields", 'add_wc_attribute_term_custom_field', 20, 1 );
    add_action( "pa_{$attribute_name}_edit_form_fields", 'edit_wc_attribute_term_custom_field', 20, 2 );
    add_action( "created_pa_{$attribute_name}", 'save_wc_attribute_term_custom_field' );
    add_action( "edited_pa_{$attribute_name}", 'save_wc_attribute_term_custom_field' );
}

// Display the select field when adding a new term to WooCommerce taxonomies
function add_wc_attribute_term_custom_field( $taxonomy ) {
    $value = 'no';
    ?>
    <div class="form-field">
        <label for="pr_addon_field"><?php esc_html_e( 'Product Add-on', 'woocommerce' ); ?></label>
        <select name="pr_addon" id="pr_addon_field">
            <option value="no"><?php esc_html_e( 'No', 'woocommerce' ); ?></option>
            <option value="yes"><?php esc_html_e( 'Yes', 'woocommerce' ); ?></option>
        </select>
        <p><?php esc_html_e( 'Field description may go here.', 'woocommerce' ); ?></p>
    </div>
    <?php
}

// Display the select field when editing the term of a WooCommerce taxonomy
function edit_wc_attribute_term_custom_field( $term, $taxonomy ) {
    $value = get_term_meta($term->term_id, 'pr_addon', true);
    $value = $value ?: 'no';
    ?>
    <tr class="form-field term-pr_addon-wrap">
        <th scope="row"><label for="pr_addon_field"><?php esc_html_e( 'Product Add-on', 'woocommerce' ); ?></label></th>
        <td>
            <select name="pr_addon" id="pr_addon_field">
                <option value="no" <?php selected( $value, 'no' ); ?>><?php esc_html_e( 'No', 'woocommerce' ); ?></option>
                <option value="yes" <?php selected( $value, 'yes' ); ?>><?php esc_html_e( 'Yes', 'woocommerce' ); ?></option>
            </select>
            <p><?php esc_html_e( 'Field description may go here.', 'woocommerce' ); ?></p>
        </td>
    </tr><?php
}

// Save the select field value for the term of a WooCommerce taxonomy
function save_wc_attribute_term_custom_field( $term_id ) {
    if ( isset($_POST['pr_addon']) ) {
        update_term_meta( $term_id, 'pr_addon', esc_attr($_POST['pr_addon']) );
    }
}

Code goes in functions.php file of your child theme (or in a plugin).

You will get the following when adding or editing a term from a product attribute:

Editing WC product attribute term

To get the value from that custom field from a defined product attribute term ID, you will use WordPress get_term_meta() function like:

$value = get_term_meta($term_id, 'pr_addon', true);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.