将Woocommerce订单合并为两个配置文件

时间:2018-07-21 19:48:18

标签: php plugins woocommerce

我开发了一个插件,可用于Admin或Sales Manager提交在Phone或WhatsApp上下的订单。这样,管理员或销售经理可以代表客户提交订单。

我的Woocommerce插件可以检测到电子邮件地址字段,并且如果该特定电子邮件地址属于注册客户,则该订单将显示在该特定客户资料中。

/**
 * Check if WooCommerce is active
 **/
// Get active network plugins - "Stolen" from Novalnet Payment Gateway
function sac_active_nw_plugins() {
if ( !is_multisite() )
    return false;
$sac_activePlugins = ( get_site_option( 'active_sitewide_plugins' ) ) ? array_keys( get_site_option( 'active_sitewide_plugins' ) ) : array();
return $sac_activePlugins;
}
if ( in_array( 'woocommerce/woocommerce.php', (array) get_option( 'active_plugins ') ) || in_array( 'woocommerce/woocommerce.php', (array) sac_active_nw_plugins() ) ) {

//Languages
add_action( 'plugins_loaded', 'shop_as_client_init' );
function shop_as_client_init() {
    load_plugin_textdomain( 'shop-as-client' );
}

//Our field
add_filter( 'woocommerce_billing_fields' , 'shop_as_client_init_woocommerce_billing_fields' );
function shop_as_client_init_woocommerce_billing_fields( $fields ) {
    if ( current_user_can( 'manage_woocommerce' ) && is_checkout() ) {
        //Shop as client?
        $fields['billing_shop_as_client'] = array(
            'label'     => __( 'Shop as client', 'shop-as-client' ),
            'required'  => true,
            'class'     => array( 'form-row-wide' ),
            'clear'     => true,
            'priority'  => 990,
            'type'      => 'select',
            'options'   => array(
                'yes'   => __( 'Yes', 'shop-as-client' ),
                'no'    => __( 'No', 'shop-as-client' ),
            ),
        );
        //Create user if it doesn't exist?
        $fields['billing_shop_as_client_create_user'] = array(
            'label'     => __( 'Create user (if not found by email)?', 'shop-as-client' ),
            'required'  => true,
            'class'     => array( 'form-row-wide' ),
            'clear'     => true,
            'priority'  => 991,
            'type'      => 'select',
            'options'   => array(
                'yes'   => __( 'Yes', 'shop-as-client' ),
                'no'    => __( 'No', 'shop-as-client' ),
            ),
            'default'   => 'no',
        );
    }
    return $fields;
}

//Get order "shop as client"
function shop_as_client_get_order_status( $order ) {
    return 'yes' == version_compare( WC_VERSION, '3.0', '>=' ) ? $order->get_meta( '_billing_shop_as_client' ) : $order->billing_shop_as_client;
}

//Adjust user - Inspiration: https://gist.github.com/twoelevenjay/80294a635969a54e4693
add_filter( 'woocommerce_checkout_customer_id', 'shop_as_client_woocommerce_checkout_customer_id' );
function shop_as_client_woocommerce_checkout_customer_id( $user_id ) {
    if ( current_user_can( 'manage_woocommerce' ) ) {
        if ( isset( $_POST['billing_shop_as_client'] ) && 'yes' == $_POST['billing_shop_as_client'] ) {
             // Check if an exisiting user already uses this email address.
            $user_email = sanitize_email( $_POST['billing_email'] );
            if ( $user = get_user_by( 'email', $user_email ) ) {
                //User found
                $user_id = $user->ID;
            } else {
                //Create user or guest?
                if ( isset( $_POST['billing_shop_as_client_create_user'] ) && 'yes' == $_POST['billing_shop_as_client_create_user'] ) {
                    // Username = Email
                    $user_id = wp_create_user( $user_email, wp_generate_password( 8, false ), $user_email );
                    if ( !is_wp_error( $user_id ) ) {
                        $user_first_name = sanitize_text_field( $_POST['billing_first_name'] );
                        $user_last_name = sanitize_text_field( $_POST['billing_last_name'] );
                        wp_update_user(
                            array( 'ID' => $user_id,
                                'first_name' => $user_first_name,
                                'last_name' => $user_last_name,
                                'display_name' => trim( $user_first_name.' '.$user_last_name ),
                                'role' => 'customer',
                            )
                        );
                    } else {
                        $user_id = 0;
                    }
                } else {
                    $user_id = 0;
                }
            }
        }
    }
    return $user_id;
}

//Information on the order edit screen
add_action( 'woocommerce_admin_order_data_after_order_details', 'shop_as_client_woocommerce_admin_order_data_after_order_details' );
function shop_as_client_woocommerce_admin_order_data_after_order_details( $order ) {
    if ( shop_as_client_get_order_status( $order ) ) {
        ?>
        <p class="form-field form-field-wide">
            <label><?php _e( 'Shop as client', 'shop-as-client' ) ?>:</label>
            <?php _e( 'Yes', 'shop-as-client' ) ?>
        </p>
        <?php
    }
}

}

我的问题是我想将该订单显示为两个配置文件。

  1. 在客户资料中
  2. 在下订单的管理员或销售经理资料中

通过这种方式,我可以检查哪个管理员或销售经理下达了该特定订单。

请帮助

0 个答案:

没有答案