Created
October 20, 2015 05:20
-
-
Save grantambrose/af68ba7fe17f58126128 to your computer and use it in GitHub Desktop.
This file contains 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('save_post', 'myweb_duplicate_product', 10, 3); | |
function myweb_duplicate_product($post_id, $post, $update) { | |
$new_or_existing = ''; | |
// only sync products from child sites, do nothing if its the parent site | |
if(get_current_blog_id() != 1){ | |
// check if the post is new or if it is being updated | |
$myPost = get_post($post_id); | |
if( $myPost->post_modified_gmt == $myPost->post_date_gmt ){ | |
// new product | |
$new_or_existing = 'new'; | |
}else{ | |
//echo 'update'; | |
} | |
} | |
copy_post_to_blog($post_id, 1, $new_or_existing); | |
} | |
function copy_post_to_blog($post_id, $target_blog_id, $new_or_existing) { | |
$posttocopy = get_post($post_id); // get the post to copy | |
$posttocopy = get_object_vars($posttocopy); | |
$childblog_id = get_current_blog_id(); | |
$mymeta = get_post_meta($post_id); | |
// retrieve all the product variations for the product we are copying across to the parent | |
// if there are any | |
$args = array( | |
'post_type' => 'product_variation', | |
'posts_per_page' => -1, | |
'post_parent' => $posttocopy[ID], | |
); | |
$child_product_variations = get_posts( $args ); | |
if ( ! empty( $child_product_variations ) ) { | |
foreach ( $child_product_variations as $child_product_variation ) { | |
$child_product_variation_postmeta = get_post_meta($child_product_variation->ID); | |
// build an array of all post meta. | |
//$child_product_variation_postmeta_all[] = $child_product_variation_postmeta; | |
} | |
//var_dump($child_product_variation_postmeta); | |
} | |
unset($posttocopy[ID]); // empty id field, to tell wordpress that this will be a new post | |
switch_to_blog($target_blog_id); // switch to target blog | |
remove_action('save_post', 'myweb_duplicate_product'); | |
// if this is a new post, create it in the parent network site | |
//echo $product_update; | |
if($new_or_existing == 'new'){ | |
// insert the post,. true means it returns the post ID on complete | |
// so $inserted_post_id will equal the new post id | |
$inserted_post_id = wp_insert_post($posttocopy, true); | |
foreach($mymeta as $key=>$value) { | |
update_post_meta($inserted_post_id,$key,$value[0]); | |
} | |
// now add blog id & post id of the product in the child site that the product came from so we can handle updates to it | |
update_post_meta($inserted_post_id,'_myweb_childblog_id',$childblog_id); | |
update_post_meta($inserted_post_id,'_myweb_childblog_post_id',$post_id); | |
if ( ! empty( $child_product_variations ) ) { | |
// loop through all product variations | |
wp_set_object_terms ($inserted_post_id,'variable','product_type'); | |
foreach ( $child_product_variations as $child_product_variation ) { | |
// then set the parent id of variations to the new parent site product. | |
$child_product_variation->post_parent = $inserted_post_id; | |
//var_dump($child_product_variation); | |
$new_parent_variation_post_id = wp_insert_post($child_product_variation, true); // insert the post | |
update_post_meta($new_parent_variation_post_id->ID,'_myweb_childblog_id',$childblog_id); | |
update_post_meta($new_parent_variation_post_id->ID,'_myweb_childblog_post_id',$post_id); | |
// | |
// THIS NEEDS TO BE DONE BEFORE SWITCHING BLOGS | |
// now get the post meta for the child site variations | |
// | |
// and then update this to the new parent variations | |
foreach($child_product_variation_postmeta as $key=>$value) { | |
update_post_meta($new_parent_variation_post_id,$key,$value[0]); | |
//echo 'key: ' . $key . ' value: ' . $value[0] . '<br />'; | |
} | |
} | |
} | |
} | |
// else if we are updating the post, dont create a new post - just update | |
else{ | |
// find the product to update in the parent site | |
$args = array( | |
'post_type' => 'product', | |
'posts_per_page' => 1, | |
'fields' => 'ids', | |
'meta_query' => array( | |
'relation' => 'AND', | |
array( | |
'key' => '_myweb_childblog_id', | |
'value' => $childblog_id, | |
'compare' => '=', | |
), | |
array( | |
'key' => '_myweb_childblog_post_id', | |
'compare' => '=', | |
'value' => $post_id, | |
), | |
) | |
); | |
// load the post from the parent site we are going to update | |
$producttoupdate = new WP_Query($args); | |
if ($producttoupdate->have_posts()): | |
foreach( $producttoupdate->posts as $id ): | |
// update the parent with new child post details. | |
//print_r($posttocopy); | |
// now set the id of the child site product to the id of the parent | |
$posttocopy[ID] = $id; | |
wp_update_post( $posttocopy); | |
//$parent_post = get_post($id); | |
//$parent_post = $posttocopy; // now we've set the update | |
//wp_update_post( $parent_post);// update the post | |
// update all parent post meta | |
foreach($mymeta as $key=>$value) { | |
update_post_meta($id,$key,$value[0]); | |
} | |
endforeach; | |
endif; | |
} | |
restore_current_blog(); // return to original blog | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment