Created
October 22, 2019 18:30
-
-
Save KimJoyFox/39f0f5a9635e84a5668627699ecfefc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action( 'acf/save_post', 'fox_sync_on_product_save', 99); | |
| function fox_sync_on_product_save( $product_id ) { | |
| $product = wc_get_product( $product_id ); | |
| wp_set_object_terms($product_id, 'variable', 'product_type'); | |
| $event_dates = get_field('event_dates', $product_id); | |
| if (is_array($event_dates)){ | |
| create_product_variation( $product_id, $event_dates ); | |
| } | |
| } | |
| function create_product_variation( $product_id, $event_dates ){ | |
| // Get the Variable product object (parent) | |
| $product = wc_get_product($product_id); | |
| $attribute = 'pa_performance_dates'; | |
| //add in the performance date/time string into the Attributes section for Performance Dates | |
| $termID = array(); | |
| foreach ($event_dates as $e){ | |
| $timeDate = strval(strtotime($e['date'])); | |
| $attributeValues[] = $timeDate; | |
| if (term_exists( $timeDate, $attribute) ){ | |
| $termID[] = get_term_by('name', $timeDate, $attribute)->term_id; | |
| } else { | |
| $newterm = wp_insert_term( $timeDate, $attribute ); | |
| $termID[] = $newterm['term_id']; | |
| } | |
| } | |
| //set the attribute values for this product | |
| wp_set_object_terms( $product_id, $termID, $attribute, true ); | |
| //not sure what this does or why I need it. | |
| $thedata = Array( | |
| 'pa_performance_dates' => Array( | |
| 'name' => $attribute, | |
| 'value' => '', | |
| 'is_visible' => '1', | |
| 'is_variation' => '1', | |
| 'is_taxonomy' => '1' | |
| ) | |
| ); | |
| update_post_meta( $product_id, '_product_attributes', $thedata ); | |
| //SETTING UP THE VARIATIONS | |
| $parent_id = $product_id; | |
| //see what the current variations are available for the product | |
| $currentVariations = $product->get_available_variations(); | |
| $currentVar = array(); | |
| //adding the current variations into a simple array to compare later | |
| foreach ($currentVariations as $cv){ | |
| $currentVar[$cv['variation_id']] = $cv['attributes']['attribute_pa_performance_dates']; | |
| } | |
| foreach ($event_dates as $e){ | |
| //if there are no variations whatsoever | |
| if( empty($currentVariations) ) { | |
| add_variation_wfc($e, $parent_id, $attribute); | |
| } else { | |
| //if there are some variations | |
| $variation_exists = 0; //set default - not existing | |
| foreach ($currentVar as $cv => $val){ | |
| if ($val == strtotime($e['date']) ){ | |
| $variation_exists = $cv; | |
| unset($currentVar[$cv]); | |
| } | |
| } | |
| //if variation exists right now | |
| if ( $variation_exists != 0){ | |
| update_post_meta( $variation_exists, '_stock', $e['number_of_tickets'] ); | |
| } else { | |
| //if variation doesn't exist yet, create it using the function below | |
| add_variation_wfc($e, $parent_id, $attribute); | |
| } | |
| } | |
| } | |
| ///REMOVE VARIATION IF THE EVENT DATE IS REMOVED | |
| if (!empty($currentVar)){ | |
| foreach ($currentVar as $cv => $val){ | |
| $thisTermID = get_term_by('name', $val, $attribute)->term_id; | |
| wp_remove_object_terms( $parent_id, $thisTermID, $attribute, true ); | |
| wp_delete_post($cv); | |
| } | |
| } | |
| } | |
| function add_variation_wfc($e, $parent_id, $attribute){ | |
| $variation = array( | |
| 'post_title' => 'Product #' . $parent_id . ' Variation', | |
| 'post_content' => '', | |
| 'post_status' => 'publish', | |
| 'post_parent' => $parent_id, | |
| 'post_type' => 'product_variation' | |
| ); | |
| // The variation id | |
| $variation_id = wp_insert_post( $variation ); | |
| $variation = new WC_Product_Variation( $variation_id ); | |
| // Regular Price ( you can set other data like sku and sale price here ) | |
| $variation->set_price( 4 ); | |
| $variation->set_regular_price( 4 ); | |
| $variation->set_stock_quantity( $e['number_of_tickets'] ); | |
| $variation->set_manage_stock(true); | |
| $variation->set_attributes( array($attribute => (string)strtotime($e['date']) ) ); | |
| $variation->save(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment