Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a new term to a product attribute and set it in the product in Woocommerce

My custom taxonomy (WooCommerce attribute) already exists and I am using the following to add a new term to the taxonomy and associate it with my WooCommerce product:

wp_set_object_terms($product_id, array($omega_jahr), 'pa_years-of-construction');

When I use the following to call the 'pa_years_of_construction' for my product, I can see that the new terms have been saved:

$v = array_values( wc_get_product_terms( $product->id, 'pa_years-of-construction', array( 'fields' => 'names' ) ) );

However, when I check my product attributes in the backed and frontend of my website, the 'pa_years_of_construction' attribution isn't showing up.

What am I missing here?

Thanks in advance for any help!

like image 859
Richard Tinkler Avatar asked Dec 10 '18 11:12

Richard Tinkler


People also ask

How to manage products by attributes&variations for WooCommerce extension?

WooCommerce offers a default product filter to help customers sort products by attribute terms. However, if you want to display attribute values in the Shop page, you can manage it with the Products By Attributes & Variations for WooCommerce extension. This will help customers choose a desired variation from the Shop page itself.

How to add taxonomy terms to a product in WooCommerce?

Adding or attaching an attribute to a product in Woocommerce involves two steps: First step is to use the wp_set_object_terms function to add a taxonomy term value to your product object. And the second step is to use the update_post_meta function to update the _product_attributes meta property of the particular product.

How do I use data types in WooCommerce?

There are two uses of this data type that are relevant for WooCommerce: First is via WooCommerce widgets. “Filter Products by Attribute” allows you to select a specific attribute. If you add this widget to your sidebar, customers can filter products in your store based on the attribute. Second is via variable products.

What is the difference between custom and variable products in WooCommerce?

Custom attributes can be used only by that specific product. Variable products are a type of products in WooCommerce that will help you create different variations of the same product. You will be able to control the stock, price, image, etc., of a variation independently of other variations.


1 Answers

Product attributes are a complicated custom taxonomy that needs much more than a simple line of code...

The following code will handle all cases for a pre-existing product attribute:

$taxonomy = 'pa_years-of-construction'; // The taxonomy

$term_name = '2009'; // The term "NAME"
$term_slug = sanitize_title($term_name); // The term "slug"

// Check if the term exist and if not it create it (and get the term ID).
if( ! term_exists( $term_name, $taxonomy ) ){
    $term_data = wp_insert_term( $term_name, $taxonomy );
    $term_id   = $term_data['term_id'];
} else {
    $term_id   = get_term_by( 'name', $term_name, $taxonomy )->term_id;
}

// get an instance of the WC_Product Object
$product = wc_get_product( $product_id );

$attributes = (array) $product->get_attributes();

// 1. If the product attribute is set for the product
if( array_key_exists( $taxonomy, $attributes ) ) {
    foreach( $attributes as $key => $attribute ){
        if( $key == $taxonomy ){
            $options = (array) $attribute->get_options();
            $options[] = $term_id;
            $attribute->set_options($options);
            $attributes[$key] = $attribute;
            break;
        }
    }
    $product->set_attributes( $attributes );
}
// 2. The product attribute is not set for the product
else {
    $attribute = new WC_Product_Attribute();

    $attribute->set_id( sizeof( $attributes) + 1 );
    $attribute->set_name( $taxonomy );
    $attribute->set_options( array( $term_id ) );
    $attribute->set_position( sizeof( $attributes) + 1 );
    $attribute->set_visible( true );
    $attribute->set_variation( false );
    $attributes[] = $attribute;

    $product->set_attributes( $attributes );
}

$product->save();

// Append the new term in the product
if( ! has_term( $term_name, $taxonomy, $product_id ))
    wp_set_object_terms($product_id, $term_slug, $taxonomy, true );

Tested and works.

like image 167
LoicTheAztec Avatar answered Oct 29 '22 14:10

LoicTheAztec