Adding a product Variation via function — Attributes do not show on Frontend.
-
I recently was tasked with adding 500 products to a WooCommerce store. Went through, added all of the products, and then the client tells me “Oh hey, by the way, we have another set of attributes to add to all of the products.” (I nearly strangled them)
This time around, out of curiosity, I wanted to try to be smart about it, instead of tediously combing through 500 products to add a new section of attributes to their variable products.
I got my set of functions to add the product’s variation, however it’s not wanting to show up on the frontend at all. I’ve tried everything that I can find via google (deleting transients, setting transients, syncing product data via sync_attributes, sync_product, sync, sync_stock_status — and nothing that I have found is working as the posters claim they are ?? So I am guessing that I am doing something wrong. Any help would be immensely appreciated.
The requirements
1. Add a product variation to an existing product, if it does not already have a variation added.
2. Set that variation to allow any attributes to be accepted, rather than linking all possible variations.
3. Set the price on that variation.
4. Sync it so that WooCommerce is able to display it on the frontend.Currently, I have 1 through 3 setup well…but when I go to the frontend, I’m hit with “This product is not currently in stock and is unavailable.”
That error goes away if I hit “Update” in the backend on the product.
Then the dropdowns for attributes appear…but none of the attributes are populated into the dropdowns. This doesn’t go away until I trick the backend into letting me hit the “Save Changes” button under Variations.
My Code
Originally, this started out as -only- setting the prices on variations that already existed…but as I progressed, I transformed it into creating a variation if one did not exist already.All of it works well, except for syncing the data so that it’s usable on the frontend. You may see what WC_Product_Variable functions I had tried using, commented out throughout.
function change_price_by_type( $product_id, $multiply_price_by, $price_type ) { $the_price = get_post_meta( $product_id, '_' . $price_type, true ); $the_price = $multiply_price_by; update_post_meta( $product_id, '_' . $price_type, $the_price ); } function change_price_all_types( $product_id, $multiply_price_by ) { change_price_by_type( $product_id, $multiply_price_by, 'price' ); change_price_by_type( $product_id, $multiply_price_by, 'sale_price' ); change_price_by_type( $product_id, $multiply_price_by, 'regular_price' ); } function change_product_price( $product_id, $multiply_price_by ) { if(has_term('Patches','product_cat',$product_id)){ change_price_all_types( $product_id, $multiply_price_by ); $product = wc_get_product( $product_id ); // Handling variable products if ( $product->is_type( 'variable' ) ) { $variations = $product->get_available_variations(); if(!empty($variations)){ foreach ( $variations as $variation ) { change_price_all_types( $variation['variation_id'], 10 ); } }else{ $my_post = array( 'post_title'=> 'Variation #1 for product #'. $product_id, 'post_name' => 'product-' . $product_id . '-variation-1', 'post_status' => 'publish', 'post_parent' => $product_id,//post is a child post of product post 'post_type' => 'product_variation',//set post type to product_variation 'guid'=>home_url() . '/?product_variation=product-' . $product_id . '-variation-1' ); $attID = wp_insert_post( $my_post ); change_price_all_types($attID,$multiply_price_by); //WC_Product_Variable::sync( $product_id ); //do_action( 'woocommerce_create_product_variation', $attID ); //WC_Product_Variable::variable_product_sync($attID); //WC_Product_Variable::sync_stock_status($product_id); //WC_Product_Variable::sync_attributes($product_id); //WC_Product_Variable::sync( $product_id ); //$transient_name = 'wc_product_children_ids_' . $attID; //delete_transient( $transient_name ); } //WC_Product_Variable::sync_stock_status(639); } } } } function fix_variation_prices(){ $args = array( 'post_type'=>'product', 'posts_per_page'=>-1 ); $query = new WP_Query($args); if($query->have_posts()){ while($query->have_posts()){ $query->the_post(); global $post; $parent_id = $post->ID; $secondary_args = array( 'post_parent'=>$parent_id, 'post_type'=>'product_variation' ); $secondary_query = new WP_Query($secondary_args); if(!$secondary_query->have_posts()){ change_product_price($parent_id, 10); } } } } //add_action('init','fix_variation_prices'); //add_action removed after ran once. Did this so I didn't need to make a button activator.
Thank you for any and all help you’re willing to provide! If you need any other info, lemme know and I’ll do my best.
- The topic ‘Adding a product Variation via function — Attributes do not show on Frontend.’ is closed to new replies.