nshelper

WPGlobaCart - sync and create global coupons to other shops in the network

Feb 4th, 2026
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.61 KB | None | 0 0
  1. <?php
  2.  
  3.     include ('../../wp-load.php');
  4.    
  5.     global $post;
  6.    
  7.     $args   =   array (
  8.                         'post_type'         =>      'shop_coupon',
  9.                         'posts_per_page'    =>  -1
  10.                         );
  11.     $custom_query   =   new WP_Query( $args );
  12.    
  13.     while ( $custom_query->have_posts() )
  14.         {
  15.             $custom_query->the_post();
  16.            
  17.             $coupon_data    =   array (
  18.                                         'global_coupon'                 =>  'yes',
  19.                                        
  20.                                         'global_coupon_unlink'          =>  'yes',
  21.                                         'product_categories'            =>  array(),
  22.                                         'exclude_product_categories'    =>  array(),
  23.                                        
  24.                                         'discount_type'                 =>  get_post_meta ( $post->ID, 'discount_type', TRUE ),
  25.                                         'coupon_amount'                 =>  get_post_meta ( $post->ID, 'coupon_amount', TRUE ),
  26.                                         'date_expires'                  =>  get_post_meta ( $post->ID, 'date_expires', TRUE ),
  27.                                         'individual_use'                =>  get_post_meta ( $post->ID, 'individual_use', TRUE ),
  28.                                         'product_ids'                   =>  array(),
  29.                                         'excluded_product_ids'          =>  array(),
  30.                                         'usage_limit'                   =>  get_post_meta ( $post->ID, 'usage_limit', TRUE ),
  31.                                         'usage_limit_per_user'          =>  get_post_meta ( $post->ID, 'usage_limit_per_user', TRUE ),
  32.                                         'limit_usage_to_x_items'        =>  get_post_meta ( $post->ID, 'limit_usage_to_x_items', TRUE ),
  33.                                         'free_shipping'                 =>  get_post_meta ( $post->ID, 'free_shipping', TRUE ),
  34.                                         'exclude_sale_items'            =>  get_post_meta ( $post->ID, 'exclude_sale_items', TRUE ),
  35.                                         'minimum_amount'                =>  get_post_meta ( $post->ID, 'minimum_amount', TRUE ),
  36.                                         'maximum_amount'                =>  get_post_meta ( $post->ID, 'maximum_amount', TRUE ),
  37.                                         'email_restrictions'            =>  array()
  38.                                         );
  39.            
  40.             echo "Synchronizing coupon ID " . $post->ID ."<br />";
  41.            
  42.             woocommerce_coupon_options_save( $post->ID, $coupon_data );
  43.            
  44.         }
  45.    
  46.    
  47.    
  48.     echo "Completed";
  49.    
  50.    
  51.    
  52.     function woocommerce_coupon_options_save( $coupon_ID, $coupon_data )
  53.         {
  54.             global $post, $WooGC;
  55.            
  56.             //previous setting
  57.             $previous_option_global_coupon  =   get_post_meta ( $coupon_ID, 'global_coupon', TRUE );
  58.            
  59.             $global_coupon      =   wc_clean( $coupon_data['global_coupon'] );
  60.            
  61.             update_post_meta ( $coupon_ID, 'global_coupon',  $global_coupon );
  62.            
  63.             //ensure there is a hash for the coupon to estabilish a global syncronisation, when changed to Yes/No
  64.             $coupon_hash    =   get_post_meta ( $coupon_ID, '_global_coupon_hash',  TRUE );
  65.             if ( empty ( $coupon_hash ) )
  66.                 {
  67.                     $coupon_hash    =   md5 ( time () . $coupon_ID );
  68.                     update_post_meta ( $coupon_ID, '_global_coupon_hash',  $coupon_hash );
  69.                 }
  70.            
  71.             //check for unlink
  72.             $global_coupon_unlink      =   wc_clean( $coupon_data['global_coupon_unlink'] );
  73.             if ( $previous_option_global_coupon ==  'yes' &&  $global_coupon    ==  'no' )
  74.                 {
  75.                     global $blog_id;
  76.                    
  77.                     $current_site   =   $blog_id;
  78.                                        
  79.                     //$coupon->save();
  80.                     $sites  =   $WooGC->functions->get_gc_sites( TRUE, 'global_coupon/save' );        
  81.                     foreach ( $sites    as  $site )
  82.                         {
  83.                             if ( $site->blog_id ==  $current_site )
  84.                                 continue;
  85.                            
  86.                             switch_to_blog( $site->blog_id );  
  87.                            
  88.                             $coupon_id  =   '';
  89.                            
  90.                             $args = array(
  91.                                            'post_type'  =>  'shop_coupon',
  92.                                            'meta_key'   => '_global_coupon_hash',
  93.                                            'meta_value' =>  $coupon_hash  
  94.                                         );
  95.                             $founds = new WP_Query($args);
  96.                             if ( $founds->found_posts > 0 )
  97.                                 {
  98.                                     $founds->the_post();
  99.                                     $coupon_id  =   $founds->post->ID;  
  100.                                 }
  101.                            
  102.                             if ( ! empty ( $coupon_id ) )
  103.                                 {
  104.                                     if ( $global_coupon_unlink == 'yes' )
  105.                                         wp_delete_post( $coupon_id, TRUE );
  106.                                         else
  107.                                         update_post_meta ( $coupon_id, 'global_coupon',  'no' );
  108.                                 }
  109.                            
  110.                            
  111.                             restore_current_blog();
  112.                         }  
  113.                    
  114.                 }
  115.                
  116.             //syncrnize with other shops
  117.             if ( $global_coupon    ==  'yes' )
  118.                 {
  119.                     global $blog_id;
  120.                    
  121.                     $current_site   =   $blog_id;
  122.                    
  123.                     $coupon_post_data    =  get_post ( $coupon_ID );
  124.                    
  125.                     $product_categories         = isset( $coupon_data['product_categories'] ) ? (array) $coupon_data['product_categories'] : array();
  126.                     $exclude_product_categories = isset( $coupon_data['exclude_product_categories'] ) ? (array) $coupon_data['exclude_product_categories'] : array();
  127.                    
  128.                     $coupon_props = array(
  129.                                             'code'                        => $post->post_title,
  130.                                             'discount_type'               => wc_clean( $coupon_data['discount_type'] ),
  131.                                             'amount'                      => wc_format_decimal( $coupon_data['coupon_amount'] ),
  132.                                             'date_expires'                => wc_clean( $coupon_data['date_expires'] ),
  133.                                             'individual_use'              => isset( $coupon_data['individual_use'] ),
  134.                                             'product_ids'                 => isset( $coupon_data['product_ids'] ) ? array_filter( array_map( 'intval', (array) $coupon_data['product_ids'] ) ) : array(),
  135.                                             'excluded_product_ids'        => isset( $coupon_data['exclude_product_ids'] ) ? array_filter( array_map( 'intval', (array) $coupon_data['exclude_product_ids'] ) ) : array(),
  136.                                             'usage_limit'                 => absint( $coupon_data['usage_limit'] ),
  137.                                             'usage_limit_per_user'        => absint( $coupon_data['usage_limit_per_user'] ),
  138.                                             'limit_usage_to_x_items'      => absint( $coupon_data['limit_usage_to_x_items'] ),
  139.                                             'free_shipping'               => isset( $coupon_data['free_shipping'] ),
  140.                                             'product_categories'          => array_filter( array_map( 'intval', $product_categories ) ),
  141.                                             'excluded_product_categories' => array_filter( array_map( 'intval', $exclude_product_categories ) ),
  142.                                             'exclude_sale_items'          => isset( $coupon_data['exclude_sale_items'] ),
  143.                                             'minimum_amount'              => wc_format_decimal( $coupon_data['minimum_amount'] ),
  144.                                             'maximum_amount'              => wc_format_decimal( $coupon_data['maximum_amount'] ),
  145.                                             'email_restrictions'          => $coupon_data['email_restrictions'],
  146.                                         );
  147.                    
  148.                     $coupon_props   =   apply_filters( 'woogc/global_coupons/props', $coupon_props, $coupon_ID );
  149.                    
  150.                     $sites  =   $WooGC->functions->get_gc_sites( TRUE, 'global_coupon/save' );        
  151.                     foreach ( $sites    as  $site )
  152.                         {
  153.                             if ( $site->blog_id ==  $current_site )
  154.                                 continue;
  155.                            
  156.                             switch_to_blog( $site->blog_id );  
  157.                            
  158.                             $args = array(
  159.                                            'post_type'  =>  'shop_coupon',
  160.                                            'meta_key'   => '_global_coupon_hash',
  161.                                            'meta_value' =>  $coupon_hash  
  162.                                         );
  163.                             $founds = new WP_Query($args);
  164.                             if ( $founds->found_posts < 1 )
  165.                                 {
  166.                                     //no coupon found, creat it
  167.                                     $new_coupon_post = (array)$coupon_post_data;
  168.                                     unset ( $new_coupon_post['ID'] );
  169.                                     unset ( $new_coupon_post['guid'] );
  170.                                      
  171.                                     // Insert the post into the database
  172.                                     $coupon_id  =   wp_insert_post( $new_coupon_post );  
  173.                                 }
  174.                                 else
  175.                                 {
  176.                                     $founds->the_post();
  177.                                     $coupon_id  =   $founds->post->ID;
  178.                                    
  179.                                     $new_coupon_post = (array)$coupon_post_data;
  180.                                     $new_coupon_post['ID']  =   $coupon_id;
  181.                                      
  182.                                     // Insert the post into the database
  183.                                     wp_update_post( $new_coupon_post );      
  184.                                 }
  185.                                
  186.                             $coupon = new WC_Coupon( $coupon_id );
  187.                             $coupon->set_props( $coupon_props );
  188.                             $coupon->save();
  189.                            
  190.                             update_post_meta ( $coupon_id, 'global_coupon',  'yes' );
  191.                             update_post_meta ( $coupon_id, '_global_coupon_hash',  $coupon_hash );
  192.                            
  193.                             restore_current_blog();
  194.                         }
  195.                 }
  196.            
  197.         }
Advertisement
Add Comment
Please, Sign In to add comment