/**
  * Get the order total formatted to show when the order will be charged
  *
  * @since 1.0
  * @param string $total price string ( note: this is already formatted by woocommerce_price() )
  * @return string the formatted order total price string
  */
 public function get_formatted_cart_total($total)
 {
     // this check prevents a formatted total from display anywhere but the cart/checkout page
     if ($this->cart_contains_pre_order()) {
         $total = WC_Pre_Orders_Manager::get_formatted_pre_order_total($total, self::get_pre_order_product());
     }
     return $total;
 }
 /**
  * Output WooCommerce Pre-orders "My Pre-Orders" table in the user's My Account
  * page
  *
  * @access public
  */
 function wc_pre_orders_my_account_pre_orders()
 {
     global $wc_pre_orders;
     $pre_orders = WC_Pre_Orders_Manager::get_users_pre_orders();
     $actions = array();
     // determine the available actions (Cancel)
     foreach ($pre_orders as $order) {
         $_actions = array();
         if (WC_Pre_Orders_Manager::can_pre_order_be_changed_to('cancelled', $order)) {
             $_actions['cancel'] = array('url' => WC_Pre_Orders_Manager::get_users_change_status_link('cancelled', $order), 'name' => __('Cancel', 'wc-pre-orders'));
         }
         $actions[$order->id] = $_actions;
     }
     // Load the template
     woocommerce_get_template('myaccount/my-pre-orders.php', array('pre_orders' => $pre_orders, 'actions' => $actions), '', $wc_pre_orders->get_plugin_path() . '/templates/');
 }
 /**
  * Get the fields to display for the selected action, in the format required by woocommerce_admin_fields()
  *
  * @since 1.0
  * @param string $section the current section to get fields for
  * @return array
  */
 private function get_action_fields($section)
 {
     $products = array(' ' => '');
     foreach (WC_Pre_Orders_Manager::get_all_pre_order_enabled_products() as $product) {
         $products[$product->id] = woocommerce_get_formatted_product_name($product);
     }
     $fields = array('email' => array(array('name' => __('Email Pre-Order Customers', WC_Pre_Orders::TEXT_DOMAIN), 'desc' => sprintf(__('You may send an email message to all customers who have pre-ordered a specific product. This will use the default template specified for the %sCustomer Note%s Email.', WC_Pre_Orders::TEXT_DOMAIN), '<a href="' . admin_url('admin.php?page=woocommerce_settings&tab=email&section=WC_Email_Customer_Note') . '">', '</a>'), 'type' => 'title'), array('id' => 'wc_pre_orders_action_product', 'name' => __('Product', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Select which product to email all pre-ordered customers.'), 'default' => ' ', 'options' => $products, 'type' => 'select'), array('id' => 'wc_pre_orders_action_email_message', 'name' => __('Message', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Enter a message to include in the email notification to customer. Limited HTML allowed.', WC_Pre_Orders::TEXT_DOMAIN), 'css' => 'min-width: 300px;', 'default' => '', 'type' => 'textarea'), array('type' => 'sectionend'), array('name' => __('Send Emails', WC_Pre_Orders::TEXT_DOMAIN), 'type' => 'submit_button')), 'change-date' => array(array('name' => __('Change the Pre-Order Release Date', WC_Pre_Orders::TEXT_DOMAIN), 'desc' => __('You may change the release date for all pre-orders of a specific product. This will send an email notification to each customer informing them that the pre-order release date was changed, along with the new release date.', WC_Pre_Orders::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_pre_orders_action_product', 'name' => __('Product', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Select which product to change the release date for.'), 'default' => ' ', 'options' => $products, 'type' => 'select'), array('id' => 'wc_pre_orders_action_new_availability_date', 'name' => __('New Availability Date', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('The new availability date for the product. This must be later than the current availability date.', WC_Pre_Orders::TEXT_DOMAIN), 'default' => '', 'type' => 'text'), array('id' => 'wc_pre_orders_action_enable_email_notification', 'name' => __('Send Email Notification', WC_Pre_Orders::TEXT_DOMAIN), 'desc' => __('Uncheck this to prevent email notifications from being sent to customers.', WC_Pre_Orders::TEXT_DOMAIN), 'default' => 'yes', 'type' => 'checkbox'), array('id' => 'wc_pre_orders_action_email_message', 'name' => __('Message', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Enter a message to include in the email notification to customer.', WC_Pre_Orders::TEXT_DOMAIN), 'default' => '', 'css' => 'min-width: 300px;', 'type' => 'textarea'), array('type' => 'sectionend'), array('name' => __('Change Release Date', WC_Pre_Orders::TEXT_DOMAIN), 'type' => 'submit_button')), 'complete' => array(array('name' => __('Complete Pre-Orders', WC_Pre_Orders::TEXT_DOMAIN), 'desc' => __('You may complete all pre-orders for a specific product. This will charge the customer\'s card the pre-ordered amount, change their order status to completed, and send them an email notification.', WC_Pre_Orders::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_pre_orders_action_product', 'name' => __('Product', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Select which product to complete all pre-orders for.'), 'default' => ' ', 'options' => $products, 'type' => 'select'), array('id' => 'wc_pre_orders_action_enable_email_notification', 'name' => __('Send Email Notification', WC_Pre_Orders::TEXT_DOMAIN), 'desc' => __('Uncheck this to prevent email notifications from being sent to customers.', WC_Pre_Orders::TEXT_DOMAIN), 'default' => 'yes', 'type' => 'checkbox'), array('id' => 'wc_pre_orders_action_email_message', 'name' => __('Message', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Enter a message to include in the email notification to customer.', WC_Pre_Orders::TEXT_DOMAIN), 'default' => '', 'css' => 'min-width: 300px;', 'type' => 'textarea'), array('type' => 'sectionend'), array('name' => __('Complete Pre-Orders', WC_Pre_Orders::TEXT_DOMAIN), 'type' => 'submit_button')), 'cancel' => array(array('name' => __('Cancel Pre-Orders', WC_Pre_Orders::TEXT_DOMAIN), 'desc' => __('You may cancel all pre-orders for a specific product. This will mark the order as cancelled and send the customer an email notification. If pre-orders were charged upfront, you must manually refund the orders.', WC_Pre_Orders::TEXT_DOMAIN), 'type' => 'title'), array('id' => 'wc_pre_orders_action_product', 'name' => __('Product', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Select which product to cancel all pre-orders for.'), 'default' => ' ', 'options' => $products, 'type' => 'select'), array('id' => 'wc_pre_orders_action_enable_email_notification', 'name' => __('Send Email Notification', WC_Pre_Orders::TEXT_DOMAIN), 'desc' => __('Uncheck this to prevent email notifications from being sent to customers.', WC_Pre_Orders::TEXT_DOMAIN), 'default' => 'yes', 'type' => 'checkbox'), array('id' => 'wc_pre_orders_action_email_message', 'name' => __('Message', WC_Pre_Orders::TEXT_DOMAIN), 'desc_tip' => __('Enter a message to include in the email notification to customer.', WC_Pre_Orders::TEXT_DOMAIN), 'default' => '', 'css' => 'min-width: 300px;', 'type' => 'textarea'), array('type' => 'sectionend'), array('name' => __('Cancel Pre-Orders', WC_Pre_Orders::TEXT_DOMAIN), 'type' => 'submit_button')));
     return isset($fields[$section]) ? $fields[$section] : array();
 }
 /**
  * Automatically cancel a pre-order if it's parent order is moved to the trash. Note that un-trashing the order does
  * not change the pre-order back to it's original status
  *
  * @since 1.0
  * @param int $order_id the order post ID
  */
 public function maybe_cancel_trashed_pre_order($order_id)
 {
     $order = new WC_Order($order_id);
     if ($this->order_contains_pre_order($order) && WC_Pre_Orders_Manager::can_pre_order_be_changed_to('cancelled', $order)) {
         $this->update_pre_order_status($order, 'cancelled');
     }
 }
 /**
  * Handle actions for both individual items and bulk edit
  *
  * @since 1.0
  */
 public function process_actions()
 {
     // get the current action (if any)
     $action = $this->current_action();
     // get the set of orders to operate on
     $order_ids = isset($_REQUEST['order_id']) ? array_map('absint', $_REQUEST['order_id']) : array();
     $message = $this->get_current_customer_message();
     // no action, or invalid action
     if (isset($_GET['page']) && 'wc_pre_orders' == $_GET['page']) {
         if (false === $action || empty($order_ids)) {
             if (!empty($_REQUEST['_wp_http_referer'])) {
                 // remove _wp_http_referer/_wp_nonce/action params
                 wp_redirect(remove_query_arg(array('_wp_http_referer', '_wpnonce', 'action', 'action2'), stripslashes($_SERVER['REQUEST_URI'])));
                 exit;
             }
             return;
         }
         $success_count = $error_count = 0;
         // process the orders
         foreach ($order_ids as $order_id) {
             $order = new WC_Order($order_id);
             // perform the action
             switch ($action) {
                 case 'cancel':
                     if (WC_Pre_Orders_Manager::can_pre_order_be_changed_to('cancelled', $order)) {
                         $success_count++;
                         WC_Pre_Orders_Manager::cancel_pre_order($order, $message);
                     } else {
                         $error_count++;
                     }
                     break;
                 case 'complete':
                     if (WC_Pre_Orders_Manager::can_pre_order_be_changed_to('completed', $order)) {
                         $success_count++;
                         WC_Pre_Orders_Manager::complete_pre_order($order, $message);
                     } else {
                         $error_count++;
                     }
                     break;
                 case 'message':
                     WC_Pre_Orders_Manager::email_pre_order_customer($order_id, $message);
                     break;
             }
         }
         $messages = array();
         switch ($action) {
             case 'cancel':
                 if ($success_count > 0) {
                     $messages[] = sprintf(_n('%d pre-order cancelled.', '%s pre-orders cancelled.', $success_count, 'wc-pre-orders'), $success_count);
                 }
                 if ($error_count > 0) {
                     $messages[] = sprintf(_n('%d pre-order could not be cancelled.', '%s pre-orders could not be cancelled.', $error_count, 'wc-pre-orders'), $error_count);
                 }
                 break;
             case 'complete':
                 if ($success_count > 0) {
                     $messages[] = sprintf(_n('%d pre-order completed.', '%s pre-orders completed.', $success_count, 'wc-pre-orders'), $success_count);
                 }
                 if ($error_count > 0) {
                     $messages[] = sprintf(_n('%d pre-order could not be completed.', '%s pre-orders could not be completed.', $error_count, 'wc-pre-orders'), $error_count);
                 }
                 break;
             case 'message':
                 $messages[] = sprintf(_n('%d email dispatched.', '%s emails dispatched.', count($order_ids), 'wc-pre-orders'), count($order_ids));
                 break;
         }
         $message_nonce = wp_create_nonce(__FILE__);
         set_transient($this->message_transient_prefix . $message_nonce, array('messages' => implode('  ', $messages)), 60 * 60);
         // get our next destination, stripping out all actions and other unneeded parameters
         if (isset($_REQUEST['_wp_http_referer'])) {
             $redirect_url = $_REQUEST['_wp_http_referer'];
         } else {
             $redirect_url = remove_query_arg(array('_wp_http_referer', '_wpnonce', 'action', 'action2', 'order_id', 'customer_message', 'customer_message2'), stripslashes($_SERVER['REQUEST_URI']));
         }
         wp_redirect(add_query_arg('message', $message_nonce, $redirect_url));
         exit;
     }
 }
 /**
  * Get column content, this is called once per column, per row item ($order)
  * returns the content to be rendered within that cell.
  *
  * @see WP_List_Table::single_row_columns()
  * @since 1.0
  * @param WC_Order $order one row (item) in the table
  * @param string $column_name the column slug
  * @return string the column content
  */
 public function column_default($order, $column_name)
 {
     switch ($column_name) {
         case 'status':
             $actions = array();
             // base action url
             $action_url = add_query_arg('order_id[]', $order->id);
             // determine any available actions
             if (WC_Pre_Orders_Manager::can_pre_order_be_changed_to('cancelled', $order)) {
                 $actions['cancel'] = sprintf('<a href="%s">%s</a>', add_query_arg('action', 'cancel', $action_url), __('Cancel', 'wc-pre-orders'));
             }
             $column_content = sprintf('<mark class="%s tips" data-tip="%s">%s</mark>', WC_Pre_Orders_Order::get_pre_order_status($order), WC_Pre_Orders_Order::get_pre_order_status_to_display($order), WC_Pre_Orders_Order::get_pre_order_status_to_display($order));
             $column_content .= $this->row_actions($actions);
             break;
         case 'customer':
             if (0 !== $order->user_id) {
                 $column_content = sprintf('<a href="%s">%s</a>', get_edit_user_link($order->user_id), $order->billing_email);
             } else {
                 $column_content = $order->billing_email;
             }
             break;
         case 'product':
             $item = WC_Pre_Orders_Order::get_pre_order_item($order);
             $product_edit = get_edit_post_link($item['product_id']);
             $column_content = $product_edit ? sprintf('<a href="%s">%s</a>', $product_edit, $item['name']) : $item['name'];
             break;
         case 'order':
             $column_content = sprintf('<a href="%s">%s</a>', get_edit_post_link($order->id), sprintf(__('Order %s', 'wc-pre-orders'), $order->get_order_number()));
             break;
         case 'order_date':
             $column_content = date_i18n(woocommerce_date_format(), strtotime($order->order_date));
             break;
         case 'availability_date':
             $product = WC_Pre_Orders_Order::get_pre_order_product($order);
             $column_content = WC_Pre_Orders_Product::get_localized_availability_date($product, '--');
             break;
         default:
             $column_content = '';
             break;
     }
     return $column_content;
 }
 /**
  * Maybe cancel pre order when product is trashed
  *
  * @param int $product_id Product ID
  * @return void
  */
 public function maybe_cancel_pre_order_product_trashed($product_id)
 {
     global $wpdb;
     $orders = $wpdb->get_results($wpdb->prepare("\n\t\t\t\tSELECT order_items.order_id\n\t\t\t\tFROM {$wpdb->prefix}woocommerce_order_items AS order_items\n\t\t\t\t\tLEFT JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS order_itemmeta\n\t\t\t\t\tON order_itemmeta.order_item_id = order_items.order_item_id\n\t\t\t\tWHERE order_itemmeta.meta_key = '_product_id'\n\t\t\t\tAND order_itemmeta.meta_value = %d\n\t\t\t", $product_id));
     foreach ($orders as $order_data) {
         $order = new WC_Order($order_data->order_id);
         if (WC_Pre_Orders_Order::order_contains_pre_order($order) && WC_Pre_Orders_Manager::can_pre_order_be_changed_to('cancelled', $order)) {
             WC_Pre_Orders_Order::update_pre_order_status($order, 'cancelled');
         }
     }
 }
 /**
  * Checks for action to cancel an existing pre order and if needed it gets executed
  *
  * @since 1.0.3
  */
 public function check_cancel_pre_order()
 {
     global $woocommerce;
     if (!isset($_GET['order_id']) || !isset($_GET['status'])) {
         return;
     }
     if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], absint($_GET['order_id']))) {
         return;
     }
     WC_Pre_Orders_Manager::cancel_pre_order(absint($_GET['order_id']));
     $string = __('Your pre-order has been cancelled.', 'wc-pre-orders');
     // Backwards compatible (pre 2.1) for outputting notice
     if (function_exists('wc_add_notice')) {
         wc_add_notice($string);
     } else {
         $woocommerce->add_message($string);
     }
 }
 /**
  * Checks for action to cancel an existing pre order and if needed it gets executed
  *
  * @since 1.0.3
  */
 public function check_cancel_pre_order()
 {
     global $woocommerce;
     if (!isset($_GET['order_id']) || !isset($_GET['status'])) {
         return;
     }
     if (!isset($_GET['_wpnonce']) || !wp_verify_nonce($_GET['_wpnonce'], absint($_GET['order_id']))) {
         return;
     }
     WC_Pre_Orders_Manager::cancel_pre_order(absint($_GET['order_id']));
     $woocommerce->add_message(__('Your pre-order has been cancelled.', WC_Pre_Orders::TEXT_DOMAIN));
 }