Skip to content

Instantly share code, notes, and snippets.

@BrianHenryIE
Last active March 14, 2022 23:40
Show Gist options
  • Save BrianHenryIE/6b9051d5cb75632c0ff8fe53a6a280cb to your computer and use it in GitHub Desktop.
Save BrianHenryIE/6b9051d5cb75632c0ff8fe53a6a280cb to your computer and use it in GitHub Desktop.
For Wholesale plugin, in WooCommerce order admin UI, add an action to toggle order between retail and wholesale.
<?php
/**
* Add an action on the WooCommerce orders page to toggle between wholesale and retail orders.
*
* Implemented:
* options 'wholesale'|'retail'
* `$order->add_meta_data('_wwpp_order_type', 'wholesale', true );`
*
* GPL 2+
*/
use WC_Order;
use WWP_Wholesale_Roles;
/**
* @link https://www.skyverge.com/blog/add-woocommerce-custom-order-actions/
*
* Class Wholesale_Order_UI
* @package BrianHenryIE\WP_GetEnhanced_Shop\WooCommerce_wholesale_prices
*/
class Wholesale_Order_UI {
const BH_WP_TOGGLE_ORDER_TO_WHOLESALE_ACTION = 'bh_wp_toggle_order_to_wholesale';
const BH_WP_TOGGLE_ORDER_TO_RETAIL_ACTION = 'bh_wp_toggle_order_to_retail';
/**
* Add "Set to wholesale order" and "Set to retail order" to order actions in admin UI order edit page.
* If neither has already been set, both options appear. If one has been set, the opposite is available to
* toggle to.
*
* @hooked woocommerce_order_actions
* @see class-wc-meta-box-order-actions.php
* @see WC_Meta_Box_Order_Actions::output()
*
* @param string[] $actions Associative array of action_id:Action Name for the WooCommerce order admin view actions.
* @return string[]
*/
public function add_admin_ui_order_action( $actions ): array {
global $theorder;
/**
* This should never be empty because it is set in WC_Meta_Box_Order_Actions::output() which calls this filter.
*
* @var \WC_Order $theorder The order currently being viewed.
*/
$current_wholesale_order_type = $theorder->get_meta( '_wwpp_order_type' );
if ( 'wholesale' !== $current_wholesale_order_type ) {
$actions[ self::BH_WP_TOGGLE_ORDER_TO_WHOLESALE_ACTION ] = 'Set to wholesale order';
}
if ( 'retail' !== $current_wholesale_order_type ) {
$actions[ self::BH_WP_TOGGLE_ORDER_TO_RETAIL_ACTION ] = 'Set to retail order';
}
return $actions;
}
/**
* Action called when shop_manager selects 'Set to wholesale order' in admin order actions drop-down.
*
* @hooked woocommerce_order_action_bh_wp_toggle_order_to_wholesale
*
* @see \WWPP_WC_Order::add_order_type_meta_to_wc_orders()
*
* @param \WC_Order $order The order to update.
*/
public function set_order_to_wholesale( $order ) {
$this->set_order_to( $order, 'wholesale' );
}
/**
* Action called when shop_manager selects 'Set to retail order' in admin order actions drop-down.
*
* @hooked woocommerce_order_action_bh_wp_toggle_order_to_retail
*
* @see \WWPP_WC_Order::add_order_type_meta_to_wc_orders()
*
* @param \WC_Order $order The order to update.
*/
public function set_order_to_retail( $order ) {
$this->set_order_to( $order, 'retail' );
}
/**
* A common function to update the order with the new wwpp type and the user role.
*
* @param WC_Order $order The order to update.
* @param string $wwpp_type 'retail'|'wholesale'.
*/
protected function set_order_to( $order, $wwpp_type ) {
// $this->logger->info('Setting order ' . $order->get_id() . ' to wholesale/retail ').
$order->add_meta_data( '_wwpp_order_type', $wwpp_type, true );
if( 'retail' === $wwpp_type ) {
$order->delete_meta_data('wwp_wholesale_role');
} else {
$order->add_meta_data('wwp_wholesale_role', 'wholesale_customer', true );
}
// I don't know what this is when they're not registered.
$customer_id = $order->get_customer_id();
if ( is_int( $customer_id ) && 0 !== $customer_id ) {
$user = get_user_by( 'id', $customer_id );
$roles = $user->roles;
if ( class_exists( WWP_Wholesale_Roles::class ) ) {
$wholesale_roles = WWP_Wholesale_Roles::getInstance()->getAllRegisteredWholesaleRoles();
} else {
$wholesale_roles = array( 'wholesale_customer' );
}
foreach ( $wholesale_roles as $wholesale_role ) {
if ( in_array( $wholesale_role, $roles, true ) ) {
$order->add_meta_data( 'wwp_wholesale_role', $wholesale_role, true );
break;
}
}
}
$order->save();
// TODO: Add admin notice.
}
}
@jirizari
Copy link

Hey can you tell me where to put this code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment