Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- include ('../../wp-load.php');
- global $post;
- $args = array (
- 'post_type' => 'shop_coupon',
- 'posts_per_page' => -1
- );
- $custom_query = new WP_Query( $args );
- while ( $custom_query->have_posts() )
- {
- $custom_query->the_post();
- $coupon_data = array (
- 'global_coupon' => 'yes',
- 'global_coupon_unlink' => 'yes',
- 'product_categories' => array(),
- 'exclude_product_categories' => array(),
- 'discount_type' => get_post_meta ( $post->ID, 'discount_type', TRUE ),
- 'coupon_amount' => get_post_meta ( $post->ID, 'coupon_amount', TRUE ),
- 'date_expires' => get_post_meta ( $post->ID, 'date_expires', TRUE ),
- 'individual_use' => get_post_meta ( $post->ID, 'individual_use', TRUE ),
- 'product_ids' => array(),
- 'excluded_product_ids' => array(),
- 'usage_limit' => get_post_meta ( $post->ID, 'usage_limit', TRUE ),
- 'usage_limit_per_user' => get_post_meta ( $post->ID, 'usage_limit_per_user', TRUE ),
- 'limit_usage_to_x_items' => get_post_meta ( $post->ID, 'limit_usage_to_x_items', TRUE ),
- 'free_shipping' => get_post_meta ( $post->ID, 'free_shipping', TRUE ),
- 'exclude_sale_items' => get_post_meta ( $post->ID, 'exclude_sale_items', TRUE ),
- 'minimum_amount' => get_post_meta ( $post->ID, 'minimum_amount', TRUE ),
- 'maximum_amount' => get_post_meta ( $post->ID, 'maximum_amount', TRUE ),
- 'email_restrictions' => array()
- );
- echo "Synchronizing coupon ID " . $post->ID ."<br />";
- woocommerce_coupon_options_save( $post->ID, $coupon_data );
- }
- echo "Completed";
- function woocommerce_coupon_options_save( $coupon_ID, $coupon_data )
- {
- global $post, $WooGC;
- //previous setting
- $previous_option_global_coupon = get_post_meta ( $coupon_ID, 'global_coupon', TRUE );
- $global_coupon = wc_clean( $coupon_data['global_coupon'] );
- update_post_meta ( $coupon_ID, 'global_coupon', $global_coupon );
- //ensure there is a hash for the coupon to estabilish a global syncronisation, when changed to Yes/No
- $coupon_hash = get_post_meta ( $coupon_ID, '_global_coupon_hash', TRUE );
- if ( empty ( $coupon_hash ) )
- {
- $coupon_hash = md5 ( time () . $coupon_ID );
- update_post_meta ( $coupon_ID, '_global_coupon_hash', $coupon_hash );
- }
- //check for unlink
- $global_coupon_unlink = wc_clean( $coupon_data['global_coupon_unlink'] );
- if ( $previous_option_global_coupon == 'yes' && $global_coupon == 'no' )
- {
- global $blog_id;
- $current_site = $blog_id;
- //$coupon->save();
- $sites = $WooGC->functions->get_gc_sites( TRUE, 'global_coupon/save' );
- foreach ( $sites as $site )
- {
- if ( $site->blog_id == $current_site )
- continue;
- switch_to_blog( $site->blog_id );
- $coupon_id = '';
- $args = array(
- 'post_type' => 'shop_coupon',
- 'meta_key' => '_global_coupon_hash',
- 'meta_value' => $coupon_hash
- );
- $founds = new WP_Query($args);
- if ( $founds->found_posts > 0 )
- {
- $founds->the_post();
- $coupon_id = $founds->post->ID;
- }
- if ( ! empty ( $coupon_id ) )
- {
- if ( $global_coupon_unlink == 'yes' )
- wp_delete_post( $coupon_id, TRUE );
- else
- update_post_meta ( $coupon_id, 'global_coupon', 'no' );
- }
- restore_current_blog();
- }
- }
- //syncrnize with other shops
- if ( $global_coupon == 'yes' )
- {
- global $blog_id;
- $current_site = $blog_id;
- $coupon_post_data = get_post ( $coupon_ID );
- $product_categories = isset( $coupon_data['product_categories'] ) ? (array) $coupon_data['product_categories'] : array();
- $exclude_product_categories = isset( $coupon_data['exclude_product_categories'] ) ? (array) $coupon_data['exclude_product_categories'] : array();
- $coupon_props = array(
- 'code' => $post->post_title,
- 'discount_type' => wc_clean( $coupon_data['discount_type'] ),
- 'amount' => wc_format_decimal( $coupon_data['coupon_amount'] ),
- 'date_expires' => wc_clean( $coupon_data['date_expires'] ),
- 'individual_use' => isset( $coupon_data['individual_use'] ),
- 'product_ids' => isset( $coupon_data['product_ids'] ) ? array_filter( array_map( 'intval', (array) $coupon_data['product_ids'] ) ) : array(),
- 'excluded_product_ids' => isset( $coupon_data['exclude_product_ids'] ) ? array_filter( array_map( 'intval', (array) $coupon_data['exclude_product_ids'] ) ) : array(),
- 'usage_limit' => absint( $coupon_data['usage_limit'] ),
- 'usage_limit_per_user' => absint( $coupon_data['usage_limit_per_user'] ),
- 'limit_usage_to_x_items' => absint( $coupon_data['limit_usage_to_x_items'] ),
- 'free_shipping' => isset( $coupon_data['free_shipping'] ),
- 'product_categories' => array_filter( array_map( 'intval', $product_categories ) ),
- 'excluded_product_categories' => array_filter( array_map( 'intval', $exclude_product_categories ) ),
- 'exclude_sale_items' => isset( $coupon_data['exclude_sale_items'] ),
- 'minimum_amount' => wc_format_decimal( $coupon_data['minimum_amount'] ),
- 'maximum_amount' => wc_format_decimal( $coupon_data['maximum_amount'] ),
- 'email_restrictions' => $coupon_data['email_restrictions'],
- );
- $coupon_props = apply_filters( 'woogc/global_coupons/props', $coupon_props, $coupon_ID );
- $sites = $WooGC->functions->get_gc_sites( TRUE, 'global_coupon/save' );
- foreach ( $sites as $site )
- {
- if ( $site->blog_id == $current_site )
- continue;
- switch_to_blog( $site->blog_id );
- $args = array(
- 'post_type' => 'shop_coupon',
- 'meta_key' => '_global_coupon_hash',
- 'meta_value' => $coupon_hash
- );
- $founds = new WP_Query($args);
- if ( $founds->found_posts < 1 )
- {
- //no coupon found, creat it
- $new_coupon_post = (array)$coupon_post_data;
- unset ( $new_coupon_post['ID'] );
- unset ( $new_coupon_post['guid'] );
- // Insert the post into the database
- $coupon_id = wp_insert_post( $new_coupon_post );
- }
- else
- {
- $founds->the_post();
- $coupon_id = $founds->post->ID;
- $new_coupon_post = (array)$coupon_post_data;
- $new_coupon_post['ID'] = $coupon_id;
- // Insert the post into the database
- wp_update_post( $new_coupon_post );
- }
- $coupon = new WC_Coupon( $coupon_id );
- $coupon->set_props( $coupon_props );
- $coupon->save();
- update_post_meta ( $coupon_id, 'global_coupon', 'yes' );
- update_post_meta ( $coupon_id, '_global_coupon_hash', $coupon_hash );
- restore_current_blog();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment