Last active
January 9, 2018 17:00
-
-
Save damiencarbery/975a9e91bcd95210fd2f8663270daaa5 to your computer and use it in GitHub Desktop.
Allow Viewing of Other Customer Orders in WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'user_has_cap', 'cci_check_view_order_capability', 10, 3 ); | |
function cci_check_view_order_capability( $allcaps, $caps, $args ) { | |
if ( isset( $caps[0] ) ) { | |
switch ( $caps[0] ) { | |
case 'view_order' : | |
// Check that the current user billing_company and order billing company match. | |
$current_user = wp_get_current_user(); | |
$current_user_billing_company = get_user_meta( $current_user->ID, 'billing_company', true ); | |
$order = wc_get_order( $args[2] ); | |
if ( $order && $current_user_billing_company == $order->get_billing_company() ) { | |
$allcaps['view_order'] = true; | |
} | |
break; | |
} | |
} | |
return $allcaps; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_my_account_my_orders_query', 'cci_change_customer_id' ); | |
function cci_change_customer_id( $args ) { | |
$current_user = wp_get_current_user(); | |
$current_user_billing_company = get_user_meta( $current_user->ID, 'billing_company', true ); | |
error_log( 'Current user billing company: '.var_export( $current_user_billing_company, true ) ); | |
// If billing company not set then WP_User_Query() returns all users so do a check first. | |
if ( isset( $current_user_billing_company ) && strlen( $current_user_billing_company ) ) { | |
$user_query = new WP_User_Query( array( 'fields' => 'ID', 'meta_key' => 'billing_company', 'meta_value' => $current_user_billing_company ) ); | |
$same_company_users = $user_query->get_results(); | |
error_log( 'WP_User_Query: ' . var_export( $same_company_users, true ) ); | |
$args[ 'customer' ] = $same_company_users; | |
} | |
else { | |
error_log( 'No current user billing company so skip search for others in the same company.' ); | |
} | |
return $args; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Allow Viewing of Other Customer Orders in WooCommerce | |
Plugin URI: http://www.damiencarbery.com | |
Description: Allow customers to view the orders of other customers if they share the same Billing Company. See http://www.boards.ie/vbulletin/showthread.php?t=2057750045 for background. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
add_filter( 'woocommerce_my_account_my_orders_query', 'cci_change_customer_id' ); | |
function cci_change_customer_id( $args ) { | |
$current_user = wp_get_current_user(); | |
$current_user_billing_company = get_user_meta( $current_user->ID, 'billing_company', true ); | |
error_log( 'Current user billing company: '.var_export( $current_user_billing_company, true ) ); | |
// If billing company not set then WP_User_Query() returns all users so do a check first. | |
if ( isset( $current_user_billing_company ) && strlen( $current_user_billing_company ) ) { | |
$user_query = new WP_User_Query( array( 'fields' => 'ID', 'meta_key' => 'billing_company', 'meta_value' => $current_user_billing_company ) ); | |
$same_company_users = $user_query->get_results(); | |
error_log( 'WP_User_Query: ' . var_export( $same_company_users, true ) ); | |
$args[ 'customer' ] = $same_company_users; | |
} | |
else { | |
error_log( 'No current user billing company so skip search for others in the same company.' ); | |
} | |
return $args; | |
} | |
add_filter( 'user_has_cap', 'cci_check_view_order_capability', 10, 3 ); | |
function cci_check_view_order_capability( $allcaps, $caps, $args ) { | |
if ( isset( $caps[0] ) ) { | |
switch ( $caps[0] ) { | |
case 'view_order' : | |
// Check that the current user billing_company and order billing company match. | |
$current_user = wp_get_current_user(); | |
$current_user_billing_company = get_user_meta( $current_user->ID, 'billing_company', true ); | |
$order = wc_get_order( $args[2] ); | |
if ( $order && $current_user_billing_company == $order->get_billing_company() ) { | |
$allcaps['view_order'] = true; | |
} | |
break; | |
} | |
} | |
return $allcaps; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment