Commit 87dfb40f authored by Reinhold Kainhofer's avatar Reinhold Kainhofer

Compatibility with WC 3.0

Fixes #15
parent 5a314453
...@@ -217,17 +217,19 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { ...@@ -217,17 +217,19 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic {
**************************************************************/ **************************************************************/
public function setupAddressReplacements(&$reps, $prefix, $address, $nrtype) { public function setupAddressReplacements(&$reps, $prefix, $address, $nrtype) {
$reps["[email]"] = $address->billing_email; $wc_ver3 = !version_compare( WC_VERSION, '2.7', '<' );
$reps["[firstname]"] = $address->billing_first_name; $reps["[email]"] = $wc_ver3?$address->get_billing_email():$address->billing_email;
$reps["[lastname]"] = $address->billing_last_name; $reps["[firstname]"] = $wc_ver3?$address->get_billing_first_name():$address->billing_first_name;
$reps["[lastname]"] = $wc_ver3?$address->get_billing_last_name():$address->billing_last_name;
$reps["[company]"] = $address->billing_company; $reps["[company]"] = $wc_ver3?$address->get_billing_company():$address->billing_company;
$reps["[zip]"] = $address->billing_postcode; $postcode = $wc_ver3?$address->get_billing_postcode():$address->billing_postcode;
$reps["[postcode]"] = $address->billing_postcode; $reps["[zip]"] = $postcode;
$reps["[city]"] = $address->billing_city; $reps["[postcode]"] = $postcode;
$reps["[city]"] = $wc_ver3?$address->get_billing_city():$address->billing_city;
$country = $address->billing_country; $country = $wc_ver3?$address->get_billing_country():$address->billing_country;
$state = $address->billing_state; $state = $wc_ver3?$address->get_billing_state():$address->billing_state;
$allcountries = WC()->countries->get_countries(); $allcountries = WC()->countries->get_countries();
$states = WC()->countries->get_states($country); $states = WC()->countries->get_states($country);
$reps["[country]"] = $country; $reps["[country]"] = $country;
...@@ -241,14 +243,19 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { ...@@ -241,14 +243,19 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic {
} }
public function setupOrderReplacements (&$reps, $order, $nrtype) { public function setupOrderReplacements (&$reps, $order, $nrtype) {
$reps["[issuborder]"] = ($order->post->post_parent != 0); $wc_ver3 = !version_compare( WC_VERSION, '2.7', '<' );
$reps["[orderid]"] = $order->id; if ($wc_ver3) {
$reps["[issuborder]"] = $order->get_parent_id();
} else {
$reps["[issuborder]"] = $order->post->post_parent;
}
$reps["[orderid]"] = $wc_ver3?$order->get_id():$order->id;
if ($nrtype != 'ordernumber') { if ($nrtype != 'ordernumber') {
$reps["[ordernumber]"] = $order->get_order_number(); $reps["[ordernumber]"] = $order->get_order_number();
} }
$reps["[orderstatus]"] = $order->get_status(); $reps["[orderstatus]"] = $order->get_status();
$reps["[currency]"] = $order->get_order_currency(); $reps["[currency]"] = $wc_ver3?$order->get_currency():$order->get_order_currency();
$this->setupAddressReplacements($reps, "", $order, $nrtype); $this->setupAddressReplacements($reps, "", $order, $nrtype);
...@@ -275,10 +282,10 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { ...@@ -275,10 +282,10 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic {
foreach ($lineitems as $l) { foreach ($lineitems as $l) {
$p = $order->get_product_from_item($l); $p = $order->get_product_from_item($l);
$skus[$p->get_sku()] = 1; $skus[$p->get_sku()] = 1;
foreach (wc_get_product_terms( $p->id, 'product_cat') as $c) { foreach (wc_get_product_terms( $p->get_id(), 'product_cat') as $c) {
$categories[$c->slug] = 1; $categories[$c->slug] = 1;
} }
foreach (wc_get_product_terms( $p->id, 'product_tag') as $c) { foreach (wc_get_product_terms( $p->get_id(), 'product_tag') as $c) {
$tags[$c->slug] = 1; $tags[$c->slug] = 1;
} }
$shippingclasses[$p->get_shipping_class()] = 1; $shippingclasses[$p->get_shipping_class()] = 1;
...@@ -289,12 +296,14 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { ...@@ -289,12 +296,14 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic {
$vendor = $p->post->post_author; $vendor = $p->post->post_author;
$vnd = get_user_by('id', $vendor); // Get user name by user id $vnd = get_user_by('id', $vendor); // Get user name by user id
$vendors[] = $vendor; $vendors[] = $vendor;
$vendors[] = $vnd->user_login; if (isset($vnd->user_login)) {
$vendors[] = $vnd->user_login;
}
} }
// "WooThemes Vendor Products" support (vendors stored in its own taxonomy) // "WooThemes Vendor Products" support (vendors stored in its own taxonomy)
if (class_exists("WooCommerce_Product_Vendors") && function_exists("get_product_vendors")) { if (class_exists("WooCommerce_Product_Vendors") && function_exists("get_product_vendors")) {
foreach (get_product_vendors($p->id) as $vendor) { foreach (get_product_vendors($p->get_id()) as $vendor) {
$vendors[] = $vendor->slug; $vendors[] = $vendor->slug;
$vendors[] = $vendor->ID; $vendors[] = $vendor->ID;
} }
...@@ -302,7 +311,7 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { ...@@ -302,7 +311,7 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic {
// "YITH WooCommerce Multi Vendor" support (vendors stored in its own taxonomy) // "YITH WooCommerce Multi Vendor" support (vendors stored in its own taxonomy)
if (function_exists("yith_get_vendor")) { if (function_exists("yith_get_vendor")) {
$vendor = yith_get_vendor($p->id, 'product'); $vendor = yith_get_vendor($p->get_id(), 'product');
if ($vendor->is_valid()) { if ($vendor->is_valid()) {
$vendors[] = $vendor->slug; $vendors[] = $vendor->slug;
$vendors[] = $vendor->term_id; $vendors[] = $vendor->term_id;
...@@ -318,7 +327,8 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { ...@@ -318,7 +327,8 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic {
} }
public function setupUserReplacements (&$reps, $details, $nrtype) { public function setupUserReplacements (&$reps, $details, $nrtype) {
$reps["[ipaddress]"] = $details->customer_ip_address; $wc_ver3 = !version_compare( WC_VERSION, '2.7', '<' );
$reps["[ipaddress]"] = $wc_ver3?$details->get_customer_ip_address():$details->customer_ip_address;
$uid = $details->get_user_id(); $uid = $details->get_user_id();
$userinfo = get_userdata($uid); $userinfo = get_userdata($uid);
$reps["[userid]"] = $uid; $reps["[userid]"] = $uid;
...@@ -468,7 +478,12 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic { ...@@ -468,7 +478,12 @@ class OpenToolsOrdernumbers extends OpenToolsOrdernumbersBasic {
$number = $this->get_or_create_number($orderid, $order, $type); $number = $this->get_or_create_number($orderid, $order, $type);
// TODO: Store the invoice number counter in _wcpdf_invoice_number and the custom invoice // TODO: Store the invoice number counter in _wcpdf_invoice_number and the custom invoice
// number in the opentools meta, because the plugin assumes the number to be numeric... // number in the opentools meta, because the plugin assumes the number to be numeric...
update_post_meta( $orderid, '_wcpdf_invoice_number', $number ); if (version_compare(WC_VERSION, '2.7', '<')) {
update_post_meta($orderid, '_wcpdf_invoice_number', $number);
} else {
$order->update_meta_data('_wcpdf_invoice_number', $number);
$order->save_meta_data();
}
} }
} }
......
...@@ -375,7 +375,7 @@ class OpenToolsOrdernumbersBasic { ...@@ -375,7 +375,7 @@ class OpenToolsOrdernumbersBasic {
* Handle the "Assign a new order number" action from the edit order page in the backend * Handle the "Assign a new order number" action from the edit order page in the backend
*/ */
public function order_action_assign_new_ordernumber( $order ) { public function order_action_assign_new_ordernumber( $order ) {
$number = $this->generateNumber($order->id, $order, 'ordernumber'); $number = $this->generateNumber($order->get_id(), $order, 'ordernumber');
} }
/** /**
...@@ -429,7 +429,56 @@ class OpenToolsOrdernumbersBasic { ...@@ -429,7 +429,56 @@ class OpenToolsOrdernumbersBasic {
} }
/** ***********************************************************
*
* WRAPPER FUNCTIONS
*
**************************************************************/
/**
* Helper wrapper function to assist in the changed WC API:
* Versions <2.7 needed update_post_meta, while WC >3.0 use $order->update_meta_data
*/
function update_order_meta($order, $meta, $value) {
$wc_ver3 = !version_compare(WC_VERSION, '2.7', '<');
if (is_object($order)) {
$order_id = $wc_ver3?$order->get_id():$order->id;
} else {
$order_id = $order;
}
if ($wc_ver3) {
if (is_numeric($order)) {
$order = wc_get_order($order);
}
$order->update_meta_data($meta, $value);
$order->save_meta_data();
} else {
update_post_meta($order_id, $meta, $value);
}
}
/**
* Helper wrapper function to assist in the changed WC API:
* Versions <2.7 needed update_post_meta, while WC >3.0 use $order->update_meta_data
*/
function get_order_meta($order, $meta) {
$wc_ver3 = !version_compare(WC_VERSION, '2.7', '<');
if (is_object($order)) {
$order_id = $wc_ver3?$order->get_id():$order->id;
} else {
$order_id = $order;
}
if ($wc_ver3) {
if (is_numeric($order)) {
$order = wc_get_order($order);
}
return $order->get_meta( $meta, true );
} else {
return get_post_meta($order_id, $meta, true);
}
}
/** *********************************************************** /** ***********************************************************
* *
* REPLACEMENT FUNCTIONS * REPLACEMENT FUNCTIONS
...@@ -457,7 +506,7 @@ class OpenToolsOrdernumbersBasic { ...@@ -457,7 +506,7 @@ class OpenToolsOrdernumbersBasic {
$customvars = get_option ('ordernumber_variables', array()); $customvars = get_option ('ordernumber_variables', array());
$number = $this->helper->createNumber ($fmt, $type, $order, $customvars, $ctrsettings); $number = $this->helper->createNumber ($fmt, $type, $order, $customvars, $ctrsettings);
update_post_meta( $order->id, $this->ordernumber_meta.$type, $number ); $this->update_order_meta($order, $this->ordernumber_meta.$type, $number );
return $number; return $number;
} else { } else {
return $default; return $default;
...@@ -470,15 +519,15 @@ class OpenToolsOrdernumbersBasic { ...@@ -470,15 +519,15 @@ class OpenToolsOrdernumbersBasic {
function assign_new_ordernumber($orderid, $order, $update=true) { function assign_new_ordernumber($orderid, $order, $update=true) {
if ((!$update) /*&& ($order->post_status == 'auto-draft')*/) { if ((!$update) /*&& ($order->post_status == 'auto-draft')*/) {
// New order => assign placeholder, which will later be overwritten the real order number // New order => assign placeholder, which will later be overwritten the real order number
update_post_meta( $orderid, $this->ordernumber_meta.'ordernumber', $this->ordernumber_new_placeholder ); $this->update_order_meta($orderid, $this->ordernumber_meta.'ordernumber', $this->ordernumber_new_placeholder);
} }
// If we do not have an order (yet), we cannot proceed. But we probably have created the // If we do not have an order (yet), we cannot proceed. But we probably have created the
// ordernumber placeholder for that post, so this function has done its job and we can return // ordernumber placeholder for that post, so this function has done its job and we can return
if (!$order instanceof WC_Order) { if (!$order instanceof WC_Order) {
return; return;
} }
$number = get_post_meta( $orderid, $this->ordernumber_meta.'ordernumber', 'true'); $number = $this->get_order_meta( $order, $this->ordernumber_meta.'ordernumber');
if ($number == $this->ordernumber_new_placeholder && $order->post_status != 'auto-draft') { if ($number == $this->ordernumber_new_placeholder/* && $order->post_status != 'auto-draft'*/) {
$number = $this->generateNumber($orderid, $order, 'ordernumber'); $number = $this->generateNumber($orderid, $order, 'ordernumber');
// Assign a new number // Assign a new number
} }
...@@ -486,17 +535,17 @@ class OpenToolsOrdernumbersBasic { ...@@ -486,17 +535,17 @@ class OpenToolsOrdernumbersBasic {
} }
function get_or_create_number($default, $order, $type = 'ordernumber') { function get_or_create_number($default, $order, $type = 'ordernumber') {
$stored_number = get_post_meta( $order->id, $this->ordernumber_meta.$type, true); $stored_number = $this->get_order_meta( $order, $this->ordernumber_meta.$type);
if (!empty($stored_number)) { if (!empty($stored_number)) {
return $stored_number; return $stored_number;
} else { } else {
return $this->generateNumber($order->id, $order, $type); return $this->generateNumber($order->get_id(), $order, $type);
} }
} }
// Only retrieve the number, but do not create it if it doesn't exist: // Only retrieve the number, but do not create it if it doesn't exist:
function get_number($default, $order, $type = 'ordernumber') { function get_number($default, $order, $type = 'ordernumber') {
return get_post_meta( $order->id, $this->ordernumber_meta.$type, true); return $this->get_order_meta($order, $this->ordernumber_meta.$type);
} }
/** /**
...@@ -506,7 +555,7 @@ class OpenToolsOrdernumbersBasic { ...@@ -506,7 +555,7 @@ class OpenToolsOrdernumbersBasic {
*/ */
function get_ordernumber($orderid, $order) { function get_ordernumber($orderid, $order) {
$type = 'ordernumber'; $type = 'ordernumber';
$stored_number = get_post_meta( $orderid, $this->ordernumber_meta.$type, 'true'); $stored_number = $this->get_order_meta($order, $this->ordernumber_meta.$type);
if ($stored_number == $this->ordernumber_new_placeholder) { if ($stored_number == $this->ordernumber_new_placeholder) {
// Check whether the order was now really created => create order number now // Check whether the order was now really created => create order number now
return $this->assign_new_ordernumber($orderid, $order); return $this->assign_new_ordernumber($orderid, $order);
......
=== WooCommerce Basic Ordernumbers ===
Contributors: opentools
Tags: WooCommerce, Order numbers, orders
Requires at least: 4.0
Tested up to: 4.4
Stable tag: trunk
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl.html
Customize order numbers for WooCommerce. The order numbers can contain arbitrary text, a running counter and the current year.
== Description ==
The most flexible and complete solution for your WooCommerce webshop to customize your order numbers!
By default, WooCommerce uses the WordPress post ID of the order, which result in gaps between the order numbers. With this plugin you can configure the order numbers to have consecutive counters. Furthermore, the order number can contain the year, and the counter can be configured to reset each year.
The number format is a simple string, where # indicates the counter and [year] or [year2] indicate the year.
To get order numbers like "WC-2015-1", "WC-2015-2", etc., simply set the format to "WC-[year]-#".
The plugin comes in two flavors:
* This **free basic version**, which provides **sequential numbers** and allows the **year in the order number**
* The **paid advanced version**, with lots of **additional features**:
* Counter formatting: initial value, counter increments, number padding
* Lots of variables to be used in the formats
- date/time: year, month, day, hour, etc.
- address: customer country, zip, name, etc.
- order-specific: Number of articles, products, order total etc.
- product categories, shipping method
* Custom variable definitions (with conditions on available variables)
* Multiple concurrent counters (e.g. numbering per country, per day, per ZIP, ...)
* Different order numbers for free orders (e.g. "FREE-01" for free orders)
* Different number format for e.g. certain IP addresses (for testing)
* Different number format depending on products, product categories, shipping classes
* Customize invoice numbers (only for the "WooCommerce PDF Invoices and Package Slips" plugin)
For the full documentation of both the basic and the advanced ordernumbers plugin for WooCommerce, see:
http://open-tools.net/documentation/advanced-order-numbers-for-woocommerce.html
== Installation ==
1. To install the plugin, either:
1. use WordPress' plugin manager to find it in the WordPress plugin directory and directly install it from the WP plugin manager, or
1. use WordPress' plugin manager to upload the plugin's zip file.
1. After installation, activate the plugin through the 'Plugins' menu in WordPress
1. Enable the plugin's functionality in the WooCommerce settings (tab "Checkout" -> "Order numbers")
== Frequently Asked Questions ==
= How can I create nice order numbers for existing orders? =
This plugin is intended for future orders. You can, however, create order numbers for existing orders in the order view in the WordPress admin section. In the top right "Order Actions" box select "Assign a new order number" and click "Save Order". Notice, however, that this will create an order number as if the order was created at that very moment.
= What about invoice numbers? =
The Advanced Ordernumbers for WooCommerce plugin supports some invoicing plugins. This functionality is not available in the free version, though.
== Screenshots ==
1.
== Changelog ==
= 1.2 =
* Make random variable indicators case-insensitive
* First attempts at making the plugin multisite-enabled
= 1.0 =
* Initial release
== Upgrade Notice ==
To install the Advanced Ordernumbers for WooCommerce package, proceed as described in the Installation section.
No upgrades yet.
...@@ -79,6 +79,7 @@ The Advanced Ordernumbers for WooCommerce plugin supports some invoicing plugins ...@@ -79,6 +79,7 @@ The Advanced Ordernumbers for WooCommerce plugin supports some invoicing plugins
== Changelog == == Changelog ==
= 1.4.0 = = 1.4.0 =
* Compatibility with WooCommerce 3.0
* Add variables [isSubOrder] to allow admins to use different order number formats for suborders created by the YITH WC Multivendor plugin * Add variables [isSubOrder] to allow admins to use different order number formats for suborders created by the YITH WC Multivendor plugin
= 1.3.9 = = 1.3.9 =
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment