根据用户角色应用自定义WooCommerce订单状态

时间:2016-12-02 作者:Gray Ayer

当订单由用户角色为“批发客户”的客户创建时,我需要创建一个新的客户订单状态为“待定批发”,这是我们通过“WooCommerce批发套件”插件创建的。我有一个创建新订单状态的好代码片段,我只是不知道如何过滤它们的应用程序。

1 个回复
SO网友:Sonali

您可以使用以下代码注册您的客户状态,并在特定用户订购产品时进行更改:

    function register_awaiting_shipment_order_status() {
register_post_status( \'wc-pendingwholesale\', array(
    \'label\'                     => \'Awaiting shipment\',
    \'public\'                    => true,
    \'exclude_from_search\'       => false,
    \'show_in_admin_all_list\'    => true,
    \'show_in_admin_status_list\' => true,
    \'label_count\'               => _n_noop( \'Awaiting shipment <span class="count">(%s)</span>\', \'Awaiting shipment <span class="count">(%s)</span>\' ),
) );
    }
    add_action( \'init\', \'register_awaiting_shipment_order_status\' );

   // Add to list of WC Order statuses
   function add_awaiting_shipment_to_order_statuses( $order_statuses )        {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
    $new_order_statuses[ $key ] = $status;
    $new_order_statuses[\'wc-pendingwholesale\'] = \'Awaiting shipment\';
}

return $new_order_statuses;
  }
  add_filter( \'wc_order_statuses\', \'add_awaiting_shipment_to_order_statuses\' );

  add_action( \'woocommerce_order_status_changed\', \'status_changed_processsing_weighted\' );
  function status_changed_processsing_weighted( $order_id, $checkout = null ) {
  global $woocommerce;
  $order = new WC_Order( $order_id );

  $usermeta = get_user_meta( get_current_user_id() ,\'wp_capabilities\' ,true );
  if ( key( $usermeta ) === \'wholesale_customer\' ) {
        $order->update_status( \'pendingwholesale\', \'order_note\' );
  }
 }

相关推荐