Merge branch 'suppliers-order' into 'master'
Give order access to suppliers , when order is created See merge request saburly/wiaas/new-wiaas!48
This commit was merged in pull request #48.
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
.wc-order-preview-address {
|
||||
display: none;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
jQuery(document).ready(function($) {
|
||||
$( document.body )
|
||||
.on( 'wc_backbone_modal_loaded', function () {
|
||||
$(".wc-order-preview-address").remove();
|
||||
} );
|
||||
|
||||
});
|
||||
@@ -0,0 +1,136 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class Wiaas_Admin_Supplier_Orders
|
||||
*/
|
||||
class Wiaas_Admin_Supplier_Orders {
|
||||
|
||||
/**
|
||||
* Displays list of orders from supplier
|
||||
*
|
||||
* Enables quick preview of each order
|
||||
*
|
||||
* This list of orders is achieved with customization of default order list for `shop_order` post
|
||||
* by using hooks and filters to allow only data that suppliershould be able to see
|
||||
*
|
||||
*/
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_filter('bulk_actions-edit-shop_order', array(__CLASS__, 'remove_bulk_actions_for_list_table_orders'), 999);
|
||||
|
||||
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('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_action('manage_shop_order_posts_custom_column', array(__CLASS__, 'render_columns_for_list_table_orders'), 999, 2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Remove all bulk actions for supplier
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function remove_bulk_actions_for_list_table_orders() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove actions from shop order preview modal so only data info is visible
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function remove_actions_from_order_preview() {
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Show only simple products from this supplier on order preview
|
||||
*
|
||||
* @param $order_items
|
||||
* @param $order
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function filter_order_items_for_order_preview($order_items, $order) {
|
||||
|
||||
$user_organisation_id = Wiaas_User_Organization::get_user_organization_id(wp_get_current_user()->ID);
|
||||
|
||||
$items = array();
|
||||
|
||||
foreach ($order_items as $key => $order_item) {
|
||||
|
||||
$product = wc_get_product($order_item->get_product_id());
|
||||
|
||||
if ($product->get_type() == 'simple') {
|
||||
|
||||
$supplier_organisation_id = Wiaas_Product_Supplier
|
||||
::get_supplier_organisation_id_from_product($order_item->get_product_id());
|
||||
|
||||
if ($supplier_organisation_id === $user_organisation_id) {
|
||||
$items[$key] = $order_item;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Override default table columns so only supplier specific columns are visible
|
||||
*
|
||||
* @param $columns
|
||||
*
|
||||
* @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' );
|
||||
|
||||
return $show_columns;
|
||||
}
|
||||
|
||||
|
||||
/** Append supplier columns to table sortable columns
|
||||
*
|
||||
* @param $sortable_columns
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function define_sortable_columns_for_list_table_orders($sortable_columns) {
|
||||
|
||||
$sortable_columns['_wiaas_order_number'] = 'ID';
|
||||
|
||||
return $sortable_columns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render supplier specific columns
|
||||
*
|
||||
* @param $column
|
||||
* @param $order_id
|
||||
*/
|
||||
public static function render_columns_for_list_table_orders($column, $order_id) {
|
||||
|
||||
if ($column === '_wiaas_order_number') {
|
||||
|
||||
$order = wc_get_order($order_id);
|
||||
|
||||
echo '<strong>#' . esc_attr($order->get_order_number()) . '</strong>';
|
||||
|
||||
if ($order->get_status() !== 'trash') {
|
||||
echo '<a href="#" class="order-preview" data-order-id="' . absint($order->get_id()) . '" title="' . esc_attr(__('Preview', 'wiaas')) . '">' . esc_html(__('Preview', 'wiaas')) . '</a>';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_Supplier_Orders::init();
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
class Wiaas_Admin_Supplier {
|
||||
|
||||
public static function init() {
|
||||
|
||||
add_action('init', array(__CLASS__, 'init_admin_supplier'));
|
||||
}
|
||||
|
||||
public static function init_admin_supplier() {
|
||||
|
||||
$current_user = wp_get_current_user();
|
||||
|
||||
$role = $current_user->roles[0];
|
||||
|
||||
$is_supplier = $role === 'supplier';
|
||||
|
||||
if ($is_supplier) {
|
||||
|
||||
require_once dirname( __FILE__ ) . '/admin-supplier/class-wiaas-admin-supplier-orders.php';
|
||||
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static function enqueue_scripts() {
|
||||
$plugin_url = untrailingslashit( plugins_url( '/', WIAAS_FILE ) );
|
||||
|
||||
wp_enqueue_script( 'wiaas-admin-supplier', $plugin_url . '/assets/js/wiaas-admin-supplier.js' );
|
||||
|
||||
wp_enqueue_style( 'wiaas-admin-supplier', $plugin_url . '/assets/css/wiaas-admin-supplier.css' );
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_Supplier::init();
|
||||
@@ -17,6 +17,7 @@ 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 );
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -76,6 +77,26 @@ class Wiaas_Access_Management {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Assignees order to supplier organizations extracted from ordered items when order payment is complete.
|
||||
*
|
||||
* @param int $order_id
|
||||
*/
|
||||
public static function assign_order_to_suppliers($order_id){
|
||||
|
||||
$order = wc_get_order($order_id);
|
||||
$product_from_order = $order->get_items('line_item');
|
||||
|
||||
foreach ($product_from_order as $product_item) {
|
||||
|
||||
$supplier_organisation_id = Wiaas_Product_Supplier
|
||||
::get_supplier_organisation_id_from_product($product_item->get_product_id());
|
||||
|
||||
Wiaas_User_Organization::assign_post_to_organization($order_id, $supplier_organisation_id);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Access_Management::init();
|
||||
|
||||
@@ -25,6 +25,8 @@ class Wiaas_Admin {
|
||||
|
||||
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-product.php';
|
||||
|
||||
require_once dirname(__FILE__) . '/admin/class-wiaas-admin-supplier.php';
|
||||
|
||||
add_action( 'admin_enqueue_scripts', array(__CLASS__, 'enqueue_scripts'), 100 );
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,8 @@ class Wiaas_DB_Update {
|
||||
'20191019014550' => 'wiaas_db_update_add_general_ui_fields',
|
||||
'20191019014650' => 'wiaas_db_update_add_product_properties_ui_fields',
|
||||
'20181019064450' => 'wiaas_db_update_add_bundle_properties_ui_field',
|
||||
'20191020014650' => 'wiaas_create_organization_roles_capabilities'
|
||||
'20191020014650' => 'wiaas_create_organization_roles_capabilities',
|
||||
'20191030162450' => 'wiaas_db_update_update_supplier_order_capabilities'
|
||||
);
|
||||
|
||||
public static function execute() {
|
||||
|
||||
@@ -310,4 +310,9 @@ function wiaas_admin_create_role_access_groups() {
|
||||
Groups_Group::create(array(
|
||||
'name' => 'admin',
|
||||
));
|
||||
}
|
||||
|
||||
function wiaas_db_update_update_supplier_order_capabilities() {
|
||||
// add supplier role to view orders
|
||||
wp_roles()->add_cap( 'supplier', 'edit_shop_orders' );
|
||||
}
|
||||
@@ -6,8 +6,8 @@ class Wiaas_Product_Supplier {
|
||||
public static function init() {
|
||||
|
||||
add_action('init', array(__CLASS__, 'register_supplier_taxonomy'));
|
||||
add_action('wiaas_organization_created', array(__CLASS__, 'on_organization_added'),20, 2);
|
||||
add_action('wiaas_organization_roles_updated' , array(__CLASS__, 'on_organization_added'),20, 2);
|
||||
add_action('wiaas_organization_created', array(__CLASS__, 'on_organization_added'), 20, 2);
|
||||
add_action('wiaas_organization_roles_updated', array(__CLASS__, 'on_organization_added'), 20, 2);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,7 @@ class Wiaas_Product_Supplier {
|
||||
'labels' => $labels,
|
||||
'show_ui' => true,
|
||||
'show_admin_column' => true,
|
||||
'meta_box_cb' => false,
|
||||
'meta_box_cb' => false,
|
||||
'query_var' => true,
|
||||
'rewrite' => array('slug' => 'template_category'),
|
||||
);
|
||||
@@ -53,15 +53,30 @@ class Wiaas_Product_Supplier {
|
||||
|
||||
$organization = get_term_by('id', $organization_id, 'wiaas-user-organization');
|
||||
|
||||
$supplier = term_exists($organization->slug, 'supplier');
|
||||
$supplier = term_exists($organization->slug, 'supplier');
|
||||
|
||||
if (! $supplier && in_array('supplier', $roles) ) {
|
||||
$supplier = wp_insert_term($organization->name, 'supplier', array(
|
||||
'slug' => $organization->slug
|
||||
));
|
||||
if (!$supplier && in_array('supplier', $roles)) {
|
||||
$supplier = wp_insert_term($organization->name, 'supplier', array(
|
||||
'slug' => $organization->slug
|
||||
));
|
||||
}
|
||||
|
||||
add_term_meta($supplier['term_id'], 'organisation_id', $organization->term_id);
|
||||
add_term_meta($supplier['term_id'], 'organisation_id', $organization->term_id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve organisation id of the supplier of the product
|
||||
*
|
||||
* @param $product_id
|
||||
* @return int organization_id
|
||||
*/
|
||||
public static function get_supplier_organisation_id_from_product($product_id) {
|
||||
|
||||
$supplier_terms = wp_get_object_terms($product_id, 'supplier');
|
||||
$supplier_organisation_slug = $supplier_terms[0]->slug;
|
||||
$supplier_organisation_id = get_term_by('slug', $supplier_organisation_slug, 'wiaas-user-organization')->term_id;
|
||||
|
||||
return $supplier_organisation_id;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user