Auto add all product attributes when adding a new product in Woocommerce
Asked Answered
H

2

8

A client of mine requested this weird change based on Wordpress' plugin Woocommerce in order to make things "easier"..

Is it somehow possible to have all product attributes automatically added when a product is created?

Also is it possible to have the "Visible on product page" checkbox automatically disabled if there is no value inputted in the attribute?

Any help will be very much appreciated.


Edit (explanation):

This is what was explained above : This is what was explained above :)

Huh answered 8/2, 2017 at 12:59 Comment(4)
Can you explain a bit further please and show an example?Barahona
pls refer stackoverflow.com/help/how-to-ask before posting your queryBookmark
As you are using variable products, I think that is some kind of complicated to automate auto adding variations on a product creation, as when you create it, you have first to define (select) what kind of product it is … etc… May be what is possible to do is: 1) to create first some variable products (without variations)… 2) then with a custom function, add the variations for each created product just as you want. But this is a real development case…Instead
It doesnt have to be a variable product,it can also be single product,does that still keep the fact that it's going to be a long dev case?Huh
I
9

Here is the way to auto add all existing product variations + terms when creating a new product.

The code (commented):

add_action( 'save_post', 'auto_add_product_attributes', 50, 3 );
function auto_add_product_attributes( $post_id, $post, $update  ) {

    ## --- Checking --- ##

    if ( $post->post_type != 'product') return; // Only products

    // Exit if it's an autosave
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;

    // Exit if it's an update
    if( $update )
        return $post_id;

    // Exit if user is not allowed
    if ( ! current_user_can( 'edit_product', $post_id ) )
        return $post_id;

    ## --- The Settings for your product attributes --- ##

    $visible   = ''; // can be: '' or '1'
    $variation = ''; // can be: '' or '1'

    ## --- The code --- ##

    // Get all existing product attributes
    global $wpdb;
    $attributes = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}woocommerce_attribute_taxonomies" );

    $position   = 0;  // Auto incremented position value starting at '0'
    $data       = array(); // initialising (empty array)

    // Loop through each exiting product attribute
    foreach( $attributes as $attribute ){
        // Get the correct taxonomy for product attributes
        $taxonomy = 'pa_'.$attribute->attribute_name;
        $attribute_id = $attribute->attribute_id;

        // Get all term Ids values for the current product attribute (array)
        $term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids'));

        // Get an empty instance of the WC_Product_Attribute object
        $product_attribute = new WC_Product_Attribute();

        // Set the related data in the WC_Product_Attribute object
        $product_attribute->set_id( $attribute_id );
        $product_attribute->set_name( $taxonomy );
        $product_attribute->set_options( $term_ids );
        $product_attribute->set_position( $position );
        $product_attribute->set_visible( $visible );
        $product_attribute->set_variation( $variation );

        // Add the product WC_Product_Attribute object in the data array
        $data[$taxonomy] = $product_attribute;

        $position++; // Incrementing position
    }
    // Get an instance of the WC_Product object
    $product = wc_get_product( $post_id );

    // Set the array of WC_Product_Attribute objects in the product
    $product->set_attributes( $data );

    $product->save(); // Save the product
}

Code goes in function.php file of your active child theme (or active theme). tested and works.

Instead answered 23/4, 2018 at 5:40 Comment(4)
I see this selects all global attributes in the attributes tab. But they dont get added to variations tab. Are they supposed to?Voile
@Voile If you set $variation = '1';instead of 0, attributes will be available for variationsInstead
When using this code today, I got a fatal error: Fatal error: Uncaught Error: Object of class WC_Product_Attribute could not be converted to string in /nas/content/live/contribnews/wp-includes/taxonomy.php on line 975Athanasius
@ZachNicodemous I will try to look at that this weekendInstead
F
1

If anybody is using the code snippet from @LoicTheAztec, it might be helpful to set another array item.

// Get all term Ids values for the current product attribute (array)
$term_ids = get_terms(array('taxonomy' => $taxonomy, 'fields' => 'ids', 'hide_empty' => false));

Hope it helps anybody.

Frankfurter answered 9/12, 2018 at 12:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.