Advertisement
verygoodplugins

Untitled

Feb 10th, 2020
21,401
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.12 KB | None | 0 0
  1. function my_wpf_export_options( $options ) {
  2.  
  3.     $options['zoho_deals'] = array(
  4.         'label'   => 'Zoho Deals',
  5.         'title'   => 'Orders',
  6.         'tooltip' => 'Finds WooCommerce orders that don\'t have a Zoho invoice ID and exports them to Zoho',
  7.     );
  8.  
  9.     return $options;
  10.  
  11. }
  12.  
  13. add_filter( 'wpf_export_options', 'my_wpf_export_options' );
  14.  
  15. function my_wpf_batch_init() {
  16.  
  17.     $args = array(
  18.         'numberposts' => - 1,
  19.         'post_type'   => 'shop_order',
  20.         'post_status' => array( 'wc-processing', 'wc-completed' ),
  21.         'fields'      => 'ids',
  22.         'order'       => 'ASC',
  23.         'meta_query'  => array(
  24.             array(
  25.                 'key'     => 'wpf_ec_zoho_invoice_id',
  26.                 'compare' => 'NOT EXISTS',
  27.             ),
  28.         ),
  29.     );
  30.  
  31.     $orders = get_posts( $args );
  32.  
  33.     wp_fusion()->logger->handle( 'info', 0, 'Beginning <strong>Zoho Deals</strong> batch operation on ' . count( $orders ) . ' orders', array( 'source' => 'batch-process' ) );
  34.  
  35.     return $orders;
  36.  
  37. }
  38.  
  39. add_action( 'wpf_batch_zoho_deals_init', 'my_wpf_batch_init' );
  40.  
  41. function my_wpf_batch_step( $order_id ) {
  42.  
  43.     $contact_id = get_post_meta( $order_id, wp_fusion()->crm->slug . '_contact_id', true );
  44.  
  45.     if ( empty( $contact_id ) ) {
  46.  
  47.         $order   = wc_get_order( $order_id );
  48.         $user_id = $order->get_user_id();
  49.  
  50.         if ( ! empty( $user_id ) ) {
  51.             $contact_id = wp_fusion()->user->get_contact_id( $user_id );
  52.         }
  53.     }
  54.  
  55.     if ( empty( $contact_id ) ) {
  56.  
  57.         wp_fusion()->logger->handle( 'notice', 0, 'Unable to get contact ID from order ' . $order_id . '. Looking up email ' . $order->get_billing_email(), array( 'source' => 'batch-process' ) );
  58.  
  59.         $contact_id = wp_fusion()->crm->get_contact_id( $order->get_billing_email() );
  60.  
  61.     }
  62.  
  63.     if ( empty( $contact_id ) ) {
  64.  
  65.         wp_fusion()->logger->handle( 'error', 0, 'Failed to find contact ID for ' . $order->get_billing_email() . '. Unable to process order.', array( 'source' => 'batch-process' ) );
  66.         return;
  67.  
  68.     }
  69.    
  70.     if ( ! empty( $contact_id ) ) {
  71.  
  72.         delete_post_meta( $order_id, 'wpf_ec_complete' );
  73.  
  74.         wp_fusion_ecommerce()->integrations->woocommerce->send_order_data( $order_id, $contact_id );
  75.  
  76.     }
  77.  
  78. }
  79.  
  80. add_action( 'wpf_batch_zoho_deals', 'my_wpf_batch_step' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement