public static function order_status_changed($id, $status = '', $new_status = '') { $rules = get_option('woorule_rules', array()); foreach ($rules as $k => $rule) { $enabled = get_option($rule['enabled']['id']) === 'yes' ? true : false; if ($enabled) { $which_event = get_option($rule['occurs']['id']); $is_opt_in_rule = false; $want_in = true; if (isset($rule['show_opt_in'])) { $is_opt_in_rule = get_option($rule['show_opt_in']['id']) === 'yes' ? true : false; } if ($is_opt_in_rule) { $want_in = get_post_meta($id, 'woorule_opt_in_' . $k, false); if (!empty($want_in)) { $want_in = $want_in[0] === 'yes' ? true : false; } } if ($want_in && $new_status === $which_event) { $integration = new WC_Integration_RuleMailer(); $order = new WC_Order($id); $user = $order->get_user(); $currency = $order->get_order_currency(); $order_subtotal = $order->order_total - ($order->order_shipping_tax + $order->order_shipping) - $order->cart_discount; $items = $order->get_items(); $brands = array(); $categories = array(); $tags = array(); foreach ($items as $item) { $p = new WC_Product_Simple($item['product_id']); $brands[] = $p->get_attribute('brand'); // this is bullshit $cat = strip_tags($p->get_categories('')); $tag = strip_tags($p->get_tags('')); if (!empty($cat)) { $categories[] = $cat; } if (!empty($tag)) { $tags[] = $tag; } } $subscription = array('apikey' => $integration->api_key, 'update_on_duplicate' => get_option($rule['update_on_duplicate']['id']) === 'yes' ? true : false, 'auto_create_tags' => get_option($rule['auto_create_tags']['id']) === 'yes' ? true : false, 'auto_create_fields' => get_option($rule['auto_create_fields']['id']) === 'yes' ? true : false, 'tags' => explode(',', get_option($rule['tags']['id'])), 'subscribers' => array('email' => $order->billing_email, 'phone_number' => $order->billing_phone, 'fields' => array(array('key' => 'Order.Number', 'value' => $order->get_order_number()), array('key' => 'Order.Date', 'value' => $order->order_date), array('key' => 'Order.FirstName', 'value' => $order->billing_first_name), array('key' => 'Order.LastName', 'value' => $order->billing_last_name), array('key' => 'Order.Street1', 'value' => $order->billing_address_1), array('key' => 'Order.Street2', 'value' => $order->billing_address_2), array('key' => 'Order.City', 'value' => $order->billing_city), array('key' => 'Order.Country', 'value' => $order->billing_country), array('key' => 'Order.State', 'value' => $order->billing_state), array('key' => 'Order.Subtotal', 'value' => $order_subtotal), array('key' => 'Order.Discount', 'value' => $order->cart_discount), array('key' => 'Order.Shipping', 'value' => $order->order_shipping + $order->order_shipping_tax), array('key' => 'Order.Total', 'value' => $order->order_total), array('key' => 'Order.Vat', 'value' => $order->order_tax)))); if (!empty($categories)) { $subscription['subscribers']['fields'][] = array('key' => 'Order.Categories', 'value' => $categories, 'type' => 'multiple'); } if (!empty($tags)) { $subscription['subscribers']['fields'][] = array('key' => 'Order.Tags', 'value' => array($tags), 'type' => 'multiple'); } if (!empty($brands)) { $subscription['subscribers']['fields'][] = array('key' => 'Order.Brands', 'value' => $brands, 'type' => 'multiple'); } $api = WP_RuleMailer_API::get_instance(); $api::subscribe($integration->api_url, $subscription); } } } }
function mysite_woocommerce_order_status_completed($order_id) { //Ajouter un suivi de pack lorsque la commande a été complété $order = new WC_Order($order_id); $items = $order->get_items(); $user = $order->get_user(); foreach ($items as $item) { if ($item["duree"]) { $pack_name = '--pack_name "'.$item["name"].'"'; $pack_type_id = ' --pack_type_id '.$item["item_meta"]["_product_id"][0]; $user_id = ' --user_id '.$user->ID; $nb_days = ' --nb_days "'.$item["duree"].'"'; $ebook = ''; if($item["item_meta"]["souhaitez-vous-ajouter-loption-e-book"][0] == 'Oui'){ $ebook = ' --ebook'; } $videos = ''; if ($item["item_meta"]["souhaitez-vous-ajouter-loption-videos"][0] == 'Oui') { $videos = ' --videos'; } $ebook_double = ''; if ($item["item_meta"]["souhaitez-vous-ajouter-loption-double-e-book"][0] == 'Oui') { $ebook_double = ' --ebook_double'; } $ebook_tips = ''; if ($item["item_meta"]["souhaitez-vous-ajouter-le-e-book-dastuces-et-menus-types"][0] == 'Oui') { $ebook_tips = ' --ebook_tips'; } $ebook_recipes = ''; if ($item["item_meta"]["souhaitez-vous-ajouter-le-e-book-de-recettes"][0] == 'Oui') { $ebook_recipes = ' --ebook_recipes'; } shell_exec(PATH_CMS.' costa:pack:start '.$pack_name.$pack_type_id.$user_id.$nb_days.$ebook.$videos.$ebook_double.$ebook_recipes.$ebook_tips); } } }
/** * Test: get_user */ function test_get_user() { $object = new WC_Order(); $this->assertEquals(false, $object->get_user()); $set_to = '1'; $object->set_customer_id($set_to); $this->assertInstanceOf('WP_User', $object->get_user()); }
/** * Get the user for an order * * @since 3.0.0 * @param \WC_Order $order * @return bool|WP_User */ public static function get_order_user(WC_Order $order) { if (self::is_wc_version_gte_2_2()) { return $order->get_user(); } else { return self::get_order_user_id($order) ? get_user_by('id', self::get_order_user_id($order)) : false; } }
function orderpost($orderId) { global $wpdb; $testMode = get_option('linksync_test'); $LAIDKey = get_option('linksync_laid'); $apicall = new linksync_class($LAIDKey, $testMode); //Checking for already sent Order $sentOrderIds = get_option('linksync_sent_order_id'); if (isset($sentOrderIds)) { if (!empty($sentOrderIds)) { $order_id_array = unserialize($sentOrderIds); } else { $order_id_array = array(); } if (!in_array($orderId, $order_id_array)) { $order = new WC_Order($orderId); if ($order->post_status == get_option('order_status_wc_to_vend')) { update_option('linksync_sent_order_id', serialize(array_merge($order_id_array, array($orderId)))); $order_no = $order->get_order_number(); if (strpos($order_no, '#') !== false) { $order_no = str_replace('#', '', $order_no); } $get_total = $order->get_total(); $get_user = $order->get_user(); $comments = $order->post->post_excerpt; $primary_email_address = $get_user->data->user_email; $currency = $order->get_order_currency(); $shipping_method = $order->get_shipping_method(); $order_total = $order->get_order_item_totals(); $transaction_id = $order->get_transaction_id(); $taxes_included = false; $total_discount = $order->get_total_discount(); $total_quantity = 0; $registerDb = get_option('wc_to_vend_register'); $vend_uid = get_option('wc_to_vend_user'); $total_tax = $order->get_total_tax(); // Geting Payment object details if (isset($order_total['payment_method']['value']) && !empty($order_total['payment_method']['value'])) { $wc_payment = get_option('wc_to_vend_payment'); if (isset($wc_payment) && !empty($wc_payment)) { $total_payments = explode(",", $wc_payment); foreach ($total_payments as $mapped_payment) { $exploded_mapped_payment = explode("|", $mapped_payment); if (isset($exploded_mapped_payment[1]) && !empty($exploded_mapped_payment[1]) && isset($exploded_mapped_payment[0]) && !empty($exploded_mapped_payment[0])) { if ($exploded_mapped_payment[1] == $order_total['payment_method']['value']) { $vend_payment_data = explode("%%", $exploded_mapped_payment[0]); if (isset($vend_payment_data[0])) { $payment_method = $vend_payment_data[0]; } if (isset($vend_payment_data[1])) { $payment_method_id = $vend_payment_data[1]; } break; } } } } $payment = array("retailer_payment_type_id" => isset($payment_method_id) ? $payment_method_id : null, "amount" => isset($get_total) ? $get_total : 0, "method" => isset($payment_method) ? $payment_method : null, "transactionNumber" => isset($transaction_id) ? $transaction_id : null); } $export_user_details = get_option('wc_to_vend_export'); if (isset($export_user_details) && !empty($export_user_details)) { if ($export_user_details == 'customer') { //woocommerce filter $billingAddress_filter = apply_filters('woocommerce_order_formatted_billing_address', array('firstName' => $order->billing_first_name, 'lastName' => $order->billing_last_name, 'phone' => $order->billing_phone, 'street1' => $order->billing_address_1, 'street2' => $order->billing_address_2, 'city' => $order->billing_city, 'state' => $order->billing_state, 'postalCode' => $order->billing_postcode, 'country' => $order->billing_country, 'company' => $order->billing_company, 'email_address' => $order->billing_email), $order); $billingAddress = array('firstName' => $billingAddress_filter['firstName'], 'lastName' => $billingAddress_filter['lastName'], 'phone' => $billingAddress_filter['phone'], 'street1' => $billingAddress_filter['street1'], 'street2' => $billingAddress_filter['street2'], 'city' => $billingAddress_filter['city'], 'state' => $billingAddress_filter['state'], 'postalCode' => $billingAddress_filter['postalCode'], 'country' => $billingAddress_filter['country'], 'company' => $billingAddress_filter['company'], 'email_address' => $billingAddress_filter['email_address']); $deliveryAddress_filter = apply_filters('woocommerce_order_formatted_shipping_address', array('firstName' => $order->shipping_first_name, 'lastName' => $order->shipping_last_name, 'phone' => $order->shipping_phone, 'street1' => $order->shipping_address_1, 'street2' => $order->shipping_address_2, 'city' => $order->shipping_city, 'state' => $order->shipping_state, 'postalCode' => $order->shipping_postcode, 'country' => $order->shipping_country, 'company' => $order->shipping_company), $order); $deliveryAddress = array('firstName' => $deliveryAddress_filter['firstName'], 'lastName' => $deliveryAddress_filter['lastName'], 'phone' => $deliveryAddress_filter['phone'], 'street1' => $deliveryAddress_filter['street1'], 'street2' => $deliveryAddress_filter['street2'], 'city' => $deliveryAddress_filter['city'], 'state' => $deliveryAddress_filter['state'], 'postalCode' => $deliveryAddress_filter['postalCode'], 'country' => $deliveryAddress_filter['country'], 'company' => $deliveryAddress_filter['company']); $primary_email = isset($primary_email_address) ? $primary_email_address : $billingAddress['email_address']; unset($billingAddress['email_address']); } } $vend_user_detail = get_option('wc_to_vend_user'); if (isset($vend_user_detail) && !empty($vend_user_detail)) { $user = explode('|', $vend_user_detail); $vend_uid = isset($user[0]) ? $user[0] : null; $vend_username = isset($user[1]) ? $user[1] : null; } //Ordered product(s) $items = $order->get_items(); $taxes = $order->get_taxes(); foreach ($items as $item) { foreach ($taxes as $tax_label) { $sql = mysql_query("SELECT tax_rate_id FROM `" . $wpdb->prefix . "woocommerce_tax_rates` WHERE tax_rate_name='" . $tax_label['label'] . "' AND tax_rate_class='" . $item['item_meta']['_tax_class'][0] . "'"); if (mysql_num_rows($sql) != 0) { $tax_classes = linksync_tax_classes($tax_label['label'], $item['item_meta']['_tax_class'][0]); if ($tax_classes['result'] == 'success') { $vend_taxes = explode('/', $tax_classes['tax_classes']); } } } $taxId = isset($vend_taxes[0]) ? $vend_taxes[0] : null; $taxName = isset($vend_taxes[1]) ? $vend_taxes[1] : null; $taxRate = isset($vend_taxes[2]) ? $vend_taxes[2] : null; if (isset($item['variation_id']) && !empty($item['variation_id'])) { $product_id = $item['variation_id']; } else { $product_id = $item['product_id']; } $pro_object = new WC_Product($product_id); $itemtotal = (double) $item['item_meta']['_line_subtotal'][0]; if (isset($item['line_subtotal']) && !empty($item['line_subtotal'])) { $product_amount = (double) ($item['line_subtotal'] / $item['qty']); } $discount = (double) $item['item_meta']['_line_subtotal'][0] - (double) $item['item_meta']['_line_total'][0]; if (isset($discount) && !empty($discount)) { $discount = (double) ($discount / $item['qty']); } #---------Changes--------# //Product Amount = product org amount - discount amount $product_total_amount = (double) $product_amount - (double) $discount; $product_sku = $pro_object->get_sku(); if (isset($product_total_amount) && isset($taxRate) && !empty($product_total_amount) && !empty($taxRate)) { $taxValue = $product_total_amount * $taxRate; } $products[] = array('sku' => $product_sku, 'title' => $item['name'], 'price' => $product_total_amount, 'quantity' => $item['qty'], 'discountAmount' => isset($discount) ? $discount : 0, 'taxName' => isset($taxName) ? $taxName : null, 'taxId' => isset($taxId) ? $taxId : null, 'taxRate' => isset($taxRate) ? $taxRate : null, 'taxValue' => isset($taxValue) ? $taxValue : null, 'discountTitle' => isset($discountTitle) ? $discountTitle : 'sale'); $total_quantity += $item['qty']; unset($taxId); unset($taxName); unset($taxRate); unset($taxValue); } #---------Discount-----# // if (isset($total_discount) && !empty($total_discount)) { // $taxes_Discount = $apicall->linksync_getTaxes(); // if (isset($taxes_Discount) && !empty($taxes_Discount)) { // if (!isset($taxes_Discount['errorCode'])) { // if (isset($taxes_Discount['taxes'])) { // foreach ($taxes_Discount['taxes'] as $select_tax) { // if ($select_tax['name'] == 'GST') { // $discountTaxName = $select_tax['name']; // $discountTaxId = $select_tax['id']; // $discountTaxRate = $select_tax['rate']; // } // } // } // } // } // if (isset($total_discount)) { // if (isset($discountTaxRate) && !empty($discountTaxRate)) { // $taxValue_discount = $discountTaxRate * $total_discount; // } // } // $products[] = array( // "price" => isset($total_discount) ? $total_discount : null, // "quantity" => 1, // "sku" => "vend-discount", // 'taxName' => isset($discountTaxName) ? $discountTaxName : null, // 'taxId' => isset($discountTaxId) ? $discountTaxId : null, // 'taxRate' => isset($discountTaxRate) ? $discountTaxRate : null, // 'taxValue' => isset($taxValue_discount) ? $taxValue_discount : null // ); // $products[] = array( // "price" => isset($total_discount) ? $total_discount : null, // "quantity" => 1, // "sku" => "vend-discount", // 'taxName' => null, // 'taxId' => null, // 'taxRate' => null, // 'taxValue' => null // ); // } #----------Shipping------------# foreach ($taxes as $tax_label) { if (isset($tax_label['shipping_tax_amount']) && !empty($tax_label['shipping_tax_amount'])) { $tax_classes = linksync_tax_classes($tax_label['label'], $item['item_meta']['_tax_class'][0]); if ($tax_classes['result'] == 'success') { $vend_taxes = explode('/', $tax_classes['tax_classes']); $taxId_shipping = isset($vend_taxes[0]) ? $vend_taxes[0] : null; $taxName_shipping = isset($vend_taxes[1]) ? $vend_taxes[1] : null; $taxRate_shipping = isset($vend_taxes[2]) ? $vend_taxes[2] : null; } } } if (isset($shipping_method) && !empty($shipping_method)) { $shipping_cost = $order->get_total_shipping(); $shipping_with_tax = $order->get_shipping_tax(); if ($shipping_with_tax > 0) { if (isset($shipping_cost) && isset($taxRate_shipping) && !empty($shipping_cost) && !empty($taxRate_shipping)) { $taxValue_shipping = $shipping_cost * $taxRate_shipping; } } $products[] = array("price" => isset($shipping_cost) ? $shipping_cost : null, "quantity" => 1, "sku" => "shipping", 'taxName' => isset($taxName_shipping) ? $taxName_shipping : null, 'taxId' => isset($taxId_shipping) ? $taxId_shipping : null, 'taxRate' => isset($taxRate_shipping) ? $taxRate_shipping : null, 'taxValue' => isset($taxValue_shipping) ? $taxValue_shipping : null); } //UTC Time date_default_timezone_set("UTC"); $order_created = date("Y-m-d H:i:s", time()); $OrderArray = array('uid' => isset($vend_uid) ? $vend_uid : null, 'created' => isset($order_created) ? $order_created : null, "orderId" => isset($order_no) ? $order_no : null, "source" => "WooCommerce", 'register_id' => isset($registerDb) ? $registerDb : null, 'user_name' => isset($vend_username) ? $vend_username : null, 'primary_email' => isset($primary_email) && !empty($primary_email) ? $primary_email : null, 'total' => isset($get_total) ? $get_total : 0, 'total_tax' => isset($total_tax) ? $total_tax : 0, 'comments' => isset($comments) ? $comments : null, 'taxes_included' => $taxes_included, 'currency' => isset($currency) ? $currency : 'USD', 'shipping_method' => isset($shipping_method) ? $shipping_method : null, 'payment' => isset($payment) && !empty($payment) ? $payment : null, 'products' => isset($products) && !empty($products) ? $products : null, 'payment_type_id' => isset($payment_method_id) ? $payment_method_id : null, 'billingAddress' => isset($billingAddress) && !empty($billingAddress) ? $billingAddress : null, 'deliveryAddress' => isset($deliveryAddress) && !empty($deliveryAddress) ? $deliveryAddress : null); $json = json_encode($OrderArray); $apicall->linksync_postOrder($json); linksync_class::add('Order Sync Woo to Vend', 'success', 'Woo Order no:' . $order_no, $LAIDKey); } } else { linksync_class::add('Order Sync Woo to Vend', 'Error', 'Already Sent Order', $LAIDKey); } } }
public function maybe_create_subscription($order_id) { if (empty(MP_Integration::$api_key)) { return; } $is_subscription = WC()->session->get('mp_is_sub'); if ($is_subscription) { update_post_meta($order_id, 'mp_order_type', 'new'); $renewal_frequency = WC()->session->get('mp_sub_freq'); update_post_meta($order_id, 'mp_frequency', $renewal_frequency); $order = new WC_Order($order_id); $customer = $order->get_user(); $order_items = $order->get_items(); $subscription_items = array(); foreach ($order_items as $item_id => $item) { $subscription_items[] = array('productId' => (int) $item['product_id'], 'variationId' => (int) $item['variation_id'], 'quantity' => (int) $item['qty'], 'discountPercent' => 0); } $first_name = $customer->first_name; if (empty($first_name)) { $first_name = $order->billing_first_name; } $last_name = $customer->last_name; if (empty($last_name)) { $last_name = $order->billing_last_name; } $shipping_methods = $order->get_shipping_methods(); $shipping_method_id = null; $shipping_method_name = null; $shipping_cost = 0; if (!empty($shipping_methods)) { $shipping_method_id = key($shipping_methods); $shipping_method = $shipping_methods[$shipping_method_id]; $shipping_method_name = $shipping_method['name']; $shipping_cost = $shipping_method['cost']; } $data = array('apiKey' => MP_Integration::$api_key, 'subscription' => array('renewalFrequencyId' => $renewal_frequency, 'shippingMethodId' => $shipping_method_id, 'shippingMethodName' => $shipping_method_name, 'shippingCost' => $shipping_cost), 'customer' => array('externalId' => $customer->ID, 'email' => $customer->user_email, 'firstName' => $first_name, 'lastName' => $last_name), 'order' => array('orderId' => $order_id, 'orderTypeId' => 'new', 'orderDate' => $order->order_date), 'subscriptionItems' => $subscription_items); $data_json = json_encode(array($data)); $ch = curl_init(MP_Environment::MP_URL . '/methods/api_CreateNewSubscription'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_json))); $subscription_id = null; $response = json_decode(curl_exec($ch)); if (curl_errno($ch)) { // TODO - error handling echo curl_error($ch); exit; } else { if (is_object($response)) { // TODO - error handling var_dump($response); exit; } else { $subscription_id = $response; } } curl_close($ch); if (!empty($subscription_id)) { update_post_meta($order_id, 'mp_subscription_id', $subscription_id); update_user_meta($customer->ID, 'mp_subscription_id', $subscription_id); } } }
public function order_delivery($order_id = 0) { if (!$order_id || !$this->settings['pickup_location'] || !$this->settings['pickup_name'] || !$this->settings['pickup_phone']) { return false; } // Get order object $order = new WC_Order($order_id); if (!$order) { return false; } // Get customer user ID $user = $order->get_user(); if (!$user) { return false; } $delivery_address = str_replace('<br/>', ', ', $order->get_formatted_shipping_address()); if (!$delivery_address) { return false; } if (!$order->billing_phone) { return false; } $delivery_address_data = $this->get_address_data($delivery_address); $args = array('pickup_address' => (string) $this->settings['pickup_location'], 'pickup_contact_name' => (string) $this->settings['pickup_name'], 'pickup_contact_phone' => (string) $this->settings['pickup_phone'], 'customer_identifier' => (string) $user->user_login, 'dropoff_contact_name' => (string) $user->display_name, 'dropoff_contact_phone' => (string) $order->billing_phone, 'dropoff_address' => (string) $delivery_address_data['address'], 'dropoff_coordinates' => (string) $delivery_address_data['location']); if ($this->settings['pickup_coords']) { $args['pickup_coordinates'] = (string) $this->settings['pickup_coords']; } if ($this->settings['pickup_remarks']) { $args['pickup_remarks'] = (string) $this->settings['pickup_remarks']; } if ($this->settings['city']) { $args['city'] = (string) $this->settings['city']; } $delivery_notes = $order->customer_note; if ($delivery_notes) { $args['dropoff_remarks'] = (string) $this->settings['delivery_notes']; } $delivery = $this->api('deliveries', $args, 'post'); if ($delivery && isset($delivery->id)) { update_post_meta($order_id, '_wumdrop_delivery_id', $delivery->id); if (isset($delivery->distance_estimate)) { update_post_meta($order_id, '_wumdrop_distance_estimate', $delivery->distance_estimate); } if (isset($delivery->time_estimate)) { update_post_meta($order_id, '_wumdrop_time_estimate', $delivery->time_estimate); } if (isset($delivery->message)) { update_post_meta($order_id, '_wumdrop_delivery_message', $delivery->message); } if (isset($delivery->time_estimate)) { update_post_meta($order_id, '_wumdrop_delivery_price', $delivery->price); } $order_note = __('WumDrop delivery order placed.', 'woocommerce-wumdrop'); if ('completed' != $order->get_status()) { $order->update_status('completed', $order_note); } else { $order->add_order_note($order_note); } return true; } return false; }