Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
741 views
in Technique[技术] by (71.8m points)

php - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...