'pending payment', self::PROCESSING_STATUS => 'processing', self::ONHOLD_STATUS => 'on hold', self::COMPLETED_STATUS => 'completed', self::CANCELLED_STATUS => 'cancelled', self::REFOUNDED_STATUS => 'refounded', self::FAILED_STATUS => 'failed' ); private $options; /** * Constructor */ public function __construct() { $this->options = new AmzFulfillment_Options(); $this->options->load(); add_action('woocommerce_order_actions', array ($this, 'addMetaboxAction')); add_action('woocommerce_order_action_' . self::METABOX_ACTION, array($this, 'processMetaboxAction')); add_action('admin_footer-edit.php', array($this, 'addBulkAction')); add_action('load-edit.php', array($this, 'processBulkAction')); add_action('woocommerce_order_status_pending', array($this, 'orderStatusPending')); add_action('woocommerce_order_status_processing', array($this, 'orderStatusProcessing')); add_action('woocommerce_order_status_on-hold', array($this, 'orderStatusOnHold')); add_action('woocommerce_order_status_completed', array($this, 'orderStatusCompleted')); add_action('woocommerce_order_status_cancelled', array ($this, 'orderStatusCancelled')); add_action('woocommerce_order_status_refunded', array($this, 'orderStatusRefunded')); add_action('woocommerce_order_status_failed', array($this, 'orderStatusFailed')); } /** * sendToAmazon * * @param int|WC_Order $order */ public function sendToAmazon($orderId) { if(!is_integer($orderId)) { $orderId = $orderId->get_id(); } AmzFulfillment_Core::instance()->doAmazonFulfillment($orderId); } /** * addBulkAction */ public function addBulkAction() { global $post_type; if ($post_type == 'shop_order') { printf(""); } } /** * processBulkAction */ public function processBulkAction() { $wpListTable = _get_list_table('WP_Posts_List_Table'); $action = $wpListTable->current_action(); if($action !== self::BULK_ACTION) { return; } $orderIds = array_map('absint', (array) $_REQUEST['post']); if(!count($orderIds)) { return; } AmzFulfillment_Core::instance()->getLog()->add(sprintf('Start bulk fulfillment for orders: %s', implode(', ', $orderIds))); foreach($orderIds as $orderId) { if(AmzFulfillment_Core::instance()->getFulfillment()->exist($orderId)) { AmzFulfillment_Core::instance()->getLog()->add(sprintf('Skipping fulfillment for order %d: Was already send to amazon')); } else { $this->sendToAmazon($orderId); } } } /** * addMetaboxAction * * Add amazon option to $actions array * * @param array $actions * @return array */ public function addMetaboxAction($actions) { $actions[self::METABOX_ACTION] = 'Amazon fulfillment'; return $actions; } /** * processMetaboxAction * * Do amazon fulfillment, executed by product meta box * * @param mixed $order */ public function processMetaboxAction($order) { $this->sendToAmazon($order); } /** * getProducts * * proucts with propperties: * - title * - sku * - stock * - productId * - varriantId * * @return array */ public function getProducts() { global $wpdb; $products = array(); $query = " SELECT p.ID, p.post_title, ts.slug FROM `%sposts` AS p INNER JOIN `%sterm_relationships` AS t ON p.ID = t.object_id INNER JOIN `%sterm_taxonomy` AS tt ON t.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN `%sterms` AS ts ON tt.term_id = ts.term_id WHERE tt.taxonomy = 'product_type' AND p.post_type = 'product' AND p.post_status = 'publish'"; $results = $wpdb->get_results(sprintf($query, $wpdb->prefix, $wpdb->prefix, $wpdb->prefix, $wpdb->prefix)); foreach($results as $result) { if($result->slug == "simple") { $products[] = array( 'title' => $result->post_title, 'sku' => get_post_meta($result->ID, '_sku', true), 'stock' => get_post_meta($result->ID, '_stock', true), 'productId' => $result->ID, 'varriantId' => false ); } elseif($result->slug == "variable"){ $args = array('post_type' => 'product_variation', 'post_status' => array( 'publish' ), 'post_parent' => $result->ID); $variations = get_posts($args); foreach($variations as $variation) { $products[] = array( 'title' => $result->post_title, 'sku' => get_post_meta($variation->ID, '_sku', true), 'stock' => get_post_meta($variation->ID, '_stock', true), 'productId' => $result->ID, 'varriantId' => $variation->ID ); } } } return $products; } /** * getProductIdBySku * * @param string $sku * @throws Exception * @return int id */ public function getProductIdBySku($sku) { global $wpdb; $query = "SELECT pm.post_id FROM %spostmeta AS pm JOIN %sposts AS p ON pm.post_id = p.ID AND pm.meta_value='%s' AND pm.meta_key='_sku' AND p.post_status='publish'"; $result = $wpdb->get_results(sprintf($query, $wpdb->prefix, $wpdb->prefix, $sku)); if(!isset($result[0])) { throw new Exception('No product found with sku ' . $sku); } return intval($result[0]->post_id); } /** * setStock * * @param int $productId * @param int $stock */ public function setStock($productId, $stock) { update_post_meta($productId, '_stock', $stock); } /** * getCurrency * * Get the currency code which is configure in WooCommerce * * @return string */ public function getCurrency() { return get_option('woocommerce_currency'); } /** * getSkus * * Get skus from all products and varriations * * @return array */ public function getSkus() { $skus = array(); foreach($this->getProducts() as $product) { $skus[] = $product['sku']; } return $skus; } /** * addNote * * @param int $orderId * @param string $note */ public function addNote($orderId, $note) { (new WC_Order($orderId))->add_order_note($note); } /** * setStatus * * @param int $orderId * @param string $status * @throws InvalidArgumentException * @throws RuntimeException */ public function setStatus($orderId, $status) { if(!array_key_exists($status, self::$orderStatusType)) { throw new InvalidArgumentException('Invalid order status type: ' . $status); } $order = new WC_Order($orderId); if($order->get_status() == $status) { throw new RuntimeException('Order is already in the status to be set: ' . $status); } $order->set_status($status); } /** * orderStatusPending * * Order has changed its status to pending * * @param int $orderId */ public function orderStatusPending($orderId) { AmzFulfillment_Core::instance()->getEventHandler()->createEvent(AmzFulfillment_Event::WOOCOMMERCE_ORDER_PENDING, $orderId); } /** * orderStatusProcessing * * Order has changed its status to processing * * @param int $orderId */ public function orderStatusProcessing($orderId) { AmzFulfillment_Core::instance()->getEventHandler()->createEvent(AmzFulfillment_Event::WOOCOMMERCE_ORDER_PROCESSING, $orderId); } /** * orderStatusOnHold * * Order has changed its status to on-hold * * @param int $orderId */ public function orderStatusOnHold($orderId) { AmzFulfillment_Core::instance()->getEventHandler()->createEvent(AmzFulfillment_Event::WOOCOMMERCE_ORDER_ONHOLD, $orderId); } /** * orderStatusCompleted * * Order has changed its status to completed * * @param int $orderId */ public function orderStatusCompleted($orderId) { AmzFulfillment_Core::instance()->getEventHandler()->createEvent(AmzFulfillment_Event::WOOCOMMERCE_ORDER_COMPLETED, $orderId); } /** * orderStatusCancelled * * Order has changed its status to cancelled * * @param int $orderId */ public function orderStatusCancelled($orderId) { AmzFulfillment_Core::instance()->getEventHandler()->createEvent(AmzFulfillment_Event::WOOCOMMERCE_ORDER_CANCELLED, $orderId); } /** * orderStatusRefunded * * Order has changed its status to pending * * @param int $orderId */ public function orderStatusRefunded($orderId) { AmzFulfillment_Core::instance()->getEventHandler()->createEvent(AmzFulfillment_Event::WOOCOMMERCE_ORDER_REFUNDED, $orderId); } /** * orderStatusFailed * * Order has changed its status to failed * * @param int $orderId */ public function orderStatusFailed($orderId) { AmzFulfillment_Core::instance()->getEventHandler()->createEvent(AmzFulfillment_Event::WOOCOMMERCE_ORDER_FAILED, $orderId); } }