From e87d1521ddc04b94356d6d7e150a96c57c572366 Mon Sep 17 00:00:00 2001 From: Almira Krdzic Date: Sun, 2 Dec 2018 22:18:09 +0100 Subject: [PATCH 01/11] Fix assigment order issues --- .../class-wiaas-access-management.php | 56 ++++- .../wiaas/includes/class-wiaas-countries.php | 10 +- .../wiaas/includes/class-wiaas-pricing.php | 2 +- .../class-wiaas-order-fields.php | 12 + backend/app/plugins/wiaas/tests/bootstrap.php | 6 + .../tests/class-wiaas-unit-test-factory.php | 28 +++ .../class-wiaas-unit-test-order-factory.php | 139 +++++++++++ ...s-wiaas-unit-test-organization-factory.php | 70 ++++++ .../class-wiaas-unit-test-product-factory.php | 218 ++++++++++++++++++ .../package/test-wiaas-package-addon.php | 6 +- .../test-wiaas-package-option-groups.php | 10 +- ...atus.php => test-wiaas-package-status.php} | 48 ++-- .../package/test-wiaas-package-type.php | 2 +- .../pricing/test-wiaas-package-cl-pricing.php | 18 +- .../pricing/test-wiaas-package-pricing.php | 2 +- .../test-wiaas-access-management.php | 196 ++++++++++++++++ .../tests/unit-tests/test-wiaas-countries.php | 33 +-- .../tests/unit-tests/test-wiaas-order.php | 69 +----- .../tests/unit-tests/test-wiaas-pricing.php | 132 +++++------ .../tests/unit-tests/test-wiaas-templates.php | 9 +- .../test-wiaas-user-organization.php | 37 +-- .../wiaas/tests/wiaas-unit-test-case.php | 9 +- .../wiaas/tests/wiaas-unit-test-factory.php | 17 -- 23 files changed, 873 insertions(+), 256 deletions(-) create mode 100644 backend/app/plugins/wiaas/tests/class-wiaas-unit-test-factory.php create mode 100644 backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-order-factory.php create mode 100644 backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-organization-factory.php create mode 100644 backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-product-factory.php rename backend/app/plugins/wiaas/tests/unit-tests/package/{class-wiaas-package-status.php => test-wiaas-package-status.php} (71%) create mode 100644 backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-access-management.php delete mode 100644 backend/app/plugins/wiaas/tests/wiaas-unit-test-factory.php diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-access-management.php b/backend/app/plugins/wiaas/includes/class-wiaas-access-management.php index 4e9eb53..1e323dc 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-access-management.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-access-management.php @@ -16,8 +16,10 @@ class Wiaas_Access_Management { add_action( 'save_post', array( __CLASS__, 'maybe_handle_product_access' ), 999, 2 ); - add_action('woocommerce_new_order', array( __CLASS__, 'assign_order_to_organization' )); - add_action('woocommerce_payment_complete', array( __CLASS__, 'assign_order_to_suppliers'),20,1 ); + add_action('woocommerce_checkout_order_processed', array( __CLASS__, 'assign_order_to_customer_organization' )); + add_action('woocommerce_checkout_order_processed', array( __CLASS__, 'assign_order_to_commercial_lead_organization' )); + add_action('woocommerce_checkout_order_processed', array( __CLASS__, 'assign_order_to_supplier_organizations')); + add_action('wiaas_order_item_installation_assigned', array(__CLASS__, 'assign_order_to_installation_organization'), 10, 3); } /** @@ -43,6 +45,7 @@ class Wiaas_Access_Management { empty(Wiaas_Package_Pricing::get_package_prices($product))) { $access_group = Groups_Group::read_by_name('admin'); + } else { $access_group = Groups_Group::read_by_name('Registered'); @@ -64,13 +67,20 @@ class Wiaas_Access_Management { * * @param int $order_id */ - public static function assign_order_to_organization($order_id) { + public static function assign_order_to_customer_organization($order_id) { // assign order to customer organization $customer_id = wiaas_get_current_user_organization_id(); Wiaas_User_Organization::assign_post_to_organization($order_id, $customer_id); + } + + /** + * Assign order to commercial lead organization + * + * @param int $order_id + */ + public static function assign_order_to_commercial_lead_organization($order_id) { $order = wc_get_order($order_id); - // assign order to commercial lead organization $commercial_lead_id = absint($order->get_meta('_wiaas_commercial_lead_id', true)); if ($commercial_lead_id) { Wiaas_User_Organization::assign_post_to_organization($order_id, $commercial_lead_id); @@ -79,24 +89,46 @@ class Wiaas_Access_Management { } /** - * Assignees order to supplier organizations extracted from ordered items when order payment is complete. + * Assignees order to supplier organizations extracted from ordered items except installation + * + * Order will be assigned to corresponding installation company during delivery process * * @param int $order_id */ - public static function assign_order_to_suppliers($order_id){ + public static function assign_order_to_supplier_organizations($order_id) { - $order = wc_get_order($order_id); - $product_from_order = $order->get_items('line_item'); + $order = wc_get_order($order_id); - foreach ($product_from_order as $product_item) { + $order_items = $order->get_items('line_item'); - $supplier_organisation_id = Wiaas_Product_Supplier - ::get_supplier_organisation_id_from_product($product_item->get_product_id()); + foreach ($order_items as $key => $order_item) { - Wiaas_User_Organization::assign_post_to_organization($order_id, $supplier_organisation_id); + $supplier_organisation_id = $order_item->get_meta('_wiaas_supplier_organization_id'); + + if (! empty($supplier_organisation_id) && $order_item->get_meta('_wiaas_category') !== 'installation') { + + Wiaas_User_Organization::assign_post_to_organization($order_id, $supplier_organisation_id); + } } } + + /** + * Assign order to installation organization that handles installation of corresponding ordered bundle item + * + * @param WC_Order $order + * @param WC_Order_Item $bundle_item + * @param WC_Order_Item $installation_item + */ + public static function assign_order_to_installation_organization($order, $bundle_item, $installation_item) { + + $supplier_organisation_id = $installation_item->get_meta('_wiaas_supplier_organization_id'); + + if (! empty($supplier_organisation_id) ) { + + Wiaas_User_Organization::assign_post_to_organization($order->get_id(), $supplier_organisation_id); + } + } } Wiaas_Access_Management::init(); diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php index bf67897..7d2cbfe 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php @@ -175,15 +175,17 @@ class Wiaas_Countries { } $name = $choices[$code]; - $result = wp_insert_term($name, 'product_country'); + $result = wp_insert_term($name, 'product_country', array( + 'slug' => $code + )); if (is_wp_error($result)) { continue; } - update_term_meta($result->term_id, '_wiaas_country_code', $code); - update_term_meta($result->term_id, '_wiaas_country_currency', $info['currency']); - update_term_meta($result->term_id, '_wiaas_country_vat', $info['vat']); + update_term_meta($result['term_id'], '_wiaas_country_code', $code); + update_term_meta($result['term_id'], '_wiaas_country_currency', $info['currency']); + update_term_meta($result['term_id'], '_wiaas_country_vat', $info['vat']); } } diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php b/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php index 2555030..d50e3a0 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-pricing.php @@ -48,7 +48,7 @@ class Wiaas_Pricing { $bundled_items = $package->get_bundled_items(); foreach ($bundled_items as $bundled_item) { - $product = $bundled_item->product; + $product = wc_get_product($bundled_item->get_product_id()); $product_cat = Wiaas_Product_Category::get_category($product); if (!isset($total_cost_per_category[$product_cat])) { diff --git a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php index fea6b52..710e99e 100644 --- a/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php +++ b/backend/app/plugins/wiaas/includes/delivery-process/class-wiaas-order-fields.php @@ -228,6 +228,12 @@ class Wiaas_Order_Fields { $bundle_item->update_meta_data('_wiaas_installation', $selected_installation->get_id()); $bundle_item->save_meta_data(); + + /** + * Apply actions related to order item installation + * (ex. make order visible to corresponding installation company) + */ + do_action('wiaas_order_item_installation_assigned', $order, $bundle_item, $selected_installation); } } } @@ -448,6 +454,12 @@ class Wiaas_Order_Fields { $entry[(string) $field->id] = 'wiaas_installation_' . $order->get_id() . '|' . $installation_item->get_id(); + /** + * Apply actions related to order item installation + * (ex. make order visible to corresponding installation company) + */ + do_action('wiaas_order_item_installation_assigned', $order, $bundle_item, $installation_item); + } else if (count($installation_items) > 1) { // force admin to select installation diff --git a/backend/app/plugins/wiaas/tests/bootstrap.php b/backend/app/plugins/wiaas/tests/bootstrap.php index c6cc935..a33225e 100644 --- a/backend/app/plugins/wiaas/tests/bootstrap.php +++ b/backend/app/plugins/wiaas/tests/bootstrap.php @@ -25,6 +25,12 @@ tests_add_filter( 'pre_option_active_plugins', 'load_active_plugins_list'); # Start up the WP testing environment. require $_tests_dir . '/includes/bootstrap.php'; +// Require Wiaas test case factories +require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-organization-factory.php'; +require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-product-factory.php'; +require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-order-factory.php'; +require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/class-wiaas-unit-test-factory.php'; + # Require Wiaas Unit Test case class require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/wiaas-unit-test-case.php'; require_once '/tmp/wiaas-backend-test/app/plugins/wiaas/tests/wiaas-api-unit-test-case.php'; diff --git a/backend/app/plugins/wiaas/tests/class-wiaas-unit-test-factory.php b/backend/app/plugins/wiaas/tests/class-wiaas-unit-test-factory.php new file mode 100644 index 0000000..11a2ee6 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/class-wiaas-unit-test-factory.php @@ -0,0 +1,28 @@ +product = new Wiaas_Unit_Test_Product_Factory(); + + $this->organization = new Wiaas_Unit_Test_Organization_Factory(); + + $this->order = new Wiaas_Unit_Test_Order_Factory(); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-order-factory.php b/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-order-factory.php new file mode 100644 index 0000000..53f28cf --- /dev/null +++ b/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-order-factory.php @@ -0,0 +1,139 @@ + $customer_id + )); + + return $order; + } + + /** + * @param WC_Order|int $order + * @param int $customer_organization_id + */ + public function add_customer_organization_info($order, $customer_organization_id) { + + if (is_numeric($order)) { + + $order = wc_get_order($order); + } + + $order->add_meta_data('_wiaas_customer', $customer_organization_id); + + $order->add_meta_data('_wiaas_customer_info', wiaas_get_organization_info($customer_organization_id)); + + $order->save_meta_data(); + } + + + /** + * @param array $args + * @param WC_Order_Item_Product|null $bundle_item + * @param WC_Order|null $order + * + * @return WC_Order_Item_Product + */ + public function create_new_simple_product_order_item($args = array(), $bundle_item = null, $order = null) { + + $defaults = array( + 'quantity' => 1, + 'total' => 0 + ); + + $args = wp_parse_args($args, $defaults); + + $item = new WC_Order_Item_Product(); + + $item->set_props( + array( + 'quantity' => $args['quantity'], + 'total' => $args['total'], + ) + ); + + if (! empty($args['category']) ) { + + $item->add_meta_data('_wiaas_category', $args['category']); + } + + if (! empty($args['supplier_organization_id']) ) { + + $item->add_meta_data('_wiaas_supplier_organization_id', $args['supplier_organization_id']); + } + + $item->set_backorder_meta(); + + if (! empty($order) ) { + + $order->add_item($item); + + $order->save(); + } + + return $item; + } + + /** + * @param array $args + * @param WC_Order|null $order + * + * @return WC_Order_Item_Product + */ + public function create_new_bundle_order_item($args = array(), $order = null) { + + $defaults = array( + 'quantity' => 1, + 'total' => 0 + ); + + $args = wp_parse_args($args, $defaults); + + $item = new WC_Order_Item_Product(); + + $item->set_props( + array( + 'quantity' => $args['quantity'], + 'total' => $args['total'], + ) + ); + + $item->set_backorder_meta(); + + if (! empty($order) ) { + + $order->add_item($item); + + $order->save(); + } + + return $item; + } + + /** + * @param WC_Order $order + * @param array $items + */ + public function add_order_items($order, $items) { + + foreach ( $items as $item ) { + + $order->add_item($item); + } + + $order->save(); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-organization-factory.php b/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-organization-factory.php new file mode 100644 index 0000000..a483b15 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-organization-factory.php @@ -0,0 +1,70 @@ + 'organization' + ); + + $args = wp_parse_args($args, $defaults); + + $term_args = array(); + + if (! empty($args['parent_id'])) { + + $term_args['parent'] = $args['parent_id']; + } + + $result = wp_insert_term( + $args['name'], + Wiaas_User_Organization::TAXONOMY_NAME, + $term_args + ); + + if (! is_wp_error($result) ) { + + return $result['term_id']; + } + + return 0; + } + + public function assign_user_to_organization($user_id, $organization_id) { + wp_set_object_terms( + $user_id, + $organization_id, + Wiaas_User_Organization::TAXONOMY_NAME); + + update_user_meta($user_id, '_wiaas_organization_id', $organization_id); + } + + public function delete_organization($organization_id) { + wp_delete_term( + $organization_id, + Wiaas_User_Organization::TAXONOMY_NAME); + } + + public function delete_organizations() { + + $terms = get_terms(array( + 'taxonomy' => Wiaas_User_Organization::TAXONOMY_NAME, + 'hide_empty' => false + )); + + foreach ($terms as $term) { + + wp_delete_term($term->term_id, Wiaas_User_Organization::TAXONOMY_NAME); + } + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-product-factory.php b/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-product-factory.php new file mode 100644 index 0000000..d7a14b7 --- /dev/null +++ b/backend/app/plugins/wiaas/tests/factories/class-wiaas-unit-test-product-factory.php @@ -0,0 +1,218 @@ + 10, + 'is_recurring' => false, + 'pay_period' => 0 + ); + + $args = wp_parse_args($args, $defaults); + + $post_id = wp_insert_post(array( + 'post_type' => 'product', + 'post_status' => 'publish', + 'post_name' => 'product', + 'post_title' => 'Product', + 'post_content' => 'Product', + 'post_excerpt' => 'Product' + ), true); + + $product = new WC_Product_Simple($post_id); + + $this->set_product_price($product, $args['price'], $args['is_recurring'], $args['pay_period']); + + $product->save(); + + if (! empty($args['category']) ) { + + $this->assign_product_to_category($product->get_id(), $args['category']); + } + + if (! empty($args['country']) ) { + + $this->assign_product_to_country($product->get_id(), $args['country']); + } + + if (! empty($args['supplier']) ) { + + $this->assign_product_to_supplier($product->get_id(), $args['supplier']); + } + + return $product; + } + + /** + * @param WC_Product_Simple|int $product Product or Product ID + * @param int $price + * @param bool $is_recurring + * @param int $pay_period + */ + public function set_product_price($product, $price = 10, $is_recurring = false, $pay_period = 0) { + + if (is_numeric($product)) { + + $product = wc_get_product($product); + } + + Wiaas_Product_Pricing::set_product_price($product, $price, $is_recurring, $pay_period); + + $product->save(); + + delete_transient('wc_bundled_product_data'); + + + } + + /** + * @param WC_Product_Simple|int $product Product or Product ID + * @param string|int $category + */ + public function assign_product_to_category($product, $category) { + + if (is_numeric($product)) { + $product_id = $product; + } else { + $product_id = $product->get_id(); + } + + wp_add_object_terms($product_id, $category, 'product_cat'); + } + + /** + * @param WC_Product_Simple|int $product Product or Product ID + * @param string|int $country + */ + public function assign_product_to_country($product, $country) { + + if (is_numeric($product)) { + $product_id = $product; + } else { + $product_id = $product->get_id(); + } + + wp_add_object_terms($product_id, $country, 'product_country'); + } + + /** + * @param WC_Product_Simple|int $product Product or Product ID + * @param string|int $supplier + */ + public function assign_product_to_supplier($product, $supplier) { + + if (is_numeric($product)) { + $product_id = $product; + } else { + $product_id = $product->get_id(); + } + + wp_add_object_terms($product_id, $supplier, 'supplier'); + } + + /** + * @param array $args { + * @type array $products Simple products + * @type array $product_quantities Quantities for corresponding products from $products ( ex: [10, 5, 7] ) + * } + * + * @return WC_Product_Bundle + */ + public function create_product_bundle($args = array()) { + + $post_id = wp_insert_post(array( + 'post_type' => 'product', + 'post_status' => 'publish', + 'post_name' => 'Bundle', + 'post_title' => 'Product Bundle', + 'post_content' => 'Product Bundle', + 'post_excerpt' => 'Product Bundle' + ), true); + + $bundle = new WC_Product_Bundle($post_id); + + if (! empty($args['products']) ) { + + if ( empty($args['product_quantities']) ) { + + $args['product_quantities'] = array_fill(0, count($args['products']), 1); + } + + if ( count($args['product_quantities']) < count($args['products'])) { + + $args['product_quantities'] = array_pad($args['product_quantities'], count($args['products']), 1); + } + + $args['product_quantities'] = array_map('absint', $args['product_quantities']); + + $bundled_data = array(); + + foreach ($args['products'] as $index => $product) { + + $bundled_data[] = array( + 'product_id' => $product->get_id(), + 'quantity_min' => $args['product_quantities'][$index], + 'quantity_max' => $args['product_quantities'][$index], + 'priced_individually' => true + ); + } + + $bundle->set_bundled_data_items($bundled_data); + + $bundle->sync(true); + } + + $bundle->save(); + + if (! empty($args['country']) ) { + + $this->assign_product_to_country($bundle->get_id(), $args['country']); + } + + return $bundle; + } + + /** + * @param WC_Product_Bundle $bundle + * @param array $pricing_rules + * @param int $commission + * @param int $cost_margin + */ + public function set_product_bundle_prices($bundle, $pricing_rules = null, $commission = 50, $cost_margin = 0) { + + if ( empty($pricing_rules) ) { + + $pricing_rules = array( + 'purchase' => array( + 'minimal_fixed_price' => 500, + 'principal_amount' => 0, + 'minimal_services_price' => 0 + ) + ); + } + + Wiaas_Package_Pricing::set_package_prices($bundle, $pricing_rules, $commission, $cost_margin); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php index 5a289a2..6f26ef0 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-addon.php @@ -16,10 +16,10 @@ class Wiaas_Package_Addon_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Package_Addon::get_package_addons() */ function test_adding_package_addons() { - $package = $this->create_new_package(); + $package = $this->factory->product->create_product_bundle(); - $addon1 = $this->create_new_package(); - $addon2 = $this->create_new_package(); + $addon1 = $this->factory->product->create_product_bundle(); + $addon2 = $this->factory->product->create_product_bundle(); $addons_ids = array( $addon1->get_id(), $addon2->get_id() diff --git a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php index 448c5ad..4423b3e 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-option-groups.php @@ -16,12 +16,12 @@ class Wiaas_Package_Option_Groups_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Package_Option_Groups::get_package_option_groups() */ function test_adding_package_option_group() { - $package = $this->create_new_package(); + $package = $this->factory->product->create_product_bundle(); - $option_package1 = $this->create_new_package(); + $option_package1 = $this->factory->product->create_product_bundle(); Wiaas_Package_Type::set_package_type($option_package1->get_id(), 'option'); - $option_package2 = $this->create_new_package(); + $option_package2 = $this->factory->product->create_product_bundle(); Wiaas_Package_Type::set_package_type($option_package2->get_id(), 'option'); $option_group1 = array( @@ -79,9 +79,9 @@ class Wiaas_Package_Option_Groups_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Package_Option_Groups::get_group_name_for_package_option() */ function test_get_group_name_for_package_option() { - $package = $this->create_new_package(); + $package = $this->factory->product->create_product_bundle(); - $option_package = $this->create_new_package(); + $option_package = $this->factory->product->create_product_bundle(); Wiaas_Package_Type::set_package_type($option_package->get_id(), 'option'); $option_group = array( diff --git a/backend/app/plugins/wiaas/tests/unit-tests/package/class-wiaas-package-status.php b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-status.php similarity index 71% rename from backend/app/plugins/wiaas/tests/unit-tests/package/class-wiaas-package-status.php rename to backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-status.php index f722379..3ed02b0 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/package/class-wiaas-package-status.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-status.php @@ -16,6 +16,7 @@ class Wiaas__Package_Status_Test extends Wiaas_Unit_Test_Case { $this->assertNotEmpty($package_statuses); $this->assertContains(Wiaas_Package_Status::AVAILABLE, $package_statuses); + $this->assertContains(Wiaas_Package_Status::INVALID_MARGIN, $package_statuses); } /** @@ -23,7 +24,7 @@ class Wiaas__Package_Status_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Package_Type::get_package_status() */ function test_adding_package_status() { - $package = $this->create_new_package(); + $package = $this->factory->product->create_product_bundle(); Wiaas_Package_Status::set_package_status($package->get_id(), Wiaas_Package_Status::AVAILABLE); @@ -37,15 +38,15 @@ class Wiaas__Package_Status_Test extends Wiaas_Unit_Test_Case { * Test package status update on simple product price update */ function test_package_status_update_on_simple_product_price_update() { - $product1 = $this->create_new_product(20); - $this->add_product_category($product1, 'hardware'); - $product2 = $this->create_new_product(20); - $this->add_product_category($product2, 'software'); + $product1 = $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )); - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $product1, $product2)); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $product1, + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'software' )) + ) + )); $pricing_rules = array( 'purchase' => array( @@ -68,10 +69,13 @@ class Wiaas__Package_Status_Test extends Wiaas_Unit_Test_Case { $cost_margin = 100; Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin); - $this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::AVAILABLE); + $this->assertEquals(Wiaas_Package_Status::get_package_status($package->get_id()), Wiaas_Package_Status::AVAILABLE); - $product1->set_price(1000); - $this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::MARGIN_EXCEEDED); + $this->factory->product->set_product_price($product1, 1000); + + Wiaas_Package_Pricing::on_product_update($product1->get_id()); + + $this->assertEquals(Wiaas_Package_Status::get_package_status($package->get_id()), Wiaas_Package_Status::INVALID_MARGIN); } @@ -79,15 +83,12 @@ class Wiaas__Package_Status_Test extends Wiaas_Unit_Test_Case { * Test package status update on cost margin update */ function test_package_status_update_on_margin_cost_update() { - $product1 = $this->create_new_product(20); - $this->add_product_category($product1, 'hardware'); - - $product2 = $this->create_new_product(20); - $this->add_product_category($product2, 'software'); - - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $product1, $product2)); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'software' )) + ) + )); $pricing_rules = array( 'purchase' => array( @@ -108,13 +109,14 @@ class Wiaas__Package_Status_Test extends Wiaas_Unit_Test_Case { ); $commision = 50; $cost_margin = 0; - + Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin); - $this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::AVAILABLE); + $this->assertEquals(Wiaas_Package_Status::get_package_status($package->get_id()), Wiaas_Package_Status::AVAILABLE); $cost_margin = 1; Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin); - $this->assertEquals(Wiaas_Package_Status::get_package_status($package)->get_id(), Wiaas_Package_Status::MARGIN_EXCEEDED); + + $this->assertEquals(Wiaas_Package_Status::get_package_status($package->get_id()), Wiaas_Package_Status::INVALID_MARGIN); } } \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php index a2e5fd0..3c66c06 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/package/test-wiaas-package-type.php @@ -23,7 +23,7 @@ class Wiaas__Package_Type_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Package_Type::get_package_type() */ function test_adding_package_type() { - $package = $this->create_new_package(); + $package = $this->factory->product->create_product_bundle(); Wiaas_Package_Type::set_package_type($package->get_id(), 'standard'); diff --git a/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-cl-pricing.php b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-cl-pricing.php index 6dcfcf5..59849b0 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-cl-pricing.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-cl-pricing.php @@ -6,17 +6,17 @@ class Wiaas_Package_CL_Pricing_Test extends Wiaas_Unit_Test_Case { function setUp() { parent::setUp(); - $this->shop_owner_id = wp_insert_term( - 'Shop owner organization', - Wiaas_User_Organization::TAXONOMY_NAME)['term_id']; + $this->shop_owner_id = $this->factory->organization->create_new_organization( + array( 'name' => 'Shop owner organization' ) + ); - $this->customer_id = wp_insert_term( - 'Customer Organization', - Wiaas_User_Organization::TAXONOMY_NAME)['term_id']; + $this->customer_id = $this->factory->organization->create_new_organization( + array( 'name' => 'Customer Organization' ) + ); - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $this->create_new_product())); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( $this->factory->product->create_simple_product() ) + )); $this->package_id = $package->get_id(); diff --git a/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php index 3c73c3f..292daed 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/pricing/test-wiaas-package-pricing.php @@ -8,7 +8,7 @@ class Wiaas_Package_Pricing_Test extends Wiaas_Unit_Test_Case { */ function test_set_and_get_package_prices() { - $package = $this->create_new_package(); + $package = $this->factory->product->create_product_bundle(); $pricing_rules = array( 'purchase' => array( diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-access-management.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-access-management.php new file mode 100644 index 0000000..0cc93de --- /dev/null +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-access-management.php @@ -0,0 +1,196 @@ +customer_user_id = wp_insert_user(array( + 'user_login' => 'test_customer', + 'user_pass' => 'test', + 'user_email' => 'test_customer@mail.com', + 'role' => 'customer', + )); + + $this->organization_id = $this->factory->organization->create_new_organization(); + + $this->factory->organization->assign_user_to_organization($this->customer_user_id, $this->organization_id); + + $this->order = $this->factory->order->create_new_order(); + } + + function tearDown() { + parent::tearDown(); + + wp_set_current_user(1); + + wp_delete_user($this->customer_user_id); + + $this->factory->organization->delete_organizations(); + } + + /** + * @covers Wiaas_Access_Management::maybe_handle_product_access() + */ + function test_simple_product_has_admin_access() { + + $simple_product = $this->factory->product->create_simple_product(); + + Wiaas_Access_Management::maybe_handle_product_access($simple_product->get_id(), get_post($simple_product->get_id())); + + $access_group_ids = Groups_Post_Access::get_read_group_ids( $simple_product->get_id() ); + $this->assertEquals(1, count($access_group_ids)); + $admin_access_group = Groups_Group::read_by_name('admin'); + $this->assertEquals($admin_access_group->group_id, $access_group_ids[0]); + } + + /** + * @covers Wiaas_Access_Management::maybe_handle_product_access() + */ + function test_bundle_with_no_price_has_admin_access() { + + $bundle_product = $this->factory->product->create_product_bundle(); + + Wiaas_Access_Management::maybe_handle_product_access($bundle_product->get_id(), get_post($bundle_product->get_id())); + + $access_group_ids = Groups_Post_Access::get_read_group_ids( $bundle_product->get_id() ); + $this->assertEquals(1, count($access_group_ids)); + $admin_access_group = Groups_Group::read_by_name('admin'); + $this->assertEquals($admin_access_group->group_id, $access_group_ids[0]); + } + + /** + * @covers Wiaas_Access_Management::maybe_handle_product_access() + */ + function test_bundle_with_prices_has_registered_access() { + + $bundle_product = $this->factory->product->create_product_bundle(); + $this->factory->product->set_product_bundle_prices($bundle_product); + + Wiaas_Access_Management::maybe_handle_product_access($bundle_product->get_id(), get_post($bundle_product->get_id())); + + $access_group_ids = Groups_Post_Access::get_read_group_ids( $bundle_product->get_id() ); + $this->assertEquals(1, count($access_group_ids)); + $registered_access_group = Groups_Group::read_by_name('Registered'); + $this->assertEquals($registered_access_group->group_id, $access_group_ids[0]); + } + + /** + * @covers Wiaas_Access_Management::assign_order_to_customer_organization() + */ + function test_order_assigned_to_customer_organization() { + + wp_set_current_user($this->customer_user_id); + + Wiaas_Access_Management::assign_order_to_customer_organization($this->order->get_id()); + + $organization_access_group = Groups_Group::read_by_name('organization'); + $access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() ); + + $this->assertEquals(1, count($access_group_ids)); + $this->assertNotNull($access_group_ids[0]); + $this->assertEquals($organization_access_group->group_id, $access_group_ids[0]); + + } + + /** + * @covers Wiaas_Access_Management::assign_order_to_commercial_lead_organization() + */ + function test_order_assigned_to_commercial_lead_organization() { + + $this->order->update_meta_data('_wiaas_commercial_lead_id', $this->organization_id); + $this->order->save_meta_data(); + + Wiaas_Access_Management::assign_order_to_commercial_lead_organization($this->order->get_id()); + + $organization_access_group = Groups_Group::read_by_name('organization'); + $access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() ); + + $this->assertEquals(1, count($access_group_ids)); + $this->assertNotNull($access_group_ids[0]); + $this->assertEquals($organization_access_group->group_id, $access_group_ids[0]); + } + + /** + * @covers Wiaas_Access_Management::assign_order_to_supplier_organizations() + */ + function test_order_assigned_to_supplier_organizations() { + + $items = array(); + $items[] = $this->factory->order->create_new_simple_product_order_item(array( + 'category' => 'hardware', + 'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'hardware supplier')) + )); + $items[] = $this->factory->order->create_new_simple_product_order_item(array( + 'category' => 'software', + 'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'software supplier')) + )); + $items[] = $this->factory->order->create_new_simple_product_order_item(array( + 'category' => 'service', + 'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'service supplier')) + )); + $items[] = $this->factory->order->create_new_simple_product_order_item(array( + 'category' => 'installation', + 'supplier_organization_id' => $this->factory->organization->create_new_organization( array( 'name' => 'installation supplier')) + )); + + $this->factory->order->add_order_items($this->order, $items); + + Wiaas_Access_Management::assign_order_to_supplier_organizations($this->order->get_id()); + + $access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() ); + $this->assertEquals(3, count($access_group_ids)); + + $organization_access_group = Groups_Group::read_by_name('hardware supplier'); + $this->assertContains($organization_access_group->group_id, $access_group_ids); + + $organization_access_group = Groups_Group::read_by_name('software supplier'); + $this->assertContains($organization_access_group->group_id, $access_group_ids); + + $organization_access_group = Groups_Group::read_by_name('service supplier'); + $this->assertContains($organization_access_group->group_id, $access_group_ids); + + } + + /** + * @covers Wiaas_Access_Management::assign_order_to_installation_organization() + */ + function test_order_assigned_to_installation_organization() { + + $bundle_item = $this->factory->order->create_new_bundle_order_item(); + + $installation_item = $this->factory->order->create_new_simple_product_order_item(array( + 'category' => 'installation', + 'supplier_organization_id' => $this->organization_id + )); + + $this->order->add_item($bundle_item); + $this->order->add_item($installation_item); + + Wiaas_Access_Management::assign_order_to_installation_organization($this->order, $bundle_item, $installation_item); + + $organization_access_group = Groups_Group::read_by_name('organization'); + $access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order->get_id() ); + + $this->assertEquals(1, count($access_group_ids)); + $this->assertNotNull($access_group_ids[0]); + $this->assertEquals($organization_access_group->group_id, $access_group_ids[0]); + } +} \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php index be4b538..6b45bdc 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php @@ -5,7 +5,7 @@ */ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { - var $product, $package, $current_country = 'Sweden'; + var $product, $package, $current_country = 'se'; public function setUp() { parent::setUp(); @@ -13,14 +13,10 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { # set admin as current user wp_set_current_user(1); - $this->product = $this->create_new_product(); - wp_set_object_terms($this->product->get_id(), $this->current_country, 'product_country', false); - clean_object_term_cache( $this->product->get_id(), 'product_country' ); + $this->product = $this->factory->product->create_simple_product(array( 'country' => $this->current_country )); - $this->package = $this->create_new_package(); - wp_set_object_terms($this->package->get_id(), $this->current_country, 'product_country', false); - clean_object_term_cache( $this->package->get_id(), 'product_country' ); + $this->package = $this->factory->product->create_product_bundle(array( 'country' => $this->current_country )); } @@ -33,15 +29,20 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { $this->assertInstanceOf(WP_Taxonomy::class, $taxonomy); - $country_names = array_map(function($term) { - return $term->name; - }, get_terms(array( 'taxonomy' => 'product_country', 'hide_empty' => false ))); + $countries = get_terms( + array( + 'taxonomy' => 'product_country', + 'hide_empty' => false, + 'fields' => 'id=>slug' + ) + ); + $country_codes = array_values($countries); - $this->assertNotEmpty($country_names); + $this->assertNotEmpty($country_codes); - $this->assertContains('Sweden', $country_names); - $this->assertContains('Denmark', $country_names); - $this->assertContains('Finland', $country_names); + $this->assertContains('se', $country_codes); + $this->assertContains('fi', $country_codes); + $this->assertContains('dk', $country_codes); } /** @@ -53,7 +54,7 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { $this->assertNotNull($retrieved_country, 'Product has not country!'); - $this->assertEquals($retrieved_country['name'], $this->current_country, 'Retrieved product country is incorrect!'); + $this->assertEquals($retrieved_country['code'], $this->current_country, 'Retrieved product country is incorrect!'); } @@ -65,7 +66,7 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { $this->assertNotNull($retrieved_country, 'Package has not country!'); - $this->assertEquals($retrieved_country['name'], $this->current_country, 'Retrieved package country is incorrect!'); + $this->assertEquals($retrieved_country['code'], $this->current_country, 'Retrieved package country is incorrect!'); } } \ No newline at end of file diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php index a789e8c..1bcb655 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-order.php @@ -6,7 +6,7 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case { - var $customer_id, $customer_organization_id, $customer_organization_name, $order_id; + var $customer_id, $customer_organization_id, $customer_organization_name, $order; public function setUp() { parent::setUp(); @@ -23,26 +23,15 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case { $this->customer_organization_name = 'test-customer-organization'; # create customer organization - $this->customer_organization_id = wp_insert_term( - $this->customer_organization_name, - Wiaas_User_Organization::TAXONOMY_NAME - )['term_id']; + $this->customer_organization_id = $this->factory->organization->create_new_organization( + array( 'name' => $this->customer_organization_name ) + ); - update_user_meta($this->customer_id, '_wiaas_organization_id', $this->customer_organization_id); - - # add customer to organization - wp_set_terms_for_user( - $this->customer_id, - Wiaas_User_Organization::TAXONOMY_NAME, - [$this->customer_organization_name]); + $this->factory->organization->assign_user_to_organization($this->customer_id, $this->customer_organization_id); wp_set_current_user($this->customer_id); - $order = wc_create_order(array( - 'customer_id' => $this->customer_id - )); - - $this->order_id = $order->get_id(); + $this->order = $this->factory->order->create_new_order(); } function tearDown() { @@ -57,46 +46,6 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case { Wiaas_User_Organization::TAXONOMY_NAME); } - /** - * @covers Wiaas_Order::assign_order_to_organization() - */ - function test_order_assigned_to_customer_organization() { - - $organization_access_group = Groups_Group::read_by_name($this->customer_organization_name); - $access_group_ids = Groups_Post_Access::get_read_group_ids( $this->order_id ); - - $this->assertEquals(1, count($access_group_ids)); - $this->assertNotNull($access_group_ids[0]); - $this->assertEquals($organization_access_group->group_id, $access_group_ids[0]); - } - - /** - * @covers Wiaas_Order::check_order_access() - */ - function test_order_customer_can_access_order() { - - $has_access = Wiaas_Order::check_order_access(true, 'view', $this->order_id, 'shop_order'); - - $this->assertTrue($has_access); - } - - /** - * @covers Wiaas_Order::check_order_access() - */ - function test_customer_cannot_access_order_when_not_in_organization() { - $customer_id = wc_create_new_customer( - 'test_customer1@mail.com', - 'test_customer1', - 'test1'); - wp_set_current_user($customer_id); - - $this->assertTrue(Groups_Post_Access::handles_post_type('shop_order')); - - $has_access = Wiaas_Order::check_order_access(true, 'view', $this->order_id, 'shop_order'); - - $this->assertFalse($has_access); - } - /** * @covers Wiaas_Order::wiaas_prepare_rest_orders_query() */ @@ -139,10 +88,12 @@ class Wiaas_Order_Test extends Wiaas_Unit_Test_Case { 'line_items' => array() ); + $this->factory->order->add_customer_organization_info($this->order, $this->customer_organization_id); + $order_rest_response = Wiaas_Order::transform_rest_order( new WP_REST_Response($order_response), - wc_get_order($this->order_id), - array( 'id' => $this->order_id)); + $this->order, + array( 'id' => $this->order->get_id() )); $transformed_order_response = $order_rest_response->get_data(); diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php index a8d766c..6f81118 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-pricing.php @@ -3,15 +3,16 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { private function _create_package_to_sell() { - $product1 = $this->create_new_product(20); - $this->add_product_category($product1, 'hardware'); - - $product2 = $this->create_new_product(20, true, 2); - $this->add_product_category($product2, 'installation'); - - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $product1, $product2)); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + $this->factory->product->create_simple_product(array( + 'price' => 20, + 'is_recurring' => true, + 'pay_period' => 2, + 'category' => 'installation' )) + ) + )); $pricing_rules = array( 'purchase' => array( @@ -32,17 +33,16 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { ); $commision = 50; $cost_margin = 0; - Wiaas_Package_Pricing::set_package_prices($package, $pricing_rules, $commision, $cost_margin); - $customer_id = wp_create_term( - 'Customer', - Wiaas_User_Organization::TAXONOMY_NAME - )['term_id']; + $this->factory->product->set_product_bundle_prices($package, $pricing_rules, $commision, $cost_margin); - $commercial_lead_id = wp_create_term( - 'Commercial Lead', - Wiaas_User_Organization::TAXONOMY_NAME - )['term_id']; + $customer_id = $this->factory->organization->create_new_organization( + array( 'name' => 'Customer' ) + ); + + $commercial_lead_id = $this->factory->organization->create_new_organization( + array( 'name' => 'Commercial Lead' ) + ); self::_set_package_default_extras($commercial_lead_id, $package->get_id()); @@ -97,7 +97,7 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Pricing::get_product_total_cost() */ function test_get_fixed_product_total_cost() { - $product = $this->create_new_product(); + $product = $this->factory->product->create_simple_product(); $total_cost = Wiaas_Pricing::get_product_total_cost($product); @@ -108,7 +108,11 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Pricing::get_product_total_cost() */ function test_get_recurring_product_total_cost() { - $product = $this->create_new_product(10, true, 2); + $product = $this->factory->product->create_simple_product(array( + 'price' => 10, + 'is_recurring' => true, + 'pay_period' => 2 + )); $total_cost = Wiaas_Pricing::get_product_total_cost($product); @@ -119,15 +123,12 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Pricing::get_package_total_cost() */ function test_get_package_with_fixed_products_total_cost() { - $product1 = $this->create_new_product(20); - $this->add_product_category($product1, 'hardware'); - - $product2 = $this->create_new_product(20); - $this->add_product_category($product2, 'software'); - - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $product1, $product2)); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'software' )) + ) + )); $expected_total_price = 40; @@ -140,15 +141,12 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Pricing::get_package_total_cost() */ function test_get_package_with_recurring_products_total_cost() { - $product1 = $this->create_new_product(20); - $this->add_product_category($product1, 'hardware'); - - $product2 = $this->create_new_product(20, true, 2); - $this->add_product_category($product2, 'software'); - - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $product1, $product2)); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + $this->factory->product->create_simple_product(array( 'price' => 20, 'is_recurring' => true, 'pay_period' => 2, 'category' => 'software' )) + ) + )); $expected_total_price = 60; @@ -161,15 +159,12 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Pricing::get_package_total_cost() */ function test_package_with_single_installation_product_total_cost() { - $product1 = $this->create_new_product(20); - $this->add_product_category($product1, 'hardware'); - - $product2 = $this->create_new_product(20, true, 2); - $this->add_product_category($product2, 'installation'); - - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $product1, $product2)); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + $this->factory->product->create_simple_product(array( 'price' => 20, 'is_recurring' => true, 'pay_period' => 2, 'category' => 'installation' )) + ) + )); $expected_total_price = 60; @@ -182,20 +177,13 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { * @covers Wiaas_Pricing::get_package_total_cost() */ function test_package_with_multiple_installation_products_total_cost() { - $product1 = $this->create_new_product(20); - $this->add_product_category($product1, 'hardware'); - - $product2 = $this->create_new_product(10, true, 2); - $this->add_product_category($product2, 'installation'); - - $this->assertTrue(Wiaas_Product_Category::is_installation($product2)); - - $product3 = $this->create_new_product(20, true, 2); - $this->add_product_category($product3, 'installation'); - - $package = $this->create_new_package(); - - $this->add_products_to_package($package, array( $product1, $product2, $product3)); + $package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + $this->factory->product->create_simple_product(array( 'price' => 20, 'is_recurring' => true, 'pay_period' => 2, 'category' => 'installation' )), + $this->factory->product->create_simple_product(array( 'price' => 20, 'is_recurring' => true, 'pay_period' => 2, 'category' => 'installation' )) + ) + )); // price will be 20 + 20*2 , more expensive installation will be applied $expected_total_price = 60; @@ -246,12 +234,11 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { function test_get_addon_package_customer_price() { list( $package, $expected_prices, $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell(); - $addon_product = $this->create_new_product(20); - $this->add_product_category($addon_product, 'hardware'); - - $addon_package = $this->create_new_package(); - - $this->add_products_to_package($addon_package, array($addon_product)); + $addon_package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + ) + )); $pricing_rules = array( 'purchase' => array( @@ -330,12 +317,11 @@ class Wiaas_Pricing_Test extends Wiaas_Unit_Test_Case { function test_get_option_package_customer_price() { list( $package, $customer_id, $commercial_lead_id ) = $this->_create_package_to_sell(); - $option_product = $this->create_new_product(20); - $this->add_product_category($option_product, 'hardware'); - - $option_package = $this->create_new_package(); - - $this->add_products_to_package($option_package, array($option_product)); + $option_package = $this->factory->product->create_product_bundle(array( + 'products' => array( + $this->factory->product->create_simple_product(array( 'price' => 20, 'category' => 'hardware' )), + ) + )); $pricing_rules = array( 'purchase' => array( diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php index e3f008d..3ff3d18 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-templates.php @@ -19,11 +19,10 @@ class Wiaas_Templates_Test extends Wiaas_Unit_Test_Case { ); $this->template = $this->create_new_wiaas_template(); - $this->product = $this->create_new_product(); - $this->package = $this->create_new_package(); - $this->add_products_to_package($this->package, $this->product); - - + $this->product = $this->factory->product->create_simple_product(); + $this->package = $this->factory->product->create_product_bundle(array( + 'products' => $this->product + )); } public function test_template_category_taxonomy_created() { diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-user-organization.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-user-organization.php index 8bb9718..fada82a 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-user-organization.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-user-organization.php @@ -22,28 +22,18 @@ class Wiaas_User_Organization_Test extends Wiaas_Unit_Test_Case { $this->user_department_name = 'test_department'; - # create organization - $this->user_organization_id = wp_insert_term( - $this->user_organization_name, - Wiaas_User_Organization::TAXONOMY_NAME - )['term_id']; + // create organization + $this->user_organization_id = $this->factory->organization->create_new_organization( + array( 'name' => $this->user_organization_name ) + ); - update_user_meta($this->user_id, '_wiaas_organization_id', $this->user_organization_id); + // create department + $this->user_department_id = $this->factory->organization->create_new_organization( + array( 'name' => $this->user_department_name, 'parent_id' => $this->user_organization_id ) + ); - # create department - $this->user_department_id = wp_insert_term( - $this->user_department_name, - Wiaas_User_Organization::TAXONOMY_NAME, - array( - 'parent' => $this->user_organization_id - ) - )['term_id']; - - # assign user to organization - wp_set_terms_for_user( - $this->user_id, - Wiaas_User_Organization::TAXONOMY_NAME, - [$this->user_organization_name]); + // assign user to organization + $this->factory->organization->assign_user_to_organization($this->user_id, $this->user_organization_id); } function tearDown() { @@ -53,12 +43,7 @@ class Wiaas_User_Organization_Test extends Wiaas_Unit_Test_Case { wp_delete_user($this->user_id); - wp_delete_term( - $this->user_organization_id, - Wiaas_User_Organization::TAXONOMY_NAME); - wp_delete_term( - $this->user_department_id, - Wiaas_User_Organization::TAXONOMY_NAME); + $this->factory->organization->delete_organizations(); } /** diff --git a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php index f83ff95..a9ba184 100644 --- a/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php +++ b/backend/app/plugins/wiaas/tests/wiaas-unit-test-case.php @@ -2,13 +2,18 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { + public $factory; + /** * Executes any db migrations that are done to * `wp_options` table since it is deleted on every execution */ function setUp() { parent::setUp(); - wp_set_current_user(1); + + $this->factory = new Wiaas_Unit_Test_Factory(); + + wp_set_current_user(1); # Setup Gravity info since options table is deleted after each test gf_upgrade()->install(); @@ -28,6 +33,8 @@ class Wiaas_Unit_Test_Case extends WP_UnitTestCase { Wiaas_Order_Project::register_order_project_taxonomy(); Wiaas_Product_Supplier::register_supplier_taxonomy(); + + Wiaas_Package_Status::register_package_status_taxonomy(); define('WP_TEST_IN_PROGRESS',true); } diff --git a/backend/app/plugins/wiaas/tests/wiaas-unit-test-factory.php b/backend/app/plugins/wiaas/tests/wiaas-unit-test-factory.php deleted file mode 100644 index 4933eae..0000000 --- a/backend/app/plugins/wiaas/tests/wiaas-unit-test-factory.php +++ /dev/null @@ -1,17 +0,0 @@ - Date: Sun, 2 Dec 2018 22:59:20 +0100 Subject: [PATCH 02/11] Fix countries --- .../wiaas/includes/class-wiaas-countries.php | 4 +-- .../tests/unit-tests/test-wiaas-countries.php | 25 ++++++------------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php index 7d2cbfe..7d14ff3 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-countries.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-countries.php @@ -175,9 +175,7 @@ class Wiaas_Countries { } $name = $choices[$code]; - $result = wp_insert_term($name, 'product_country', array( - 'slug' => $code - )); + $result = wp_insert_term($name, 'product_country'); if (is_wp_error($result)) { continue; diff --git a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php index 6b45bdc..a352811 100644 --- a/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php +++ b/backend/app/plugins/wiaas/tests/unit-tests/test-wiaas-countries.php @@ -5,7 +5,7 @@ */ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { - var $product, $package, $current_country = 'se'; + var $product, $package; public function setUp() { parent::setUp(); @@ -13,10 +13,10 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { # set admin as current user wp_set_current_user(1); - $this->product = $this->factory->product->create_simple_product(array( 'country' => $this->current_country )); + $this->product = $this->factory->product->create_simple_product(array( 'country' => 'sweden' )); - $this->package = $this->factory->product->create_product_bundle(array( 'country' => $this->current_country )); + $this->package = $this->factory->product->create_product_bundle(array( 'country' => 'sweden' )); } @@ -25,20 +25,9 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { */ function test_available_countries_created() { // test taxonomy is available - $taxonomy = get_taxonomy('product_country'); + $countries = Wiaas_Countries::get_available_countries(); - $this->assertInstanceOf(WP_Taxonomy::class, $taxonomy); - - $countries = get_terms( - array( - 'taxonomy' => 'product_country', - 'hide_empty' => false, - 'fields' => 'id=>slug' - ) - ); - $country_codes = array_values($countries); - - $this->assertNotEmpty($country_codes); + $country_codes = wp_list_pluck($countries, 'code'); $this->assertContains('se', $country_codes); $this->assertContains('fi', $country_codes); @@ -54,7 +43,7 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { $this->assertNotNull($retrieved_country, 'Product has not country!'); - $this->assertEquals($retrieved_country['code'], $this->current_country, 'Retrieved product country is incorrect!'); + $this->assertEquals($retrieved_country['name'], 'Sweden', 'Retrieved product country is incorrect!'); } @@ -66,7 +55,7 @@ class Wiaas_Countries_Test extends Wiaas_Unit_Test_Case { $this->assertNotNull($retrieved_country, 'Package has not country!'); - $this->assertEquals($retrieved_country['code'], $this->current_country, 'Retrieved package country is incorrect!'); + $this->assertEquals($retrieved_country['name'], 'Sweden', 'Retrieved package country is incorrect!'); } } \ No newline at end of file From a5419e2f3caf7173c1e03ebcfec02cc6b6ff4517 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 22 Nov 2018 13:56:24 +0100 Subject: [PATCH 03/11] add custom columns to order list view --- .../admin/class-wiaas-admin-orders.php | 51 +++++++++++++++++++ .../wiaas/includes/class-wiaas-admin.php | 4 +- .../wiaas/includes/class-wiaas-order.php | 19 +++++++ 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php diff --git a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php new file mode 100644 index 0000000..e12eb30 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php @@ -0,0 +1,51 @@ +ID); + break; + + case 'commercial_lead': + $column_content = Wiaas_Order::get_order_commercial_lead_name($post->ID); + break; + + case 'customer': + $column_content = Wiaas_Order::get_order_customer_full_name($post->ID); + break; + } + + echo $column_content; + } + + +} + +Wiaas_Admin_Orders::init(); \ No newline at end of file diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-admin.php b/backend/app/plugins/wiaas/includes/class-wiaas-admin.php index 45cb271..c093770 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-admin.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-admin.php @@ -30,7 +30,9 @@ class Wiaas_Admin { require_once dirname(__FILE__) . '/admin/class-wiaas-admin-user-profile.php'; - require_once dirname(__FILE__) . '/admin/class-wiaas-admin-countries.php'; + require_once dirname(__FILE__) . '/admin/class-wiaas-admin-countries.php'; + + require_once dirname(__FILE__) . '/admin/class-wiaas-admin-orders.php'; add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 ); } diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-order.php b/backend/app/plugins/wiaas/includes/class-wiaas-order.php index f5042b9..e20aef6 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-order.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-order.php @@ -370,6 +370,25 @@ class Wiaas_Order { return $response; } + public static function get_order_customer_full_name($order_id){ + $order = wc_get_order($order_id); + + $customer_user_id = $order->get_customer_id(); + + $customer = get_userdata($customer_user_id); + + return $customer->last_name . ' ' . $customer->first_name; + } + + public static function get_order_commercial_lead_name($order_id){ + $order = wc_get_order($order_id); + + $commercial_lead_org_id = $order->get_meta('_wiaas_commercial_lead_id', true); + $commercial_lead_organization_info = wiaas_get_organization_info($commercial_lead_org_id); + + return $commercial_lead_organization_info['name']; + } + public static function set_order_vat($order_id, $vat_code) { add_post_meta($order_id, '_wiaas_vat_code', $vat_code); } From 434d0ab6b30510cf165dd3373e74d2184e7b8ec6 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Mon, 26 Nov 2018 22:45:56 +0100 Subject: [PATCH 04/11] add commercial lead name to order preview --- .../admin/class-wiaas-admin-orders.php | 9 + .../abstract-class-wc-admin-list-table.php | 276 ++++++ .../class-wc-admin-list-table-orders.php | 866 ++++++++++++++++++ backend/app/plugins/wiaas/wiaas.php | 2 + 4 files changed, 1153 insertions(+) create mode 100644 backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php create mode 100644 backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php diff --git a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php index e12eb30..962e60e 100644 --- a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php @@ -7,6 +7,8 @@ class Wiaas_Admin_Orders { add_filter( 'manage_edit-shop_order_columns', array(__CLASS__, 'add_additional_columns_to_orders_screen') ); add_action( 'manage_shop_order_posts_custom_column', array(__CLASS__, 'add_custom_columns_content') ); + + add_filter( 'woocommerce_admin_order_preview_get_order_details', array(__CLASS__, 'add_custom_data_to_order_preview') ); } public static function add_additional_columns_to_orders_screen( $columns ){ @@ -45,6 +47,13 @@ class Wiaas_Admin_Orders { echo $column_content; } + public static function add_custom_data_to_order_preview ($order){ + + $order['commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] );; + + return $order; + } + } diff --git a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php new file mode 100644 index 0000000..bd2f2a2 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php @@ -0,0 +1,276 @@ +list_table_type ) { + add_action( 'manage_posts_extra_tablenav', array( $this, 'maybe_render_blank_state' ) ); + add_filter( 'view_mode_post_types', array( $this, 'disable_view_mode' ) ); + add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) ); + add_filter( 'request', array( $this, 'request_query' ) ); + add_filter( 'post_row_actions', array( $this, 'row_actions' ), 100, 2 ); + add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 ); + add_filter( 'list_table_primary_column', array( $this, 'list_table_primary_column' ), 10, 2 ); + add_filter( 'manage_edit-' . $this->list_table_type . '_sortable_columns', array( $this, 'define_sortable_columns' ) ); + add_filter( 'manage_' . $this->list_table_type . '_posts_columns', array( $this, 'define_columns' ) ); + add_filter( 'bulk_actions-edit-' . $this->list_table_type, array( $this, 'define_bulk_actions' ) ); + add_action( 'manage_' . $this->list_table_type . '_posts_custom_column', array( $this, 'render_columns' ), 10, 2 ); + add_filter( 'handle_bulk_actions-edit-' . $this->list_table_type, array( $this, 'handle_bulk_actions' ), 10, 3 ); + } + } + + /** + * Show blank slate. + * + * @param string $which String which tablenav is being shown. + */ + public function maybe_render_blank_state( $which ) { + global $post_type; + + if ( $post_type === $this->list_table_type && 'bottom' === $which ) { + $counts = (array) wp_count_posts( $post_type ); + unset( $counts['auto-draft'] ); + $count = array_sum( $counts ); + + if ( 0 < $count ) { + return; + } + + $this->render_blank_state(); + + echo ''; + } + } + + /** + * Render blank state. Extend to add content. + */ + protected function render_blank_state() {} + + /** + * Removes this type from list of post types that support "View Mode" switching. + * View mode is seen on posts where you can switch between list or excerpt. Our post types don't support + * it, so we want to hide the useless UI from the screen options tab. + * + * @param array $post_types Array of post types supporting view mode. + * @return array Array of post types supporting view mode, without this type. + */ + public function disable_view_mode( $post_types ) { + unset( $post_types[ $this->list_table_type ] ); + return $post_types; + } + + /** + * See if we should render search filters or not. + */ + public function restrict_manage_posts() { + global $typenow; + + if ( $this->list_table_type === $typenow ) { + $this->render_filters(); + } + } + + /** + * Handle any filters. + * + * @param array $query_vars Query vars. + * @return array + */ + public function request_query( $query_vars ) { + global $typenow; + + if ( $this->list_table_type === $typenow ) { + return $this->query_filters( $query_vars ); + } + + return $query_vars; + } + + /** + * Render any custom filters and search inputs for the list table. + */ + protected function render_filters() {} + + /** + * Handle any custom filters. + * + * @param array $query_vars Query vars. + * @return array + */ + protected function query_filters( $query_vars ) { + return $query_vars; + } + + /** + * Set row actions. + * + * @param array $actions Array of actions. + * @param WP_Post $post Current post object. + * @return array + */ + public function row_actions( $actions, $post ) { + if ( $this->list_table_type === $post->post_type ) { + return $this->get_row_actions( $actions, $post ); + } + return $actions; + } + + /** + * Get row actions to show in the list table. + * + * @param array $actions Array of actions. + * @param WP_Post $post Current post object. + * @return array + */ + protected function get_row_actions( $actions, $post ) { + return $actions; + } + + /** + * Adjust which columns are displayed by default. + * + * @param array $hidden Current hidden columns. + * @param object $screen Current screen. + * @return array + */ + public function default_hidden_columns( $hidden, $screen ) { + if ( isset( $screen->id ) && 'edit-' . $this->list_table_type === $screen->id ) { + $hidden = array_merge( $hidden, $this->define_hidden_columns() ); + } + return $hidden; + } + + /** + * Set list table primary column. + * + * @param string $default Default value. + * @param string $screen_id Current screen ID. + * @return string + */ + public function list_table_primary_column( $default, $screen_id ) { + if ( 'edit-' . $this->list_table_type === $screen_id && $this->get_primary_column() ) { + return $this->get_primary_column(); + } + return $default; + } + + /** + * Define primary column. + * + * @return array + */ + protected function get_primary_column() { + return ''; + } + + /** + * Define hidden columns. + * + * @return array + */ + protected function define_hidden_columns() { + return array(); + } + + /** + * Define which columns are sortable. + * + * @param array $columns Existing columns. + * @return array + */ + public function define_sortable_columns( $columns ) { + return $columns; + } + + /** + * Define which columns to show on this screen. + * + * @param array $columns Existing columns. + * @return array + */ + public function define_columns( $columns ) { + return $columns; + } + + /** + * Define bulk actions. + * + * @param array $actions Existing actions. + * @return array + */ + public function define_bulk_actions( $actions ) { + return $actions; + } + + /** + * Pre-fetch any data for the row each column has access to it. + * + * @param int $post_id Post ID being shown. + */ + protected function prepare_row_data( $post_id ) {} + + /** + * Render individual columns. + * + * @param string $column Column ID to render. + * @param int $post_id Post ID being shown. + */ + public function render_columns( $column, $post_id ) { + $this->prepare_row_data( $post_id ); + + if ( ! $this->object ) { + return; + } + + if ( is_callable( array( $this, 'render_' . $column . '_column' ) ) ) { + $this->{"render_{$column}_column"}(); + } + } + + /** + * Handle bulk actions. + * + * @param string $redirect_to URL to redirect to. + * @param string $action Action name. + * @param array $ids List of ids. + * @return string + */ + public function handle_bulk_actions( $redirect_to, $action, $ids ) { + return esc_url_raw( $redirect_to ); + } +} diff --git a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php new file mode 100644 index 0000000..0214261 --- /dev/null +++ b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php @@ -0,0 +1,866 @@ +'; + echo '

' . esc_html__( 'When you receive a new order, it will appear here.', 'woocommerce' ) . '

'; + echo '' . esc_html__( 'Learn more about orders', 'woocommerce' ) . ''; + echo ''; + } + + /** + * Define primary column. + * + * @return string + */ + protected function get_primary_column() { + return 'order_number'; + } + + /** + * Get row actions to show in the list table. + * + * @param array $actions Array of actions. + * @param WP_Post $post Current post object. + * @return array + */ + protected function get_row_actions( $actions, $post ) { + return array(); + } + + /** + * Define hidden columns. + * + * @return array + */ + protected function define_hidden_columns() { + return array( + 'shipping_address', + 'billing_address', + 'wc_actions', + ); + } + + /** + * Define which columns are sortable. + * + * @param array $columns Existing columns. + * @return array + */ + public function define_sortable_columns( $columns ) { + $custom = array( + 'order_number' => 'ID', + 'order_total' => 'order_total', + 'order_date' => 'date', + ); + unset( $columns['comments'] ); + + return wp_parse_args( $custom, $columns ); + } + + /** + * Define which columns to show on this screen. + * + * @param array $columns Existing columns. + * @return array + */ + public function define_columns( $columns ) { + $show_columns = array(); + $show_columns['cb'] = $columns['cb']; + $show_columns['order_number'] = __( 'Order', 'woocommerce' ); + $show_columns['order_date'] = __( 'Date', 'woocommerce' ); + $show_columns['order_status'] = __( 'Status', 'woocommerce' ); + $show_columns['billing_address'] = __( 'Billing', 'woocommerce' ); + $show_columns['shipping_address'] = __( 'Ship to', 'woocommerce' ); + $show_columns['order_total'] = __( 'Total', 'woocommerce' ); + $show_columns['wc_actions'] = __( 'Actions', 'woocommerce' ); + + wp_enqueue_script( 'wc-orders' ); + + return $show_columns; + } + + /** + * Define bulk actions. + * + * @param array $actions Existing actions. + * @return array + */ + public function define_bulk_actions( $actions ) { + if ( isset( $actions['edit'] ) ) { + unset( $actions['edit'] ); + } + + $actions['mark_processing'] = __( 'Change status to processing', 'woocommerce' ); + $actions['mark_on-hold'] = __( 'Change status to on-hold', 'woocommerce' ); + $actions['mark_completed'] = __( 'Change status to completed', 'woocommerce' ); + $actions['remove_personal_data'] = __( 'Remove personal data', 'woocommerce' ); + + return $actions; + } + + /** + * Pre-fetch any data for the row each column has access to it. the_order global is there for bw compat. + * + * @param int $post_id Post ID being shown. + */ + protected function prepare_row_data( $post_id ) { + global $the_order; + + if ( empty( $this->object ) || $this->object->get_id() !== $post_id ) { + $this->object = wc_get_order( $post_id ); + $the_order = $this->object; + } + } + + /** + * Render columm: order_number. + */ + protected function render_order_number_column() { + $buyer = ''; + + if ( $this->object->get_billing_first_name() || $this->object->get_billing_last_name() ) { + /* translators: 1: first name 2: last name */ + $buyer = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $this->object->get_billing_first_name(), $this->object->get_billing_last_name() ) ); + } elseif ( $this->object->get_billing_company() ) { + $buyer = trim( $this->object->get_billing_company() ); + } elseif ( $this->object->get_customer_id() ) { + $user = get_user_by( 'id', $this->object->get_customer_id() ); + $buyer = ucwords( $user->display_name ); + } + + if ( $this->object->get_status() === 'trash' ) { + echo '#' . esc_attr( $this->object->get_order_number() ) . ' ' . esc_html( $buyer ) . ''; + } else { + echo '' . esc_html( __( 'Preview', 'woocommerce' ) ) . ''; + echo '#' . esc_attr( $this->object->get_order_number() ) . ' ' . esc_html( $buyer ) . ''; + } + } + + /** + * Render columm: order_status. + */ + protected function render_order_status_column() { + $tooltip = ''; + $comment_count = get_comment_count( $this->object->get_id() ); + $approved_comments_count = absint( $comment_count['approved'] ); + + if ( $approved_comments_count ) { + $latest_notes = wc_get_order_notes( + array( + 'order_id' => $this->object->get_id(), + 'limit' => 1, + 'orderby' => 'date_created_gmt', + ) + ); + + $latest_note = current( $latest_notes ); + + if ( isset( $latest_note->content ) && 1 === $approved_comments_count ) { + $tooltip = wc_sanitize_tooltip( $latest_note->content ); + } elseif ( isset( $latest_note->content ) ) { + /* translators: %d: notes count */ + $tooltip = wc_sanitize_tooltip( $latest_note->content . '
' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $approved_comments_count - 1 ), 'woocommerce' ), $approved_comments_count - 1 ) . '' ); + } else { + /* translators: %d: notes count */ + $tooltip = wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $approved_comments_count, 'woocommerce' ), $approved_comments_count ) ); + } + } + + if ( $tooltip ) { + printf( '%s', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), wp_kses_post( $tooltip ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) ); + } else { + printf( '%s', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) ); + } + } + + /** + * Render columm: order_date. + */ + protected function render_order_date_column() { + $order_timestamp = $this->object->get_date_created()->getTimestamp(); + + // Check if the order was created within the last 24 hours, and not in the future. + if ( $order_timestamp > strtotime( '-1 day', current_time( 'timestamp', true ) ) && $order_timestamp <= current_time( 'timestamp', true ) ) { + $show_date = sprintf( + /* translators: %s: human-readable time difference */ + _x( '%s ago', '%s = human-readable time difference', 'woocommerce' ), + human_time_diff( $this->object->get_date_created()->getTimestamp(), current_time( 'timestamp', true ) ) + ); + } else { + $show_date = $this->object->get_date_created()->date_i18n( apply_filters( 'woocommerce_admin_order_date_format', __( 'M j, Y', 'woocommerce' ) ) ); + } + printf( + '', + esc_attr( $this->object->get_date_created()->date( 'c' ) ), + esc_html( $this->object->get_date_created()->date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) ), + esc_html( $show_date ) + ); + } + + /** + * Render columm: order_total. + */ + protected function render_order_total_column() { + if ( $this->object->get_payment_method_title() ) { + /* translators: %s: method */ + echo '' . wp_kses_post( $this->object->get_formatted_order_total() ) . ''; + } else { + echo wp_kses_post( $this->object->get_formatted_order_total() ); + } + } + + /** + * Render columm: wc_actions. + */ + protected function render_wc_actions_column() { + echo '

'; + + do_action( 'woocommerce_admin_order_actions_start', $this->object ); + + $actions = array(); + + if ( $this->object->has_status( array( 'pending', 'on-hold' ) ) ) { + $actions['processing'] = array( + 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $this->object->get_id() ), 'woocommerce-mark-order-status' ), + 'name' => __( 'Processing', 'woocommerce' ), + 'action' => 'processing', + ); + } + + if ( $this->object->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) { + $actions['complete'] = array( + 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $this->object->get_id() ), 'woocommerce-mark-order-status' ), + 'name' => __( 'Complete', 'woocommerce' ), + 'action' => 'complete', + ); + } + + $actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $this->object ); + + echo wc_render_action_buttons( $actions ); // WPCS: XSS ok. + + do_action( 'woocommerce_admin_order_actions_end', $this->object ); + + echo '

'; + } + + /** + * Render columm: billing_address. + */ + protected function render_billing_address_column() { + $address = $this->object->get_formatted_billing_address(); + + if ( $address ) { + echo esc_html( preg_replace( '##i', ', ', $address ) ); + + if ( $this->object->get_payment_method() ) { + /* translators: %s: payment method */ + echo '' . sprintf( __( 'via %s', 'woocommerce' ), esc_html( $this->object->get_payment_method_title() ) ) . ''; // WPCS: XSS ok. + } + } else { + echo '–'; + } + } + + /** + * Render columm: shipping_address. + */ + protected function render_shipping_address_column() { + $address = $this->object->get_formatted_shipping_address(); + + if ( $address ) { + echo '' . esc_html( preg_replace( '##i', ', ', $address ) ) . ''; + if ( $this->object->get_shipping_method() ) { + /* translators: %s: shipping method */ + echo '' . sprintf( __( 'via %s', 'woocommerce' ), esc_html( $this->object->get_shipping_method() ) ) . ''; // WPCS: XSS ok. + } + } else { + echo '–'; + } + } + + /** + * Template for order preview. + * + * @since 3.3.0 + */ + public function order_preview_template() { + ?> + + get_items(), $order ); + $columns = apply_filters( + 'woocommerce_admin_order_preview_line_item_columns', array( + 'product' => __( 'Product', 'woocommerce' ), + 'quantity' => __( 'Quantity', 'woocommerce' ), + 'tax' => __( 'Tax', 'woocommerce' ), + 'total' => __( 'Total', 'woocommerce' ), + ), $order + ); + + if ( ! wc_tax_enabled() ) { + unset( $columns['tax'] ); + } + + $html = ' +
+ + + '; + + foreach ( $columns as $column => $label ) { + $html .= ''; + } + + $html .= ' + + + '; + + foreach ( $line_items as $item_id => $item ) { + + $product_object = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null; + $row_class = apply_filters( 'woocommerce_admin_html_order_preview_item_class', '', $item, $order ); + + $html .= ''; + + foreach ( $columns as $column => $label ) { + $html .= ''; + } + + $html .= ''; + } + + $html .= ' + +
' . esc_html( $label ) . '
'; + switch ( $column ) { + case 'product': + $html .= wp_kses_post( $item->get_name() ); + + if ( $product_object ) { + $html .= '
' . esc_html( $product_object->get_sku() ) . '
'; + } + + $meta_data = $item->get_formatted_meta_data( '' ); + + if ( $meta_data ) { + $html .= ''; + + foreach ( $meta_data as $meta_id => $meta ) { + if ( in_array( $meta->key, $hidden_order_itemmeta, true ) ) { + continue; + } + $html .= ''; + } + $html .= '
' . wp_kses_post( $meta->display_key ) . ':' . wp_kses_post( force_balance_tags( $meta->display_value ) ) . '
'; + } + break; + case 'quantity': + $html .= esc_html( $item->get_quantity() ); + break; + case 'tax': + $html .= wc_price( $item->get_total_tax(), array( 'currency' => $order->get_currency() ) ); + break; + case 'total': + $html .= wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) ); + break; + default: + $html .= apply_filters( 'woocommerce_admin_order_preview_line_item_column_' . sanitize_key( $column ), '', $item, $item_id, $order ); + break; + } + $html .= '
+
'; + + return $html; + } + + /** + * Get actions to display in the preview as HTML. + * + * @param WC_Order $order Order object. + * @return string + */ + public static function get_order_preview_actions_html( $order ) { + $actions = array(); + $status_actions = array(); + + if ( $order->has_status( array( 'pending' ) ) ) { + $status_actions['on-hold'] = array( + 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=on-hold&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ), + 'name' => __( 'On-hold', 'woocommerce' ), + 'title' => __( 'Change order status to on-hold', 'woocommerce' ), + 'action' => 'on-hold', + ); + } + + if ( $order->has_status( array( 'pending', 'on-hold' ) ) ) { + $status_actions['processing'] = array( + 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ), + 'name' => __( 'Processing', 'woocommerce' ), + 'title' => __( 'Change order status to processing', 'woocommerce' ), + 'action' => 'processing', + ); + } + + if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) { + $status_actions['complete'] = array( + 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ), + 'name' => __( 'Completed', 'woocommerce' ), + 'title' => __( 'Change order status to completed', 'woocommerce' ), + 'action' => 'complete', + ); + } + + if ( $status_actions ) { + $actions['status'] = array( + 'group' => __( 'Change status: ', 'woocommerce' ), + 'actions' => $status_actions, + ); + } + + return wc_render_action_buttons( apply_filters( 'woocommerce_admin_order_preview_actions', $actions, $order ) ); + } + + /** + * Get order details to send to the ajax endpoint for previews. + * + * @param WC_Order $order Order object. + * @return array + */ + public static function order_preview_get_order_details( $order ) { + if ( ! $order ) { + return array(); + } + + $payment_via = $order->get_payment_method_title(); + $payment_method = $order->get_payment_method(); + $payment_gateways = WC()->payment_gateways() ? WC()->payment_gateways->payment_gateways() : array(); + $transaction_id = $order->get_transaction_id(); + + if ( $transaction_id ) { + + $url = isset( $payment_gateways[ $payment_method ] ) ? $payment_gateways[ $payment_method ]->get_transaction_url( $order ) : false; + + if ( $url ) { + $payment_via .= ' (' . esc_html( $transaction_id ) . ')'; + } else { + $payment_via .= ' (' . esc_html( $transaction_id ) . ')'; + } + } + + $billing_address = $order->get_formatted_billing_address(); + $shipping_address = $order->get_formatted_shipping_address(); + + return apply_filters( + 'woocommerce_admin_order_preview_get_order_details', array( + 'data' => $order->get_data(), + 'order_number' => $order->get_order_number(), + 'item_html' => WC_Admin_List_Table_Orders::get_order_preview_item_html( $order ), + 'actions_html' => WC_Admin_List_Table_Orders::get_order_preview_actions_html( $order ), + 'ship_to_billing' => wc_ship_to_billing_address_only(), + 'needs_shipping' => $order->needs_shipping_address(), + 'formatted_billing_address' => $billing_address ? $billing_address : __( 'N/A', 'woocommerce' ), + 'formatted_shipping_address' => $shipping_address ? $shipping_address : __( 'N/A', 'woocommerce' ), + 'shipping_address_map_url' => $order->get_shipping_address_map_url(), + 'payment_via' => $payment_via, + 'shipping_via' => $order->get_shipping_method(), + 'status' => $order->get_status(), + 'status_name' => wc_get_order_status_name( $order->get_status() ), + ), $order + ); + } + + /** + * Handle bulk actions. + * + * @param string $redirect_to URL to redirect to. + * @param string $action Action name. + * @param array $ids List of ids. + * @return string + */ + public function handle_bulk_actions( $redirect_to, $action, $ids ) { + $ids = array_map( 'absint', $ids ); + $changed = 0; + + if ( 'remove_personal_data' === $action ) { + $report_action = 'removed_personal_data'; + + foreach ( $ids as $id ) { + $order = wc_get_order( $id ); + + if ( $order ) { + do_action( 'woocommerce_remove_order_personal_data', $order ); + $changed++; + } + } + } elseif ( false !== strpos( $action, 'mark_' ) ) { + $order_statuses = wc_get_order_statuses(); + $new_status = substr( $action, 5 ); // Get the status name from action. + $report_action = 'marked_' . $new_status; + + // Sanity check: bail out if this is actually not a status, or is not a registered status. + if ( isset( $order_statuses[ 'wc-' . $new_status ] ) ) { + // Initialize payment gateways in case order has hooked status transition actions. + wc()->payment_gateways(); + + foreach ( $ids as $id ) { + $order = wc_get_order( $id ); + $order->update_status( $new_status, __( 'Order status changed by bulk edit:', 'woocommerce' ), true ); + do_action( 'woocommerce_order_edit_status', $id, $new_status ); + $changed++; + } + } + } + + if ( $changed ) { + $redirect_to = add_query_arg( + array( + 'post_type' => $this->list_table_type, + 'bulk_action' => $report_action, + 'changed' => $changed, + 'ids' => join( ',', $ids ), + ), $redirect_to + ); + } + + return esc_url_raw( $redirect_to ); + } + + /** + * Show confirmation message that order status changed for number of orders. + */ + public function bulk_admin_notices() { + global $post_type, $pagenow; + + // Bail out if not on shop order list page. + if ( 'edit.php' !== $pagenow || 'shop_order' !== $post_type || ! isset( $_REQUEST['bulk_action'] ) ) { // WPCS: input var ok, CSRF ok. + return; + } + + $order_statuses = wc_get_order_statuses(); + $number = isset( $_REQUEST['changed'] ) ? absint( $_REQUEST['changed'] ) : 0; // WPCS: input var ok, CSRF ok. + $bulk_action = wc_clean( wp_unslash( $_REQUEST['bulk_action'] ) ); // WPCS: input var ok, CSRF ok. + + // Check if any status changes happened. + foreach ( $order_statuses as $slug => $name ) { + if ( 'marked_' . str_replace( 'wc-', '', $slug ) === $bulk_action ) { // WPCS: input var ok, CSRF ok. + /* translators: %d: orders count */ + $message = sprintf( _n( '%d order status changed.', '%d order statuses changed.', $number, 'woocommerce' ), number_format_i18n( $number ) ); + echo '

' . esc_html( $message ) . '

'; + break; + } + } + + if ( 'removed_personal_data' === $bulk_action ) { // WPCS: input var ok, CSRF ok. + /* translators: %d: orders count */ + $message = sprintf( _n( 'Removed personal data from %d order.', 'Removed personal data from %d orders.', $number, 'woocommerce' ), number_format_i18n( $number ) ); + echo '

' . esc_html( $message ) . '

'; + } + } + + /** + * See if we should render search filters or not. + */ + public function restrict_manage_posts() { + global $typenow; + + if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ), true ) ) { + $this->render_filters(); + } + } + + /** + * Render any custom filters and search inputs for the list table. + */ + protected function render_filters() { + $user_string = ''; + $user_id = ''; + + if ( ! empty( $_GET['_customer_user'] ) ) { // WPCS: input var ok. + $user_id = absint( $_GET['_customer_user'] ); // WPCS: input var ok, sanitization ok. + $user = get_user_by( 'id', $user_id ); + + $user_string = sprintf( + /* translators: 1: user display name 2: user ID 3: user email */ + esc_html__( '%1$s (#%2$s – %3$s)', 'woocommerce' ), + $user->display_name, + absint( $user->ID ), + $user->user_email + ); + } + ?> + + query_filters( $query_vars ); + } + + return $query_vars; + } + + /** + * Handle any custom filters. + * + * @param array $query_vars Query vars. + * @return array + */ + protected function query_filters( $query_vars ) { + global $wp_post_statuses; + + // Filter the orders by the posted customer. + if ( ! empty( $_GET['_customer_user'] ) ) { // WPCS: input var ok. + // @codingStandardsIgnoreStart + $query_vars['meta_query'] = array( + array( + 'key' => '_customer_user', + 'value' => (int) $_GET['_customer_user'], // WPCS: input var ok, sanitization ok. + 'compare' => '=', + ), + ); + // @codingStandardsIgnoreEnd + } + + // Sorting. + if ( isset( $query_vars['orderby'] ) ) { + if ( 'order_total' === $query_vars['orderby'] ) { + // @codingStandardsIgnoreStart + $query_vars = array_merge( $query_vars, array( + 'meta_key' => '_order_total', + 'orderby' => 'meta_value_num', + ) ); + // @codingStandardsIgnoreEnd + } + } + + // Status. + if ( ! isset( $query_vars['post_status'] ) ) { + $post_statuses = wc_get_order_statuses(); + + foreach ( $post_statuses as $status => $value ) { + if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) { + unset( $post_statuses[ $status ] ); + } + } + + $query_vars['post_status'] = array_keys( $post_statuses ); + } + return $query_vars; + } + + /** + * Change the label when searching orders. + * + * @param mixed $query Current search query. + * @return string + */ + public function search_label( $query ) { + global $pagenow, $typenow; + + if ( 'edit.php' !== $pagenow || 'shop_order' !== $typenow || ! get_query_var( 'shop_order_search' ) || ! isset( $_GET['s'] ) ) { // WPCS: input var ok. + return $query; + } + + return wc_clean( wp_unslash( $_GET['s'] ) ); // WPCS: input var ok, sanitization ok. + } + + /** + * Query vars for custom searches. + * + * @param mixed $public_query_vars Array of query vars. + * @return array + */ + public function add_custom_query_var( $public_query_vars ) { + $public_query_vars[] = 'shop_order_search'; + return $public_query_vars; + } + + /** + * Search custom fields as well as content. + * + * @param WP_Query $wp Query object. + */ + public function search_custom_fields( $wp ) { + global $pagenow; + + if ( 'edit.php' !== $pagenow || empty( $wp->query_vars['s'] ) || 'shop_order' !== $wp->query_vars['post_type'] || ! isset( $_GET['s'] ) ) { // WPCS: input var ok. + return; + } + + $post_ids = wc_order_search( wc_clean( wp_unslash( $_GET['s'] ) ) ); // WPCS: input var ok, sanitization ok. + + if ( ! empty( $post_ids ) ) { + // Remove "s" - we don't want to search order name. + unset( $wp->query_vars['s'] ); + + // so we know we're doing this. + $wp->query_vars['shop_order_search'] = true; + + // Search by found posts. + $wp->query_vars['post__in'] = array_merge( $post_ids, array( 0 ) ); + } + } +} diff --git a/backend/app/plugins/wiaas/wiaas.php b/backend/app/plugins/wiaas/wiaas.php index 582e624..67b74ca 100644 --- a/backend/app/plugins/wiaas/wiaas.php +++ b/backend/app/plugins/wiaas/wiaas.php @@ -61,6 +61,8 @@ include_once WIAAS_DIR . '/includes/wiaas-class-measurement-units.php'; include_once WIAAS_DIR . '/includes/class-wiaas-validation.php'; +include_once WIAAS_DIR . '/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php'; + function wiaas_redirect_to_login() { wp_safe_redirect(get_site_url('', 'wp-login.php')); } From 8b5ea356e702fa89b940d87224d9bfc1ee6437c8 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Tue, 27 Nov 2018 00:01:09 +0100 Subject: [PATCH 05/11] show item prices in order preview --- .../class-wc-admin-list-table-orders.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php index 0214261..15bbc30 100644 --- a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php @@ -505,7 +505,14 @@ class WC_Admin_List_Table_Orders extends Wiaas_Custom_WC_Admin_List_Table { $html .= wc_price( $item->get_total_tax(), array( 'currency' => $order->get_currency() ) ); break; case 'total': - $html .= wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) ); + $item_price = $item->get_meta('_wiaas_product_price'); + if ($item_price){ + $total_price = $item->get_quantity() * floatval($item_price); + }else{ + $total_price = $item->get_total(); + } + + $html .= wc_price( $total_price , array( 'currency' => $order->get_currency() ) ); break; default: $html .= apply_filters( 'woocommerce_admin_order_preview_line_item_column_' . sanitize_key( $column ), '', $item, $item_id, $order ); From 2f18e1e47c56514f0f31df0b1b1b3811ee229638 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Wed, 28 Nov 2018 00:04:47 +0100 Subject: [PATCH 06/11] remove obsolete info from order preview --- .../class-wc-admin-list-table-orders.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php index 15bbc30..8fbe0d6 100644 --- a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php @@ -358,7 +358,7 @@ class WC_Admin_List_Table_Orders extends Wiaas_Custom_WC_Admin_List_Table {

{{{ data.formatted_billing_address }}} - {{data}} + <# if ( data.data.billing.email ) { #> {{ data.data.billing.email }} From 00a1ef7da5254c6fa972684fa74fc042e0e4da34 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 29 Nov 2018 05:01:51 +0100 Subject: [PATCH 07/11] add order payment details to order preview --- .../admin/class-wiaas-admin-orders.php | 84 +++++++++++++++++++ .../class-wc-admin-list-table-orders.php | 11 +-- 2 files changed, 86 insertions(+), 9 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php index 962e60e..c48369c 100644 --- a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php @@ -9,6 +9,31 @@ class Wiaas_Admin_Orders { add_action( 'manage_shop_order_posts_custom_column', array(__CLASS__, 'add_custom_columns_content') ); add_filter( 'woocommerce_admin_order_preview_get_order_details', array(__CLASS__, 'add_custom_data_to_order_preview') ); + + add_filter( 'woocommerce_admin_order_preview_line_items', array(__CLASS__, 'remove_simple_items_from_preview')); + + + add_action('init', array(__CLASS__, 'init_admin')); + + } + + public static function init_admin(){ + $current_user = wp_get_current_user(); + $role = $current_user->roles[0]; + $is_admin = $role === 'administrator'; + + if ($is_admin){ + add_filter( 'woocommerce_admin_order_preview_line_item_columns', array(__CLASS__, 'customize_order_preview_columns')); + + add_filter( 'woocommerce_admin_order_preview_line_item_column_payment_type', array(__CLASS__, 'fill_in_payment_type'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_services_extra', array(__CLASS__, 'fill_in_services_extra'), 10, 4); + add_filter( 'woocommerce_admin_order_preview_line_item_column_services_contract_period', array(__CLASS__, 'fill_in_services_contract_period'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_max_contract_period', array(__CLASS__, 'fill_in_max_contract_period'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_period_unit', array(__CLASS__, 'fill_in_period_unit'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_recurrent_extra', array(__CLASS__, 'fill_in_recurrent_extra'), 10, 4); + add_filter( 'woocommerce_admin_order_preview_line_item_column_pay_period', array(__CLASS__, 'fill_in_pay_period'), 10, 2); + + } } public static function add_additional_columns_to_orders_screen( $columns ){ @@ -54,6 +79,65 @@ class Wiaas_Admin_Orders { return $order; } + public static function remove_simple_items_from_preview( $order_items){ + + $items = array(); + + foreach ($order_items as $order_item) { + if (isset($order_item['wiaas_standard_package'])) { + $items[] = $order_item; + } + } + + return $items; + } + + public static function customize_order_preview_columns( $columns ){ + + unset($columns['total']); + + $columns['payment_type'] = __( 'Payment type', 'wiaas' ); + $columns['services_extra'] = __( 'Services extra', 'wiaas'); + $columns['recurrent_extra'] = __( 'Recurrent extra', 'wiaas'); + $columns['services_contract_period'] = __( 'Contract period', 'wiaas'); + $columns['max_contract_period'] = __( 'Maximum contract period', 'wiaas'); + $columns['pay_period'] = __( 'Pay period', 'wiaas'); + $columns['period_unit'] = __( 'Period unit', 'wiaas'); + + $columns['total'] = __( 'Total', 'woocommerce'); + + return $columns; + + } + + public static function fill_in_payment_type ( $empty , $item){ + return $item->get_meta('_wiaas_payment_type'); + } + + public static function fill_in_services_extra ($empty, $item, $item_id, $order){ + return wc_price( $item->get_meta('_wiaas_services_extra'), array( 'currency' => $order->get_currency() ) ); + } + + public static function fill_in_services_contract_period($empty, $item){ + return $item->get_meta('_wiaas_service_contract_period'); + } + + public static function fill_in_max_contract_period($empty, $item){ + return $item->get_meta('_wiaas_max_contract_period'); + } + + public static function fill_in_period_unit ($empty, $item){ + return $item->get_meta('_wiaas_period_unit'); + } + + public static function fill_in_recurrent_extra($empty, $item, $item_id, $order){ + return wc_price( $item->get_meta('_wiaas_recurrent_extra'), array( 'currency' => $order->get_currency() ) ); + } + + public static function fill_in_pay_period($empty, $item){ + return $item->get_meta('_wiaas_pay_period'); + } + } diff --git a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php index 8fbe0d6..5c27f0a 100644 --- a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php @@ -358,7 +358,7 @@ class WC_Admin_List_Table_Orders extends Wiaas_Custom_WC_Admin_List_Table {

{{{ data.formatted_billing_address }}} - + <# if ( data.data.billing.email ) { #> {{ data.data.billing.email }} @@ -505,14 +505,7 @@ class WC_Admin_List_Table_Orders extends Wiaas_Custom_WC_Admin_List_Table { $html .= wc_price( $item->get_total_tax(), array( 'currency' => $order->get_currency() ) ); break; case 'total': - $item_price = $item->get_meta('_wiaas_product_price'); - if ($item_price){ - $total_price = $item->get_quantity() * floatval($item_price); - }else{ - $total_price = $item->get_total(); - } - - $html .= wc_price( $total_price , array( 'currency' => $order->get_currency() ) ); + $html .= wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) ); break; default: $html .= apply_filters( 'woocommerce_admin_order_preview_line_item_column_' . sanitize_key( $column ), '', $item, $item_id, $order ); From 29fb07205e879db9462b7f7fe3017e21fd23b165 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 29 Nov 2018 12:44:12 +0100 Subject: [PATCH 08/11] Remove custom class --- .../admin/class-wiaas-admin-orders.php | 14 +- .../abstract-class-wc-admin-list-table.php | 276 ------ .../class-wc-admin-list-table-orders.php | 866 ------------------ backend/app/plugins/wiaas/wiaas.php | 1 - 4 files changed, 13 insertions(+), 1144 deletions(-) delete mode 100644 backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php delete mode 100644 backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php diff --git a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php index c48369c..bb45ecf 100644 --- a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php @@ -10,6 +10,8 @@ class Wiaas_Admin_Orders { add_filter( 'woocommerce_admin_order_preview_get_order_details', array(__CLASS__, 'add_custom_data_to_order_preview') ); + add_filter( 'woocommerce_admin_order_preview_start', array(__CLASS__, 'show_custom_data_before_order_preview') ); + add_filter( 'woocommerce_admin_order_preview_line_items', array(__CLASS__, 'remove_simple_items_from_preview')); @@ -74,11 +76,21 @@ class Wiaas_Admin_Orders { public static function add_custom_data_to_order_preview ($order){ - $order['commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] );; + $order['commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] ); return $order; } + public static function show_custom_data_before_order_preview () { + + echo '
+
+

Commercial lead

+ {{data.commercial_lead_name}} +
+
'; + } + public static function remove_simple_items_from_preview( $order_items){ $items = array(); diff --git a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php deleted file mode 100644 index bd2f2a2..0000000 --- a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/abstract-class-wc-admin-list-table.php +++ /dev/null @@ -1,276 +0,0 @@ -list_table_type ) { - add_action( 'manage_posts_extra_tablenav', array( $this, 'maybe_render_blank_state' ) ); - add_filter( 'view_mode_post_types', array( $this, 'disable_view_mode' ) ); - add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) ); - add_filter( 'request', array( $this, 'request_query' ) ); - add_filter( 'post_row_actions', array( $this, 'row_actions' ), 100, 2 ); - add_filter( 'default_hidden_columns', array( $this, 'default_hidden_columns' ), 10, 2 ); - add_filter( 'list_table_primary_column', array( $this, 'list_table_primary_column' ), 10, 2 ); - add_filter( 'manage_edit-' . $this->list_table_type . '_sortable_columns', array( $this, 'define_sortable_columns' ) ); - add_filter( 'manage_' . $this->list_table_type . '_posts_columns', array( $this, 'define_columns' ) ); - add_filter( 'bulk_actions-edit-' . $this->list_table_type, array( $this, 'define_bulk_actions' ) ); - add_action( 'manage_' . $this->list_table_type . '_posts_custom_column', array( $this, 'render_columns' ), 10, 2 ); - add_filter( 'handle_bulk_actions-edit-' . $this->list_table_type, array( $this, 'handle_bulk_actions' ), 10, 3 ); - } - } - - /** - * Show blank slate. - * - * @param string $which String which tablenav is being shown. - */ - public function maybe_render_blank_state( $which ) { - global $post_type; - - if ( $post_type === $this->list_table_type && 'bottom' === $which ) { - $counts = (array) wp_count_posts( $post_type ); - unset( $counts['auto-draft'] ); - $count = array_sum( $counts ); - - if ( 0 < $count ) { - return; - } - - $this->render_blank_state(); - - echo ''; - } - } - - /** - * Render blank state. Extend to add content. - */ - protected function render_blank_state() {} - - /** - * Removes this type from list of post types that support "View Mode" switching. - * View mode is seen on posts where you can switch between list or excerpt. Our post types don't support - * it, so we want to hide the useless UI from the screen options tab. - * - * @param array $post_types Array of post types supporting view mode. - * @return array Array of post types supporting view mode, without this type. - */ - public function disable_view_mode( $post_types ) { - unset( $post_types[ $this->list_table_type ] ); - return $post_types; - } - - /** - * See if we should render search filters or not. - */ - public function restrict_manage_posts() { - global $typenow; - - if ( $this->list_table_type === $typenow ) { - $this->render_filters(); - } - } - - /** - * Handle any filters. - * - * @param array $query_vars Query vars. - * @return array - */ - public function request_query( $query_vars ) { - global $typenow; - - if ( $this->list_table_type === $typenow ) { - return $this->query_filters( $query_vars ); - } - - return $query_vars; - } - - /** - * Render any custom filters and search inputs for the list table. - */ - protected function render_filters() {} - - /** - * Handle any custom filters. - * - * @param array $query_vars Query vars. - * @return array - */ - protected function query_filters( $query_vars ) { - return $query_vars; - } - - /** - * Set row actions. - * - * @param array $actions Array of actions. - * @param WP_Post $post Current post object. - * @return array - */ - public function row_actions( $actions, $post ) { - if ( $this->list_table_type === $post->post_type ) { - return $this->get_row_actions( $actions, $post ); - } - return $actions; - } - - /** - * Get row actions to show in the list table. - * - * @param array $actions Array of actions. - * @param WP_Post $post Current post object. - * @return array - */ - protected function get_row_actions( $actions, $post ) { - return $actions; - } - - /** - * Adjust which columns are displayed by default. - * - * @param array $hidden Current hidden columns. - * @param object $screen Current screen. - * @return array - */ - public function default_hidden_columns( $hidden, $screen ) { - if ( isset( $screen->id ) && 'edit-' . $this->list_table_type === $screen->id ) { - $hidden = array_merge( $hidden, $this->define_hidden_columns() ); - } - return $hidden; - } - - /** - * Set list table primary column. - * - * @param string $default Default value. - * @param string $screen_id Current screen ID. - * @return string - */ - public function list_table_primary_column( $default, $screen_id ) { - if ( 'edit-' . $this->list_table_type === $screen_id && $this->get_primary_column() ) { - return $this->get_primary_column(); - } - return $default; - } - - /** - * Define primary column. - * - * @return array - */ - protected function get_primary_column() { - return ''; - } - - /** - * Define hidden columns. - * - * @return array - */ - protected function define_hidden_columns() { - return array(); - } - - /** - * Define which columns are sortable. - * - * @param array $columns Existing columns. - * @return array - */ - public function define_sortable_columns( $columns ) { - return $columns; - } - - /** - * Define which columns to show on this screen. - * - * @param array $columns Existing columns. - * @return array - */ - public function define_columns( $columns ) { - return $columns; - } - - /** - * Define bulk actions. - * - * @param array $actions Existing actions. - * @return array - */ - public function define_bulk_actions( $actions ) { - return $actions; - } - - /** - * Pre-fetch any data for the row each column has access to it. - * - * @param int $post_id Post ID being shown. - */ - protected function prepare_row_data( $post_id ) {} - - /** - * Render individual columns. - * - * @param string $column Column ID to render. - * @param int $post_id Post ID being shown. - */ - public function render_columns( $column, $post_id ) { - $this->prepare_row_data( $post_id ); - - if ( ! $this->object ) { - return; - } - - if ( is_callable( array( $this, 'render_' . $column . '_column' ) ) ) { - $this->{"render_{$column}_column"}(); - } - } - - /** - * Handle bulk actions. - * - * @param string $redirect_to URL to redirect to. - * @param string $action Action name. - * @param array $ids List of ids. - * @return string - */ - public function handle_bulk_actions( $redirect_to, $action, $ids ) { - return esc_url_raw( $redirect_to ); - } -} diff --git a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php b/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php deleted file mode 100644 index 5c27f0a..0000000 --- a/backend/app/plugins/wiaas/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php +++ /dev/null @@ -1,866 +0,0 @@ -'; - echo '

' . esc_html__( 'When you receive a new order, it will appear here.', 'woocommerce' ) . '

'; - echo '' . esc_html__( 'Learn more about orders', 'woocommerce' ) . ''; - echo '
'; - } - - /** - * Define primary column. - * - * @return string - */ - protected function get_primary_column() { - return 'order_number'; - } - - /** - * Get row actions to show in the list table. - * - * @param array $actions Array of actions. - * @param WP_Post $post Current post object. - * @return array - */ - protected function get_row_actions( $actions, $post ) { - return array(); - } - - /** - * Define hidden columns. - * - * @return array - */ - protected function define_hidden_columns() { - return array( - 'shipping_address', - 'billing_address', - 'wc_actions', - ); - } - - /** - * Define which columns are sortable. - * - * @param array $columns Existing columns. - * @return array - */ - public function define_sortable_columns( $columns ) { - $custom = array( - 'order_number' => 'ID', - 'order_total' => 'order_total', - 'order_date' => 'date', - ); - unset( $columns['comments'] ); - - return wp_parse_args( $custom, $columns ); - } - - /** - * Define which columns to show on this screen. - * - * @param array $columns Existing columns. - * @return array - */ - public function define_columns( $columns ) { - $show_columns = array(); - $show_columns['cb'] = $columns['cb']; - $show_columns['order_number'] = __( 'Order', 'woocommerce' ); - $show_columns['order_date'] = __( 'Date', 'woocommerce' ); - $show_columns['order_status'] = __( 'Status', 'woocommerce' ); - $show_columns['billing_address'] = __( 'Billing', 'woocommerce' ); - $show_columns['shipping_address'] = __( 'Ship to', 'woocommerce' ); - $show_columns['order_total'] = __( 'Total', 'woocommerce' ); - $show_columns['wc_actions'] = __( 'Actions', 'woocommerce' ); - - wp_enqueue_script( 'wc-orders' ); - - return $show_columns; - } - - /** - * Define bulk actions. - * - * @param array $actions Existing actions. - * @return array - */ - public function define_bulk_actions( $actions ) { - if ( isset( $actions['edit'] ) ) { - unset( $actions['edit'] ); - } - - $actions['mark_processing'] = __( 'Change status to processing', 'woocommerce' ); - $actions['mark_on-hold'] = __( 'Change status to on-hold', 'woocommerce' ); - $actions['mark_completed'] = __( 'Change status to completed', 'woocommerce' ); - $actions['remove_personal_data'] = __( 'Remove personal data', 'woocommerce' ); - - return $actions; - } - - /** - * Pre-fetch any data for the row each column has access to it. the_order global is there for bw compat. - * - * @param int $post_id Post ID being shown. - */ - protected function prepare_row_data( $post_id ) { - global $the_order; - - if ( empty( $this->object ) || $this->object->get_id() !== $post_id ) { - $this->object = wc_get_order( $post_id ); - $the_order = $this->object; - } - } - - /** - * Render columm: order_number. - */ - protected function render_order_number_column() { - $buyer = ''; - - if ( $this->object->get_billing_first_name() || $this->object->get_billing_last_name() ) { - /* translators: 1: first name 2: last name */ - $buyer = trim( sprintf( _x( '%1$s %2$s', 'full name', 'woocommerce' ), $this->object->get_billing_first_name(), $this->object->get_billing_last_name() ) ); - } elseif ( $this->object->get_billing_company() ) { - $buyer = trim( $this->object->get_billing_company() ); - } elseif ( $this->object->get_customer_id() ) { - $user = get_user_by( 'id', $this->object->get_customer_id() ); - $buyer = ucwords( $user->display_name ); - } - - if ( $this->object->get_status() === 'trash' ) { - echo '#' . esc_attr( $this->object->get_order_number() ) . ' ' . esc_html( $buyer ) . ''; - } else { - echo '' . esc_html( __( 'Preview', 'woocommerce' ) ) . ''; - echo '#' . esc_attr( $this->object->get_order_number() ) . ' ' . esc_html( $buyer ) . ''; - } - } - - /** - * Render columm: order_status. - */ - protected function render_order_status_column() { - $tooltip = ''; - $comment_count = get_comment_count( $this->object->get_id() ); - $approved_comments_count = absint( $comment_count['approved'] ); - - if ( $approved_comments_count ) { - $latest_notes = wc_get_order_notes( - array( - 'order_id' => $this->object->get_id(), - 'limit' => 1, - 'orderby' => 'date_created_gmt', - ) - ); - - $latest_note = current( $latest_notes ); - - if ( isset( $latest_note->content ) && 1 === $approved_comments_count ) { - $tooltip = wc_sanitize_tooltip( $latest_note->content ); - } elseif ( isset( $latest_note->content ) ) { - /* translators: %d: notes count */ - $tooltip = wc_sanitize_tooltip( $latest_note->content . '
' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $approved_comments_count - 1 ), 'woocommerce' ), $approved_comments_count - 1 ) . '' ); - } else { - /* translators: %d: notes count */ - $tooltip = wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $approved_comments_count, 'woocommerce' ), $approved_comments_count ) ); - } - } - - if ( $tooltip ) { - printf( '%s', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), wp_kses_post( $tooltip ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) ); - } else { - printf( '%s', esc_attr( sanitize_html_class( 'status-' . $this->object->get_status() ) ), esc_html( wc_get_order_status_name( $this->object->get_status() ) ) ); - } - } - - /** - * Render columm: order_date. - */ - protected function render_order_date_column() { - $order_timestamp = $this->object->get_date_created()->getTimestamp(); - - // Check if the order was created within the last 24 hours, and not in the future. - if ( $order_timestamp > strtotime( '-1 day', current_time( 'timestamp', true ) ) && $order_timestamp <= current_time( 'timestamp', true ) ) { - $show_date = sprintf( - /* translators: %s: human-readable time difference */ - _x( '%s ago', '%s = human-readable time difference', 'woocommerce' ), - human_time_diff( $this->object->get_date_created()->getTimestamp(), current_time( 'timestamp', true ) ) - ); - } else { - $show_date = $this->object->get_date_created()->date_i18n( apply_filters( 'woocommerce_admin_order_date_format', __( 'M j, Y', 'woocommerce' ) ) ); - } - printf( - '', - esc_attr( $this->object->get_date_created()->date( 'c' ) ), - esc_html( $this->object->get_date_created()->date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) ) ), - esc_html( $show_date ) - ); - } - - /** - * Render columm: order_total. - */ - protected function render_order_total_column() { - if ( $this->object->get_payment_method_title() ) { - /* translators: %s: method */ - echo '' . wp_kses_post( $this->object->get_formatted_order_total() ) . ''; - } else { - echo wp_kses_post( $this->object->get_formatted_order_total() ); - } - } - - /** - * Render columm: wc_actions. - */ - protected function render_wc_actions_column() { - echo '

'; - - do_action( 'woocommerce_admin_order_actions_start', $this->object ); - - $actions = array(); - - if ( $this->object->has_status( array( 'pending', 'on-hold' ) ) ) { - $actions['processing'] = array( - 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $this->object->get_id() ), 'woocommerce-mark-order-status' ), - 'name' => __( 'Processing', 'woocommerce' ), - 'action' => 'processing', - ); - } - - if ( $this->object->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) { - $actions['complete'] = array( - 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $this->object->get_id() ), 'woocommerce-mark-order-status' ), - 'name' => __( 'Complete', 'woocommerce' ), - 'action' => 'complete', - ); - } - - $actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $this->object ); - - echo wc_render_action_buttons( $actions ); // WPCS: XSS ok. - - do_action( 'woocommerce_admin_order_actions_end', $this->object ); - - echo '

'; - } - - /** - * Render columm: billing_address. - */ - protected function render_billing_address_column() { - $address = $this->object->get_formatted_billing_address(); - - if ( $address ) { - echo esc_html( preg_replace( '##i', ', ', $address ) ); - - if ( $this->object->get_payment_method() ) { - /* translators: %s: payment method */ - echo '' . sprintf( __( 'via %s', 'woocommerce' ), esc_html( $this->object->get_payment_method_title() ) ) . ''; // WPCS: XSS ok. - } - } else { - echo '–'; - } - } - - /** - * Render columm: shipping_address. - */ - protected function render_shipping_address_column() { - $address = $this->object->get_formatted_shipping_address(); - - if ( $address ) { - echo '' . esc_html( preg_replace( '##i', ', ', $address ) ) . ''; - if ( $this->object->get_shipping_method() ) { - /* translators: %s: shipping method */ - echo '' . sprintf( __( 'via %s', 'woocommerce' ), esc_html( $this->object->get_shipping_method() ) ) . ''; // WPCS: XSS ok. - } - } else { - echo '–'; - } - } - - /** - * Template for order preview. - * - * @since 3.3.0 - */ - public function order_preview_template() { - ?> - - get_items(), $order ); - $columns = apply_filters( - 'woocommerce_admin_order_preview_line_item_columns', array( - 'product' => __( 'Product', 'woocommerce' ), - 'quantity' => __( 'Quantity', 'woocommerce' ), - 'tax' => __( 'Tax', 'woocommerce' ), - 'total' => __( 'Total', 'woocommerce' ), - ), $order - ); - - if ( ! wc_tax_enabled() ) { - unset( $columns['tax'] ); - } - - $html = ' -
- - - '; - - foreach ( $columns as $column => $label ) { - $html .= ''; - } - - $html .= ' - - - '; - - foreach ( $line_items as $item_id => $item ) { - - $product_object = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null; - $row_class = apply_filters( 'woocommerce_admin_html_order_preview_item_class', '', $item, $order ); - - $html .= ''; - - foreach ( $columns as $column => $label ) { - $html .= ''; - } - - $html .= ''; - } - - $html .= ' - -
' . esc_html( $label ) . '
'; - switch ( $column ) { - case 'product': - $html .= wp_kses_post( $item->get_name() ); - - if ( $product_object ) { - $html .= '
' . esc_html( $product_object->get_sku() ) . '
'; - } - - $meta_data = $item->get_formatted_meta_data( '' ); - - if ( $meta_data ) { - $html .= ''; - - foreach ( $meta_data as $meta_id => $meta ) { - if ( in_array( $meta->key, $hidden_order_itemmeta, true ) ) { - continue; - } - $html .= ''; - } - $html .= '
' . wp_kses_post( $meta->display_key ) . ':' . wp_kses_post( force_balance_tags( $meta->display_value ) ) . '
'; - } - break; - case 'quantity': - $html .= esc_html( $item->get_quantity() ); - break; - case 'tax': - $html .= wc_price( $item->get_total_tax(), array( 'currency' => $order->get_currency() ) ); - break; - case 'total': - $html .= wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) ); - break; - default: - $html .= apply_filters( 'woocommerce_admin_order_preview_line_item_column_' . sanitize_key( $column ), '', $item, $item_id, $order ); - break; - } - $html .= '
-
'; - - return $html; - } - - /** - * Get actions to display in the preview as HTML. - * - * @param WC_Order $order Order object. - * @return string - */ - public static function get_order_preview_actions_html( $order ) { - $actions = array(); - $status_actions = array(); - - if ( $order->has_status( array( 'pending' ) ) ) { - $status_actions['on-hold'] = array( - 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=on-hold&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ), - 'name' => __( 'On-hold', 'woocommerce' ), - 'title' => __( 'Change order status to on-hold', 'woocommerce' ), - 'action' => 'on-hold', - ); - } - - if ( $order->has_status( array( 'pending', 'on-hold' ) ) ) { - $status_actions['processing'] = array( - 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=processing&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ), - 'name' => __( 'Processing', 'woocommerce' ), - 'title' => __( 'Change order status to processing', 'woocommerce' ), - 'action' => 'processing', - ); - } - - if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) { - $status_actions['complete'] = array( - 'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=completed&order_id=' . $order->get_id() ), 'woocommerce-mark-order-status' ), - 'name' => __( 'Completed', 'woocommerce' ), - 'title' => __( 'Change order status to completed', 'woocommerce' ), - 'action' => 'complete', - ); - } - - if ( $status_actions ) { - $actions['status'] = array( - 'group' => __( 'Change status: ', 'woocommerce' ), - 'actions' => $status_actions, - ); - } - - return wc_render_action_buttons( apply_filters( 'woocommerce_admin_order_preview_actions', $actions, $order ) ); - } - - /** - * Get order details to send to the ajax endpoint for previews. - * - * @param WC_Order $order Order object. - * @return array - */ - public static function order_preview_get_order_details( $order ) { - if ( ! $order ) { - return array(); - } - - $payment_via = $order->get_payment_method_title(); - $payment_method = $order->get_payment_method(); - $payment_gateways = WC()->payment_gateways() ? WC()->payment_gateways->payment_gateways() : array(); - $transaction_id = $order->get_transaction_id(); - - if ( $transaction_id ) { - - $url = isset( $payment_gateways[ $payment_method ] ) ? $payment_gateways[ $payment_method ]->get_transaction_url( $order ) : false; - - if ( $url ) { - $payment_via .= ' (' . esc_html( $transaction_id ) . ')'; - } else { - $payment_via .= ' (' . esc_html( $transaction_id ) . ')'; - } - } - - $billing_address = $order->get_formatted_billing_address(); - $shipping_address = $order->get_formatted_shipping_address(); - - return apply_filters( - 'woocommerce_admin_order_preview_get_order_details', array( - 'data' => $order->get_data(), - 'order_number' => $order->get_order_number(), - 'item_html' => WC_Admin_List_Table_Orders::get_order_preview_item_html( $order ), - 'actions_html' => WC_Admin_List_Table_Orders::get_order_preview_actions_html( $order ), - 'ship_to_billing' => wc_ship_to_billing_address_only(), - 'needs_shipping' => $order->needs_shipping_address(), - 'formatted_billing_address' => $billing_address ? $billing_address : __( 'N/A', 'woocommerce' ), - 'formatted_shipping_address' => $shipping_address ? $shipping_address : __( 'N/A', 'woocommerce' ), - 'shipping_address_map_url' => $order->get_shipping_address_map_url(), - 'payment_via' => $payment_via, - 'shipping_via' => $order->get_shipping_method(), - 'status' => $order->get_status(), - 'status_name' => wc_get_order_status_name( $order->get_status() ), - ), $order - ); - } - - /** - * Handle bulk actions. - * - * @param string $redirect_to URL to redirect to. - * @param string $action Action name. - * @param array $ids List of ids. - * @return string - */ - public function handle_bulk_actions( $redirect_to, $action, $ids ) { - $ids = array_map( 'absint', $ids ); - $changed = 0; - - if ( 'remove_personal_data' === $action ) { - $report_action = 'removed_personal_data'; - - foreach ( $ids as $id ) { - $order = wc_get_order( $id ); - - if ( $order ) { - do_action( 'woocommerce_remove_order_personal_data', $order ); - $changed++; - } - } - } elseif ( false !== strpos( $action, 'mark_' ) ) { - $order_statuses = wc_get_order_statuses(); - $new_status = substr( $action, 5 ); // Get the status name from action. - $report_action = 'marked_' . $new_status; - - // Sanity check: bail out if this is actually not a status, or is not a registered status. - if ( isset( $order_statuses[ 'wc-' . $new_status ] ) ) { - // Initialize payment gateways in case order has hooked status transition actions. - wc()->payment_gateways(); - - foreach ( $ids as $id ) { - $order = wc_get_order( $id ); - $order->update_status( $new_status, __( 'Order status changed by bulk edit:', 'woocommerce' ), true ); - do_action( 'woocommerce_order_edit_status', $id, $new_status ); - $changed++; - } - } - } - - if ( $changed ) { - $redirect_to = add_query_arg( - array( - 'post_type' => $this->list_table_type, - 'bulk_action' => $report_action, - 'changed' => $changed, - 'ids' => join( ',', $ids ), - ), $redirect_to - ); - } - - return esc_url_raw( $redirect_to ); - } - - /** - * Show confirmation message that order status changed for number of orders. - */ - public function bulk_admin_notices() { - global $post_type, $pagenow; - - // Bail out if not on shop order list page. - if ( 'edit.php' !== $pagenow || 'shop_order' !== $post_type || ! isset( $_REQUEST['bulk_action'] ) ) { // WPCS: input var ok, CSRF ok. - return; - } - - $order_statuses = wc_get_order_statuses(); - $number = isset( $_REQUEST['changed'] ) ? absint( $_REQUEST['changed'] ) : 0; // WPCS: input var ok, CSRF ok. - $bulk_action = wc_clean( wp_unslash( $_REQUEST['bulk_action'] ) ); // WPCS: input var ok, CSRF ok. - - // Check if any status changes happened. - foreach ( $order_statuses as $slug => $name ) { - if ( 'marked_' . str_replace( 'wc-', '', $slug ) === $bulk_action ) { // WPCS: input var ok, CSRF ok. - /* translators: %d: orders count */ - $message = sprintf( _n( '%d order status changed.', '%d order statuses changed.', $number, 'woocommerce' ), number_format_i18n( $number ) ); - echo '

' . esc_html( $message ) . '

'; - break; - } - } - - if ( 'removed_personal_data' === $bulk_action ) { // WPCS: input var ok, CSRF ok. - /* translators: %d: orders count */ - $message = sprintf( _n( 'Removed personal data from %d order.', 'Removed personal data from %d orders.', $number, 'woocommerce' ), number_format_i18n( $number ) ); - echo '

' . esc_html( $message ) . '

'; - } - } - - /** - * See if we should render search filters or not. - */ - public function restrict_manage_posts() { - global $typenow; - - if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ), true ) ) { - $this->render_filters(); - } - } - - /** - * Render any custom filters and search inputs for the list table. - */ - protected function render_filters() { - $user_string = ''; - $user_id = ''; - - if ( ! empty( $_GET['_customer_user'] ) ) { // WPCS: input var ok. - $user_id = absint( $_GET['_customer_user'] ); // WPCS: input var ok, sanitization ok. - $user = get_user_by( 'id', $user_id ); - - $user_string = sprintf( - /* translators: 1: user display name 2: user ID 3: user email */ - esc_html__( '%1$s (#%2$s – %3$s)', 'woocommerce' ), - $user->display_name, - absint( $user->ID ), - $user->user_email - ); - } - ?> - - query_filters( $query_vars ); - } - - return $query_vars; - } - - /** - * Handle any custom filters. - * - * @param array $query_vars Query vars. - * @return array - */ - protected function query_filters( $query_vars ) { - global $wp_post_statuses; - - // Filter the orders by the posted customer. - if ( ! empty( $_GET['_customer_user'] ) ) { // WPCS: input var ok. - // @codingStandardsIgnoreStart - $query_vars['meta_query'] = array( - array( - 'key' => '_customer_user', - 'value' => (int) $_GET['_customer_user'], // WPCS: input var ok, sanitization ok. - 'compare' => '=', - ), - ); - // @codingStandardsIgnoreEnd - } - - // Sorting. - if ( isset( $query_vars['orderby'] ) ) { - if ( 'order_total' === $query_vars['orderby'] ) { - // @codingStandardsIgnoreStart - $query_vars = array_merge( $query_vars, array( - 'meta_key' => '_order_total', - 'orderby' => 'meta_value_num', - ) ); - // @codingStandardsIgnoreEnd - } - } - - // Status. - if ( ! isset( $query_vars['post_status'] ) ) { - $post_statuses = wc_get_order_statuses(); - - foreach ( $post_statuses as $status => $value ) { - if ( isset( $wp_post_statuses[ $status ] ) && false === $wp_post_statuses[ $status ]->show_in_admin_all_list ) { - unset( $post_statuses[ $status ] ); - } - } - - $query_vars['post_status'] = array_keys( $post_statuses ); - } - return $query_vars; - } - - /** - * Change the label when searching orders. - * - * @param mixed $query Current search query. - * @return string - */ - public function search_label( $query ) { - global $pagenow, $typenow; - - if ( 'edit.php' !== $pagenow || 'shop_order' !== $typenow || ! get_query_var( 'shop_order_search' ) || ! isset( $_GET['s'] ) ) { // WPCS: input var ok. - return $query; - } - - return wc_clean( wp_unslash( $_GET['s'] ) ); // WPCS: input var ok, sanitization ok. - } - - /** - * Query vars for custom searches. - * - * @param mixed $public_query_vars Array of query vars. - * @return array - */ - public function add_custom_query_var( $public_query_vars ) { - $public_query_vars[] = 'shop_order_search'; - return $public_query_vars; - } - - /** - * Search custom fields as well as content. - * - * @param WP_Query $wp Query object. - */ - public function search_custom_fields( $wp ) { - global $pagenow; - - if ( 'edit.php' !== $pagenow || empty( $wp->query_vars['s'] ) || 'shop_order' !== $wp->query_vars['post_type'] || ! isset( $_GET['s'] ) ) { // WPCS: input var ok. - return; - } - - $post_ids = wc_order_search( wc_clean( wp_unslash( $_GET['s'] ) ) ); // WPCS: input var ok, sanitization ok. - - if ( ! empty( $post_ids ) ) { - // Remove "s" - we don't want to search order name. - unset( $wp->query_vars['s'] ); - - // so we know we're doing this. - $wp->query_vars['shop_order_search'] = true; - - // Search by found posts. - $wp->query_vars['post__in'] = array_merge( $post_ids, array( 0 ) ); - } - } -} diff --git a/backend/app/plugins/wiaas/wiaas.php b/backend/app/plugins/wiaas/wiaas.php index 67b74ca..5de9a09 100644 --- a/backend/app/plugins/wiaas/wiaas.php +++ b/backend/app/plugins/wiaas/wiaas.php @@ -61,7 +61,6 @@ include_once WIAAS_DIR . '/includes/wiaas-class-measurement-units.php'; include_once WIAAS_DIR . '/includes/class-wiaas-validation.php'; -include_once WIAAS_DIR . '/includes/admin/custom-woocommerce-admin-classes/class-wc-admin-list-table-orders.php'; function wiaas_redirect_to_login() { wp_safe_redirect(get_site_url('', 'wp-login.php')); From a0b7faa90fd3fe8b8ecf863d59fdccf18d3e9540 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Thu, 29 Nov 2018 13:24:03 +0100 Subject: [PATCH 09/11] show delivery details --- .../plugins/wiaas/includes/admin/class-wiaas-admin-orders.php | 1 + 1 file changed, 1 insertion(+) diff --git a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php index bb45ecf..6f75a13 100644 --- a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php @@ -77,6 +77,7 @@ class Wiaas_Admin_Orders { public static function add_custom_data_to_order_preview ($order){ $order['commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] ); + $order['needs_shipping'] = true; return $order; } From 397a0ed8319179cf8f3fc8361d54105480d45ec3 Mon Sep 17 00:00:00 2001 From: Bilal Catic Date: Fri, 30 Nov 2018 13:44:48 +0100 Subject: [PATCH 10/11] add wiaas prefix --- .../admin/class-wiaas-admin-orders.php | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php index 6f75a13..26fb3d1 100644 --- a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php @@ -27,13 +27,13 @@ class Wiaas_Admin_Orders { if ($is_admin){ add_filter( 'woocommerce_admin_order_preview_line_item_columns', array(__CLASS__, 'customize_order_preview_columns')); - add_filter( 'woocommerce_admin_order_preview_line_item_column_payment_type', array(__CLASS__, 'fill_in_payment_type'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_services_extra', array(__CLASS__, 'fill_in_services_extra'), 10, 4); - add_filter( 'woocommerce_admin_order_preview_line_item_column_services_contract_period', array(__CLASS__, 'fill_in_services_contract_period'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_max_contract_period', array(__CLASS__, 'fill_in_max_contract_period'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_period_unit', array(__CLASS__, 'fill_in_period_unit'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_recurrent_extra', array(__CLASS__, 'fill_in_recurrent_extra'), 10, 4); - add_filter( 'woocommerce_admin_order_preview_line_item_column_pay_period', array(__CLASS__, 'fill_in_pay_period'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_payment_type', array(__CLASS__, 'fill_in_payment_type'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_services_extra', array(__CLASS__, 'fill_in_services_extra'), 10, 4); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_services_contract_period', array(__CLASS__, 'fill_in_services_contract_period'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_max_contract_period', array(__CLASS__, 'fill_in_max_contract_period'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_period_unit', array(__CLASS__, 'fill_in_period_unit'), 10, 2); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_recurrent_extra', array(__CLASS__, 'fill_in_recurrent_extra'), 10, 4); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_pay_period', array(__CLASS__, 'fill_in_pay_period'), 10, 2); } } @@ -42,9 +42,9 @@ class Wiaas_Admin_Orders { $new_columns = array(); - $new_columns['reference'] = 'Location'; - $new_columns['commercial_lead'] = 'Commercial lead'; - $new_columns['customer'] = 'Customer'; + $new_columns['wiaas_reference'] = 'Location'; + $new_columns['wiaas_commercial_lead'] = 'Commercial lead'; + $new_columns['wiaas_customer'] = 'Customer'; $insertAfterColumn = 1; @@ -58,15 +58,15 @@ class Wiaas_Admin_Orders { $column_content = ''; switch ($column){ - case 'reference': + case 'wiaas_reference': $column_content = Wiaas_Order::get_order_reference($post->ID); break; - case 'commercial_lead': + case 'wiaas_commercial_lead': $column_content = Wiaas_Order::get_order_commercial_lead_name($post->ID); break; - case 'customer': + case 'wiaas_customer': $column_content = Wiaas_Order::get_order_customer_full_name($post->ID); break; } @@ -76,7 +76,7 @@ class Wiaas_Admin_Orders { public static function add_custom_data_to_order_preview ($order){ - $order['commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] ); + $order['wiaas_commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] ); $order['needs_shipping'] = true; return $order; @@ -87,7 +87,7 @@ class Wiaas_Admin_Orders { echo '

Commercial lead

- {{data.commercial_lead_name}} + {{data.wiaas_commercial_lead_name}}
'; } @@ -109,13 +109,13 @@ class Wiaas_Admin_Orders { unset($columns['total']); - $columns['payment_type'] = __( 'Payment type', 'wiaas' ); - $columns['services_extra'] = __( 'Services extra', 'wiaas'); - $columns['recurrent_extra'] = __( 'Recurrent extra', 'wiaas'); - $columns['services_contract_period'] = __( 'Contract period', 'wiaas'); - $columns['max_contract_period'] = __( 'Maximum contract period', 'wiaas'); - $columns['pay_period'] = __( 'Pay period', 'wiaas'); - $columns['period_unit'] = __( 'Period unit', 'wiaas'); + $columns['wiaas_payment_type'] = __( 'Payment type', 'wiaas' ); + $columns['wiaas_services_extra'] = __( 'Services extra', 'wiaas'); + $columns['wiaas_recurrent_extra'] = __( 'Recurrent extra', 'wiaas'); + $columns['wiaas_services_contract_period'] = __( 'Contract period', 'wiaas'); + $columns['wiaas_max_contract_period'] = __( 'Maximum contract period', 'wiaas'); + $columns['wiaas_pay_period'] = __( 'Pay period', 'wiaas'); + $columns['wiaas_period_unit'] = __( 'Period unit', 'wiaas'); $columns['total'] = __( 'Total', 'woocommerce'); From a2c088ba52c19fd3e3f78976fd3e94c5e35f80f7 Mon Sep 17 00:00:00 2001 From: Almira Krdzic Date: Sat, 1 Dec 2018 00:17:54 +0100 Subject: [PATCH 11/11] Fix order details display --- .../admin-cl/class-wiaas-admin-cl-orders.php | 16 +- .../class-wiaas-admin-supplier-orders.php | 62 +++- .../admin/class-wiaas-admin-orders.php | 285 ++++++++++++++---- .../wiaas/includes/class-wiaas-cart.php | 36 --- .../wiaas/includes/class-wiaas-order.php | 48 +++ .../includes/order/class-wiaas-order-item.php | 90 ++++++ 6 files changed, 416 insertions(+), 121 deletions(-) create mode 100644 backend/app/plugins/wiaas/includes/order/class-wiaas-order-item.php diff --git a/backend/app/plugins/wiaas/includes/admin/admin-cl/class-wiaas-admin-cl-orders.php b/backend/app/plugins/wiaas/includes/admin/admin-cl/class-wiaas-admin-cl-orders.php index 2486d65..4822296 100644 --- a/backend/app/plugins/wiaas/includes/admin/admin-cl/class-wiaas-admin-cl-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/admin-cl/class-wiaas-admin-cl-orders.php @@ -78,12 +78,16 @@ class Wiaas_Admin_CL_Orders { * @return array */ public static function columns_for_list_table_orders($columns) { - $show_columns = array(); - $show_columns['cb'] = $columns['cb']; - $show_columns['_wiaas_order_number'] = __( 'Order', 'woocommerce' ); - $show_columns['order_date'] = __( 'Date', 'woocommerce' ); - $show_columns['order_status'] = __( 'Status', 'woocommerce' ); - $show_columns['order_total'] = __( 'Total', 'woocommerce' ); + $show_columns = array(); + $show_columns['cb'] = $columns['cb']; + $show_columns['_wiaas_order_number'] = __( 'Order', 'wiaas' ); + $show_columns['order_date'] = $columns['order_date']; + $show_columns['wiaas_reference'] = __( 'Location', 'wiaas' ); + $show_columns['wiaas_customer'] = __( 'Customer', 'wiaas' ); + $show_columns['order_status'] = $columns['order_status']; + $show_columns['billing_address'] = $columns['billing_address']; + $show_columns['shipping_address'] = $columns['shipping_address']; + $show_columns['order_total'] = $columns['order_total']; return $show_columns; } diff --git a/backend/app/plugins/wiaas/includes/admin/admin-supplier/class-wiaas-admin-supplier-orders.php b/backend/app/plugins/wiaas/includes/admin/admin-supplier/class-wiaas-admin-supplier-orders.php index 700272b..afc4264 100644 --- a/backend/app/plugins/wiaas/includes/admin/admin-supplier/class-wiaas-admin-supplier-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/admin-supplier/class-wiaas-admin-supplier-orders.php @@ -21,11 +21,15 @@ class Wiaas_Admin_Supplier_Orders { add_filter('woocommerce_admin_order_preview_actions', array(__CLASS__, 'remove_actions_from_order_preview')); - add_filter('woocommerce_admin_order_preview_line_items', array(__CLASS__, 'filter_order_items_for_order_preview'), 10, 2); + add_filter('woocommerce_admin_order_preview_line_items', array(__CLASS__, 'filter_order_items_for_order_preview'), 999, 2); + + add_filter( 'woocommerce_admin_order_preview_line_item_columns', array(__CLASS__, 'customize_order_preview_columns'), 999); + + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_simple_product_bundle', array(__CLASS__, 'render_bundled_item_bundle'), 10, 4); add_filter('manage_shop_order_posts_columns', array(__CLASS__, 'columns_for_list_table_orders'), 999); - add_filter('manage_edit-shop_order_sortable_columns', array(__CLASS__, 'define_sortable_columns_for_list_table_orders')); + add_filter('manage_edit-shop_order_sortable_columns', array(__CLASS__, 'define_sortable_columns_for_list_table_orders'), 999); add_action('manage_shop_order_posts_custom_column', array(__CLASS__, 'render_columns_for_list_table_orders'), 999, 2); } @@ -53,7 +57,7 @@ class Wiaas_Admin_Supplier_Orders { * Show only simple products from this supplier on order preview * * @param $order_items - * @param $order + * @param WC_Order $order * * @return array */ @@ -61,26 +65,51 @@ class Wiaas_Admin_Supplier_Orders { $user_organisation_id = Wiaas_User_Organization::get_user_organization_id(wp_get_current_user()->ID); + $order_items = $order->get_items(); $items = array(); foreach ($order_items as $key => $order_item) { - $product = wc_get_product($order_item->get_product_id()); + if (wc_pb_is_bundled_order_item($order_item)) { - if ($product->get_type() == 'simple') { + $item_supplier_organization_id = absint($order_item['wiaas_supplier_organization_id']); - $supplier_organisation_id = Wiaas_Product_Supplier - ::get_supplier_organisation_id_from_product($order_item->get_product_id()); + if ($item_supplier_organization_id === $user_organisation_id) { - if ($supplier_organisation_id === $user_organisation_id) { - $items[$key] = $order_item; - } + $items[] = $order_item; + } } } return $items; } + public static function customize_order_preview_columns() { + + return array( + 'product' => __('Product', 'wiaas'), + 'quantity' => __( 'Quantity', 'wiaas' ), + 'wiaas_simple_product_bundle' => __( '', 'wiaas' ) + ); + } + + public static function render_bundled_item_bundle($empty, $item, $item_id, $order) { + + $html = ''; + + if (wc_pb_is_bundled_order_item($item) ) { + + $container_order_item = wc_pb_get_bundled_order_item_container($item, $order); + + if ( ! empty($container_order_item) ) { + + return $container_order_item->get_name(); + } + } + + return $html; + } + /** * Override default table columns so only supplier specific columns are visible * @@ -89,11 +118,14 @@ class Wiaas_Admin_Supplier_Orders { * @return array */ public static function columns_for_list_table_orders($columns) { - $show_columns = array(); - $show_columns['cb'] = $columns['cb']; - $show_columns['_wiaas_order_number'] = __( 'Order', 'woocommerce' ); - $show_columns['order_date'] = __( 'Date', 'woocommerce' ); - $show_columns['order_status'] = __( 'Status', 'woocommerce' ); + $show_columns = array(); + $show_columns['cb'] = $columns['cb']; + $show_columns['_wiaas_order_number'] = __( 'Order', 'wiaas' ); + $show_columns['order_date'] = $columns['order_date']; + $show_columns['wiaas_reference'] = __( 'Location', 'wiaas' ); + $show_columns['wiaas_customer'] = __( 'Customer', 'wiaas' ); + $show_columns['order_status'] = $columns['order_status']; + $show_columns['shipping_address'] = $columns['shipping_address']; return $show_columns; } diff --git a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php index 26fb3d1..5302a71 100644 --- a/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php +++ b/backend/app/plugins/wiaas/includes/admin/class-wiaas-admin-orders.php @@ -4,9 +4,15 @@ class Wiaas_Admin_Orders { public static function init() { - add_filter( 'manage_edit-shop_order_columns', array(__CLASS__, 'add_additional_columns_to_orders_screen') ); + // Orders list customization - add_action( 'manage_shop_order_posts_custom_column', array(__CLASS__, 'add_custom_columns_content') ); + add_filter( 'manage_shop_order_posts_columns', array(__CLASS__, 'add_additional_columns_to_orders_list'), 11 ); + + add_action( 'manage_shop_order_posts_custom_column', array(__CLASS__, 'render_orders_list_additional_columns') ); + + add_filter('default_hidden_columns', array(__CLASS__, 'filter_orders_list_default_hidden_columns'), 11, 2); + + // Order preview customization add_filter( 'woocommerce_admin_order_preview_get_order_details', array(__CLASS__, 'add_custom_data_to_order_preview') ); @@ -14,45 +20,53 @@ class Wiaas_Admin_Orders { add_filter( 'woocommerce_admin_order_preview_line_items', array(__CLASS__, 'remove_simple_items_from_preview')); + add_filter( 'woocommerce_admin_order_preview_line_item_columns', array(__CLASS__, 'order_preview_order_item_columns')); - add_action('init', array(__CLASS__, 'init_admin')); + add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_order_item_price', array(__CLASS__, 'render_order_item_preview_price_column'), 10, 4); + + // Order item metadata customization + + add_action('woocommerce_before_order_itemmeta', array(__CLASS__, 'render_order_details_order_item_custom_info'), 10, 3); + + add_filter('woocommerce_order_item_get_formatted_meta_data', array(__CLASS__, 'filter_order_item_formatted_meta_data'), 10, 2); + + add_filter('woocommerce_order_item_display_meta_key', array(__CLASS__, 'order_item_display_meta_key'), 10, 3); + + add_filter('woocommerce_order_item_display_meta_value', array(__CLASS__, 'order_item_display_meta_value'), 10, 3); } - public static function init_admin(){ - $current_user = wp_get_current_user(); - $role = $current_user->roles[0]; - $is_admin = $role === 'administrator'; - - if ($is_admin){ - add_filter( 'woocommerce_admin_order_preview_line_item_columns', array(__CLASS__, 'customize_order_preview_columns')); - - add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_payment_type', array(__CLASS__, 'fill_in_payment_type'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_services_extra', array(__CLASS__, 'fill_in_services_extra'), 10, 4); - add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_services_contract_period', array(__CLASS__, 'fill_in_services_contract_period'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_max_contract_period', array(__CLASS__, 'fill_in_max_contract_period'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_period_unit', array(__CLASS__, 'fill_in_period_unit'), 10, 2); - add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_recurrent_extra', array(__CLASS__, 'fill_in_recurrent_extra'), 10, 4); - add_filter( 'woocommerce_admin_order_preview_line_item_column_wiaas_pay_period', array(__CLASS__, 'fill_in_pay_period'), 10, 2); - - } - } - - public static function add_additional_columns_to_orders_screen( $columns ){ + /** + * Add columns to orders list with wiaas specific informations + * @param array $columns + * + * @return array + */ + public static function add_additional_columns_to_orders_list( $columns ) { $new_columns = array(); - $new_columns['wiaas_reference'] = 'Location'; - $new_columns['wiaas_commercial_lead'] = 'Commercial lead'; - $new_columns['wiaas_customer'] = 'Customer'; + $new_columns['wiaas_reference'] = __('Location', 'wiaas'); + $new_columns['wiaas_commercial_lead'] = __('Commercial lead', 'wiaas'); + $new_columns['wiaas_customer'] = __('Customer', 'wiaas'); + $new_columns['taxonomy-shop_order_project'] = __('Project', 'wiaas'); - $insertAfterColumn = 1; + $columns = array_merge( + array_slice($columns,0,3), + $new_columns, + array_slice($columns,3) + ); - return array_merge(array_slice($columns,0,$insertAfterColumn+1), $new_columns, array_slice($columns,$insertAfterColumn+1)); + return $columns; } - public static function add_custom_columns_content( $column ){ + /** + * Display data for custom wiaas columns in orders list + * + * @param string $column + */ + public static function render_orders_list_additional_columns( $column ) { global $post; $column_content = ''; @@ -68,13 +82,46 @@ class Wiaas_Admin_Orders { case 'wiaas_customer': $column_content = Wiaas_Order::get_order_customer_full_name($post->ID); + + $customer_organization_info = Wiaas_Order::get_customer_organization_info($post->ID); + + if ( ! empty($customer_organization_info) ) { + + $column_content .= '
'; + $column_content .= '' . $customer_organization_info['name'] . ''; + } break; } echo $column_content; } - public static function add_custom_data_to_order_preview ($order){ + /** + * Filter default hidden columns for orders list + * + * @param array $hidden + * @param object $screen + * + * @return array + */ + public static function filter_orders_list_default_hidden_columns($hidden, $screen) { + + if (isset($screen->id) && $screen->id === 'edit-shop_order') { + + $hidden = array( 'wc_actions' ); + } + + return $hidden; + } + + /** + * Add custom information to order preview data + * + * @param array $order + * + * @return array + */ + public static function add_custom_data_to_order_preview ($order) { $order['wiaas_commercial_lead_name'] = Wiaas_Order::get_order_commercial_lead_name( $order['data']['id'] ); $order['needs_shipping'] = true; @@ -82,6 +129,10 @@ class Wiaas_Admin_Orders { return $order; } + /** + * Display custom information in order preview + * + */ public static function show_custom_data_before_order_preview () { echo '
@@ -92,12 +143,22 @@ class Wiaas_Admin_Orders {
'; } + /** + * Display only bundles (not simple products) + * + * This will also not display options and addons + * + * @param array $order_items + * + * @return array + */ public static function remove_simple_items_from_preview( $order_items){ $items = array(); foreach ($order_items as $order_item) { - if (isset($order_item['wiaas_standard_package'])) { + + if ( Wiaas_Order_Item::is_standard_bundle($order_item) ) { $items[] = $order_item; } } @@ -105,52 +166,148 @@ class Wiaas_Admin_Orders { return $items; } - public static function customize_order_preview_columns( $columns ){ + /** + * Columns for order preview order items table + * + * @return array + */ + public static function order_preview_order_item_columns( ) { - unset($columns['total']); - - $columns['wiaas_payment_type'] = __( 'Payment type', 'wiaas' ); - $columns['wiaas_services_extra'] = __( 'Services extra', 'wiaas'); - $columns['wiaas_recurrent_extra'] = __( 'Recurrent extra', 'wiaas'); - $columns['wiaas_services_contract_period'] = __( 'Contract period', 'wiaas'); - $columns['wiaas_max_contract_period'] = __( 'Maximum contract period', 'wiaas'); - $columns['wiaas_pay_period'] = __( 'Pay period', 'wiaas'); - $columns['wiaas_period_unit'] = __( 'Period unit', 'wiaas'); - - $columns['total'] = __( 'Total', 'woocommerce'); - - return $columns; + return array( + 'product' => __('Product', 'wiaas'), + 'quantity' => __( 'Quantity', 'wiaas' ), + 'wiaas_order_item_price' => __( 'Price', 'wiaas' ) + ); } - public static function fill_in_payment_type ( $empty , $item){ - return $item->get_meta('_wiaas_payment_type'); + /** + * @param $empty + * @param WC_Order_Item $item + * @param int $item_id + * @param WC_Order $order + * + * @return string + */ + public static function render_order_item_preview_price_column($empty, $item, $item_id, $order) { + + if (Wiaas_Order_Item::is_standard_bundle($item)) { + + $total_price = wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) ); + + $monthly_price = wc_price( + Wiaas_Order_Item::get_monthly_recurring_total($item), + array( 'currency' => $order->get_currency() ) + ); + + return sprintf('On Delivery: %s
Monthly: %s', + $total_price, + $monthly_price); + } + + return ''; } - public static function fill_in_services_extra ($empty, $item, $item_id, $order){ - return wc_price( $item->get_meta('_wiaas_services_extra'), array( 'currency' => $order->get_currency() ) ); + /** + * Render order item payment info with order item metadata on order details page + * + * @param $item_id + * @param WC_Order_Item $item + * @param $product + */ + public static function render_order_details_order_item_custom_info($item_id, $item, $product) { + + if (Wiaas_Order_Item::is_standard_bundle($item)) { + + $order = $item->get_order(); + + $total_price = wc_price( $item->get_total(), array( 'currency' => $order->get_currency() ) ); + + $monthly_price = wc_price( + Wiaas_Order_Item::get_monthly_recurring_total($item), + array( 'currency' => $order->get_currency() ) + ); + + ?> +

+ On Delivery: +
+ Monthly: + get_meta('_wiaas_service_contract_period'); - } + /** + * @param array $formatted_meta + * @param WC_Order_Item $order_item + * + * @return array + */ + public static function filter_order_item_formatted_meta_data($formatted_meta, $order_item) { - public static function fill_in_max_contract_period($empty, $item){ - return $item->get_meta('_wiaas_max_contract_period'); - } + if (Wiaas_Order_Item::is_standard_bundle($order_item)) { - public static function fill_in_period_unit ($empty, $item){ - return $item->get_meta('_wiaas_period_unit'); - } + return $formatted_meta; + } - public static function fill_in_recurrent_extra($empty, $item, $item_id, $order){ - return wc_price( $item->get_meta('_wiaas_recurrent_extra'), array( 'currency' => $order->get_currency() ) ); - } + return array(); + } - public static function fill_in_pay_period($empty, $item){ - return $item->get_meta('_wiaas_pay_period'); - } + /** + * @param string $display_key + * @param object $meta + * @param WC_Order_Item $item + * + * @return string + */ + public static function order_item_display_meta_key($display_key, $meta, $item) { + switch ($meta->key) { + case '_wiaas_payment_type': + return 'Payment type'; + + case '_wiaas_services_extra': + return 'Services and support price'; + + case '_wiaas_recurrent_extra': + return 'Recurrent price'; + + default: + return $display_key; + } + } + + /** + * @param string $display_value + * @param object $meta + * @param WC_Order_Item $item + * + * @return string + */ + public static function order_item_display_meta_value($display_value, $meta, $item) { + + switch ($meta->key) { + + case '_wiaas_services_extra': + $order = $item->get_order(); + + return wc_price( + Wiaas_Order_Item::get_services_total($item), + array( 'currency' => $order->get_currency()) + ); + + case '_wiaas_recurrent_extra': + $order = $item->get_order(); + + return wc_price( + Wiaas_Order_Item::get_recurrent_total($item), + array( 'currency' => $order->get_currency()) + ); + + default: + return $display_value; + } + } } diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-cart.php b/backend/app/plugins/wiaas/includes/class-wiaas-cart.php index 3f3e766..8f64b9e 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-cart.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-cart.php @@ -21,8 +21,6 @@ class Wiaas_Cart { public static function init() { add_action( 'woocommerce_checkout_create_order_line_item', array( __CLASS__, 'add_order_item_meta' ), 10, 3 ); - add_filter( 'woocommerce_hidden_order_itemmeta', array( __CLASS__, 'hidden_order_item_meta' ) ); - add_action( 'woocommerce_before_calculate_totals', array( __CLASS__, 'on_calculate_totals' ), 99, 1); add_action( 'woocommerce_cart_loaded_from_session', array( __CLASS__, 'on_calculate_totals' ), 99, 1); @@ -444,40 +442,6 @@ class Wiaas_Cart { return $order_item; } - /** - * Mark extended properties for order as hidden - * @param $hidden - * - * @return array - */ - public static function hidden_order_item_meta( $hidden ) { - - return array_merge( $hidden, array( - '_wiaas_payment_type', - '_wiaas_services_extra', - '_wiaas_service_contract_period', - '_wiaas_max_contract_period', - '_wiaas_period_unit', - '_wiaas_recurrent_extra', - '_wiaas_pay_period', - '_wiaas_addon_items', - '_wiaas_addon_for', - '_wiaas_option_items', - '_wiaas_option_for', - '_wiaas_option_group_name', - '_wiaas_standard_package', - '_wiaas_documents', - '_wiaas_category', - '_wiaas_manufacturer_product_no', - '_wiaas_supplier_product_no', - '_wiaas_supplier_organization_id', - '_wiaas_product_price', - '_wiaas_earliest_installation_additional_days', - '_wiaas_installation', - '_wiaas_installation_date' - ) ); - } - /** * Sets additional order data form cart after order is successfully created * diff --git a/backend/app/plugins/wiaas/includes/class-wiaas-order.php b/backend/app/plugins/wiaas/includes/class-wiaas-order.php index e20aef6..bcd5859 100644 --- a/backend/app/plugins/wiaas/includes/class-wiaas-order.php +++ b/backend/app/plugins/wiaas/includes/class-wiaas-order.php @@ -19,6 +19,7 @@ class Wiaas_Order { require_once dirname( __FILE__ ) . '/order/class-wiaas-order-project.php'; require_once dirname( __FILE__ ) . '/order/wiaas-order-functions.php'; + require_once dirname( __FILE__ ) . '/order/class-wiaas-order-item.php'; add_filter('woocommerce_register_post_type_shop_order', array(__CLASS__, 'manage_order_settings')); @@ -436,6 +437,53 @@ class Wiaas_Order { return $code; } + /** + * Retrieve customer organization id for order + * + * @param int $order_id + * + * @return int|null + */ + public static function get_order_customer_organization_id($order_id) { + $order = wc_get_order($order_id); + + $customer_organization_id = $order->get_meta('_wiaas_customer_id', true); + + if (empty($customer_organization_id)) { + + $customer_organization_id = wiaas_get_user_organization_id($order->get_customer_id()); + } + + return $customer_organization_id; + } + + /** + * Retrieve customer organization info from order + * + * @param int $order_id + * + * @return array|null + */ + public static function get_customer_organization_info($order_id) { + $order = wc_get_order($order_id); + + $customer_organization_info = $order->get_meta('_wiaas_customer_info', true); + + $customer_organization_id = self::get_order_customer_organization_id($order_id); + + if ( empty($customer_organization_info) && ! empty( $customer_organization_id) ) { + + $customer_organization_info = wiaas_get_organization_info($customer_organization_id); + } + + if ( ! empty($customer_organization_info) ) { + + $customer_organization_info['id'] = $customer_organization_id; + } + + return ! empty($customer_organization_info) ? $customer_organization_info : null; + } + /** * PRIVATE */ diff --git a/backend/app/plugins/wiaas/includes/order/class-wiaas-order-item.php b/backend/app/plugins/wiaas/includes/order/class-wiaas-order-item.php new file mode 100644 index 0000000..57da78f --- /dev/null +++ b/backend/app/plugins/wiaas/includes/order/class-wiaas-order-item.php @@ -0,0 +1,90 @@ +get_meta('_wiaas_standard_package'); + return ! empty($is_standard); + } + + /** + * @param WC_Order_Item $order_item + * + * @return float + */ + public static function get_services_total($order_item) { + $quantity = $order_item->get_quantity(); + + return $quantity * floatval($order_item->get_meta('_wiaas_services_extra')); + } + + /** + * @param WC_Order_Item $order_item + * + * @return float + */ + public static function get_recurrent_total($order_item) { + $quantity = $order_item->get_quantity(); + + return $quantity * floatval($order_item->get_meta('_wiaas_recurrent_extra')); + } + + /** + * @param WC_Order_Item $order_item + * + * @return float + */ + public static function get_monthly_recurring_total($order_item) { + + return self::get_services_total($order_item) + self::get_recurrent_total($order_item); + } + +} + +Wiaas_Order_Item::init();