/**
  * Save meta box data.
  *
  * @param int $post_id
  * @param WP_Post $post
  */
 public static function save($post_id, $post)
 {
     global $wpdb;
     // Order data saved, now get it so we can manipulate status
     $order = wc_get_order($post_id);
     // Handle button actions
     if (!empty($_POST['wc_order_action'])) {
         $action = wc_clean($_POST['wc_order_action']);
         if (strstr($action, 'send_email_')) {
             do_action('woocommerce_before_resend_order_emails', $order);
             // Ensure gateways are loaded in case they need to insert data into the emails
             WC()->payment_gateways();
             WC()->shipping();
             // Load mailer
             $mailer = WC()->mailer();
             $email_to_send = str_replace('send_email_', '', $action);
             $mails = $mailer->get_emails();
             if (!empty($mails)) {
                 foreach ($mails as $mail) {
                     if ($mail->id == $email_to_send) {
                         $mail->trigger($order->get_id());
                         /* translators: %s: email title */
                         $order->add_order_note(sprintf(__('%s email notification manually sent.', 'woocommerce'), $mail->title), false, true);
                     }
                 }
             }
             do_action('woocommerce_after_resend_order_email', $order, $email_to_send);
             // Change the post saved message
             add_filter('redirect_post_location', array(__CLASS__, 'set_email_sent_message'));
         } elseif ('regenerate_download_permissions' === $action) {
             $data_store = WC_Data_Store::load('customer-download');
             $data_store->delete_by_order_id($post_id);
             wc_downloadable_product_permissions($post_id, true);
         } else {
             if (!did_action('woocommerce_order_action_' . sanitize_title($action))) {
                 do_action('woocommerce_order_action_' . sanitize_title($action), $order);
             }
         }
     }
 }
/**
 * @deprecated
 */
function woocommerce_downloadable_product_permissions($order_id)
{
    wc_downloadable_product_permissions($order_id);
}
 /**
  * Save meta box data
  */
 public static function save($post_id, $post)
 {
     // Order data saved, now get it so we can manipulate status
     $order = wc_get_order($post_id);
     // Handle button actions
     if (!empty($_POST['wc_order_action'])) {
         $action = wc_clean($_POST['wc_order_action']);
         if (strstr($action, 'send_email_')) {
             do_action('woocommerce_before_resend_order_emails', $order);
             // Ensure gateways are loaded in case they need to insert data into the emails
             WC()->payment_gateways();
             WC()->shipping();
             // Load mailer
             $mailer = WC()->mailer();
             $email_to_send = str_replace('send_email_', '', $action);
             $mails = $mailer->get_emails();
             if (!empty($mails)) {
                 foreach ($mails as $mail) {
                     if ($mail->id == $email_to_send) {
                         $mail->trigger($order->id);
                     }
                 }
             }
             do_action('woocommerce_after_resend_order_email', $order, $email_to_send);
         } elseif ($action == 'regenerate_download_permissions') {
             delete_post_meta($post_id, '_download_permissions_granted');
             wc_downloadable_product_permissions($post_id);
         } else {
             do_action('woocommerce_order_action_' . sanitize_title($action), $order);
         }
     }
 }
 /**
  * Update order data
  *
  * Based on WC_API_Customers::update_customer_data()
  *
  * @since 3.0.0
  * @param \WC_Order $order
  * @param array $data
  * @param array $options
  */
 private function update_order_data(WC_Order $order, $data, $options)
 {
     if (isset($data['date']) && $data['date']) {
         wp_update_post(array('ID' => $order->id, 'post_date' => date('Y-m-d H:i:s', $data['date'])));
     }
     $merging = $options['merge'] && isset($data['id']) && $data['id'];
     $this->process_terms($order->id, $data['terms']);
     // set order addresses
     $order->set_address($data['billing_address'], 'billing');
     $order->set_address($data['shipping_address'], 'shipping');
     // clear any previously set refunded order item ids
     $this->refunded_item_order_ids = array();
     // set order lines
     foreach ($this->line_types as $line_type => $line) {
         // don't set lines if they're empty. This ensures partial updates/merges
         // are supported and won't wipe out order lines
         if (!empty($data[$line]) && is_array($data[$line])) {
             $this->process_items($order, $data[$line], $line_type, $merging);
         }
     }
     // set order currency
     if (isset($data['currency'])) {
         update_post_meta($order->id, '_order_currency', $data['currency']);
     }
     // grant downloadable product permissions
     if (isset($data['download_permissions_granted']) && $data['download_permissions_granted']) {
         wc_downloadable_product_permissions($order->id);
     }
     // set order meta
     if (isset($data['order_meta']) && is_array($data['order_meta'])) {
         $this->set_order_meta($order->id, $data['order_meta']);
     }
     // set the paying customer flag on the user meta if applicable
     $paid_statuses = array('processing', 'completed', 'refunded');
     if ($data['customer_id'] && in_array($data['status'], $paid_statuses)) {
         update_user_meta($data['customer_id'], "paying_customer", 1);
     }
     // process refunds
     if (!empty($data['refunds'])) {
         // remove previous refunds
         foreach ($order->get_refunds() as $refund) {
             wc_delete_shop_order_transients($refund->id);
             wp_delete_post($refund->id, true);
         }
         foreach ($data['refunds'] as $refund_data) {
             // try mapping temp refunded item ids to real order item ids
             if (!empty($refund_data['line_items'])) {
                 foreach ($refund_data['line_items'] as $key => $refunded_item) {
                     if (isset($refunded_item['refunded_item_temp_id'])) {
                         $temp_id = $refunded_item['refunded_item_temp_id'];
                         // get the real order item id for this refunded itme
                         $order_item_id = $this->get_array_key_value($this->refunded_item_order_ids, $temp_id);
                         if ($order_item_id) {
                             $refund_data['line_items'][$order_item_id] = $refunded_item;
                             unset($refund_data['line_items'][$key]);
                         }
                     }
                 }
             }
             wc_create_refund(array('order_id' => $order->id, 'amount' => $refund_data['amount'], 'reason' => $refund_data['reason'], 'line_items' => $refund_data['line_items'], 'date' => $refund_data['date']));
         }
     }
     wc_delete_shop_order_transients($order->id);
     /**
      * Triggered after order data has been updated via CSV
      *
      * This will be triggered for both new and updated orders
      *
      * @since 3.0.0
      * @param int $id Order ID
      * @param array $data Order data
      * @param array $options Import options
      */
     do_action('wc_csv_import_suite_update_order_data', $order->id, $data, $options);
 }