/** * 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/'); }
/** * 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'); } }
/** * 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; }
/** * 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; } }
/** * 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'); } } }