The WooCommerce order factory creating the right order objects
Author: WooThemes
Example #1
0
 public function parse_order_data($order_id)
 {
     $result = array('amount' => 0, 'transaction_id' => 0, 'description' => '');
     $f = new WC_Order_Factory();
     $order = $f->get_order($order_id);
     if (!$order) {
         return $result;
     }
     $result['amount'] = $order->get_total() - $order->get_total_tax() - $order->get_total_shipping();
     if ($this->tax_mode === "manual") {
         $result['amount'] *= 10000;
         $result['amount'] /= 100 + floatval($this->tax_amount);
         $result['amount'] = 0.01 * round($result['amount']);
     }
     $result['transaction_id'] = $order->get_order_number();
     $result['description'] = array();
     foreach ($order->get_items() as $item) {
         $result['description'][] = $item['item_meta']['_qty'][0] . 'x' . $item['name'];
     }
     $result['description'] = implode('|', $result['description']);
     return $result;
 }
 /**
  * Get an order item object, based on it's type.
  * @since  2.7.0
  * @param  int $item_id
  * @return WC_Order_Item
  */
 public function get_item($item_id)
 {
     return WC_Order_Factory::get_order_item($item_id);
 }
 /**
  * Stores information about whether stock was reduced.
  *
  * @param WC_Order|int $order
  * @param bool $set
  */
 public function set_stock_reduced($order, $set)
 {
     $order_id = WC_Order_Factory::get_order_id($order);
     update_post_meta($order_id, '_order_stock_reduced', wc_bool_to_string($set));
 }
function plexuserGuestCheckoutUserCb($order_id = '')
{
    try {
        if (!empty($order_id)) {
            $wooFactoryObject = new WC_Order_Factory();
            $wooOrderObject = $wooFactoryObject->get_order($order_id);
            $user_id = $wooOrderObject->get_user_id();
            $update_customer = false;
            if (empty($user_id)) {
                $_billing_first_name = get_post_meta($order_id, '_billing_first_name', true);
                $_billing_first_name = isset($_billing_first_name) && !empty($_billing_first_name) ? trim($_billing_first_name) : '';
                $_billing_email = get_post_meta($order_id, '_billing_email', true);
                $_billing_email = isset($_billing_email) && !empty($_billing_email) ? trim($_billing_email) : '';
                if (!empty($_billing_email)) {
                    if (email_exists($_billing_email) == true) {
                        $user = get_user_by('email', $_billing_email);
                        $unam = isset($user->user_login) && !empty($user->user_login) ? $user->user_login : '';
                        $user_id = isset($user->ID) && !empty($user->ID) ? $user->ID : false;
                        $update_customer = true;
                    } else {
                        $unam = $_billing_email;
                        $upass = wp_generate_password(12, false);
                        $_SESSION["woocomGuestCheckOut"] = "1";
                        $user_id = wp_create_user($unam, $upass, $_billing_email);
                        if (!empty($user_id)) {
                            $usrObj = new WP_User($user_id);
                            $usrObj->remove_role('subscriber');
                            $usrObj->add_role('customer');
                            $update_customer = true;
                        } else {
                            $user_id = false;
                        }
                    }
                } else {
                    $unam = 'plexuser.guest_' . $order_id . '_' . time();
                    $umail = 'plexuser.guest_' . $order_id . '@plexuser.com';
                    if (email_exists($umail) == false) {
                        $upass = wp_generate_password(12, false);
                        $_SESSION["woocomGuestCheckOut"] = "1";
                        $user_id = wp_create_user($unam, $upass, $umail);
                        $usrObj = new WP_User($user_id);
                        $usrObj->remove_role('subscriber');
                        $usrObj->add_role('customer');
                        $update_customer = true;
                    } else {
                        $user_id = false;
                    }
                }
            } else {
                $user_id = $user_id;
            }
            if ($update_customer && !empty($user_id) && !empty($unam)) {
                $custom_field_keys = get_post_custom_keys($order_id);
                if (!in_array('_customer_user', $custom_field_keys)) {
                    add_post_meta($order_id, '_customer_user', $user_id, false);
                } else {
                    update_post_meta($order_id, '_customer_user', $user_id);
                }
                $pattern = '#(^_billing|^_shipping)#';
                foreach ($custom_field_keys as $ky => $val) {
                    if (is_string($val)) {
                        $ord_meta = get_post_meta($order_id, $val, true);
                        $ord_meta = isset($ord_meta) && !empty($ord_meta) ? trim($ord_meta) : '';
                        if (preg_match($pattern, $val) === 1) {
                            $xkey = trim($val, '_');
                            if (get_user_meta($user_id, $xkey, true) == '') {
                                add_user_meta($user_id, $xkey, $ord_meta, true);
                            } else {
                                update_user_meta($user_id, $xkey, $ord_meta);
                            }
                        }
                    }
                }
            }
        } else {
            $user_id = false;
        }
        return $user_id;
    } catch (Exception $e) {
        print $e->getFile() . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString() . "\n";
    }
}
function gocoin_callback()
{
    global $woocommerce;
    $gateways = $woocommerce->payment_gateways->payment_gateways();
    $logger = new WC_Logger();
    if (!isset($gateways['gocoin'])) {
        return;
    }
    $gocoin = $gateways['gocoin'];
    $gocoin_setting = isset($gocoin->settings) && is_array($gocoin->settings) ? $gocoin->settings : array();
    $key = isset($gocoin_setting['accessToken']) && !empty($gocoin_setting['accessToken']) ? $gocoin_setting['accessToken'] : '';
    if (empty($key)) {
        return $logger->add('gocoin-callback', 'Api Key is  blank');
    }
    $data = Util::postData();
    if (isset($data->error)) {
        return $logger->add('gocoin-callback', $data->error);
    } else {
        //  $key                = $gocoin -> settings -> accessToken;
        $event_id = $data->id;
        $event = $data->event;
        $invoice = $data->payload;
        $payload_arr = get_object_vars($invoice);
        ksort($payload_arr);
        $signature = $invoice->user_defined_8;
        $sig_comp = Util::sign($payload_arr, $key);
        $status = $invoice->status;
        $order_id = (int) $invoice->order_id;
        $order = WC_Order_Factory::get_order($order_id);
        if (!$order) {
            $msg = "Order with id: " . $order_id . " was not found. Event ID: " . $event_id;
            return $logger->add('gocoin-callback', $msg);
        }
        // Check that if a signature exists, it is valid
        if (isset($signature) && $signature != $sig_comp) {
            $msg = "Signature : " . $signature . "does not match for Order: " . $order_id . "{$sig_comp}        |    {$signature} ";
        } elseif (empty($signature) || empty($sig_comp)) {
            $msg = "Signature is blank for Order: " . $order_id;
        } elseif ($signature == $sig_comp) {
            switch ($event) {
                case 'invoice_created':
                    break;
                case 'invoice_payment_received':
                    switch ($status) {
                        case 'ready_to_ship':
                            $msg = 'Order ' . $order_id . ' is paid and awaiting payment confirmation on blockchain.';
                            $order->update_status('on-hold', __($msg, 'woothemes'));
                            break;
                        case 'paid':
                            $msg = 'Order ' . $order_id . ' is paid and awaiting payment confirmation on blockchain.';
                            $order->update_status('on-hold', __($msg, 'woothemes'));
                            break;
                        case 'underpaid':
                            $msg = 'Order ' . $order_id . ' is underpaid.';
                            $order->update_status('on-hold', __($msg, 'woothemes'));
                            break;
                    }
                    break;
                case 'invoice_merchant_review':
                    $msg = 'Order ' . $order_id . ' is under review. Action must be taken from the GoCoin Dashboard.';
                    $order->update_status('on-hold', __($msg, 'woothemes'));
                    break;
                case 'invoice_ready_to_ship':
                    $msg = 'Order ' . $order_id . ' has been paid in full and confirmed on the blockchain.';
                    $order->payment_complete();
                    break;
                case 'invoice_invalid':
                    $msg = 'Order ' . $order_id . ' is invalid and will not be confirmed on the blockchain.';
                    $order->update_status('failed', __($msg, 'woothemes'));
                    break;
                default:
                    $msg = "Unrecognized event type: " . $event;
            }
            if (isset($msg)) {
                $msg .= ' Event ID: ' . $event_id;
            }
        }
        return $logger->add('gocoin-callback', $msg);
    }
}