【问题标题】:Need some function code for checkout需要一些功能代码进行结帐
【发布时间】:2018-05-02 06:38:10
【问题描述】:

我在 ipad 上的商店中使用我的网站来处理管理员帐户上的商店订单,并且我创建了一个插件,允许管理员在商店中使用称为 POS CASH 和 POS CARD 的新选项结帐,而不是通过网上商户银行。

问题 每次我处理销售时,处理它的管理员帐户都会覆盖其名称、帐单详细信息和运输详细信息。

可能的解决方案 我认为我需要的是子函数 .php 文件中的一些代码行或放置在我的子主题中的修改后的 woocemmerce 文件。

我猜这是结帐时更新用户帐户的挂钩。

任何帮助将不胜感激。我自己做了很多改变,但对搞砸钩子没有信心。

谢谢罗伯

【问题讨论】:

  • 或者如果管理员正在结帐,可能是一行代码不预先填写结帐页面上的客户详细信息?
  • 尝试编写解决方案。
  • 我正在尝试。如果我知道哪个文件具有更新客户详细信息的源代码,那将有所帮助。我认为它的 class-wc-checkout .php 是正确的?
  • 这可以在我的子函数 php 文件中使用吗? $current_user = wp_get_current_user(); if(is_array($current_user->roles) && in_array('administrator',$current_user->roles)){ add_filter('woocommerce_checkout_update_customer_data', '__return_false'); }
  • 是的,应该会阻止更新客户信息。

标签: php function woocommerce checkout


【解决方案1】:

前几天我写这篇文章是因为我代表客户下订单时遇到了类似的问题。它将创建一个新客户并将订单分配给他们,但不会更新当前登录用户的人口统计数据。我进入了感谢页面,因此无论付款是否成功,它都会运行。如果您希望它在成功付款后更改订单,您可以将其更改为使用woocommerce_payment_complete 挂钩。

function es_after_setup_theme() {
    /**
     * Ensure customer billing details are not updated if an admin or shop manager is placing an order
     *
     */
    if( es_check_sales_assistant_user() ) {
        add_filter( 'woocommerce_checkout_update_customer_data', '__return_false' );
    }

}

add_action( 'after_setup_theme', 'es_after_setup_theme' );

function es_check_sales_assistant_user() {
    $user = wp_get_current_user();
    $admin_roles = array( 'administrator', 'shop-manager' );

    if( array_intersect( $admin_roles, $user->roles ) ) { 
        return true;
    }

    return false;
}


function es_create_new_customer( $order_id ) {
    if( es_check_sales_assistant_user() ) {
        $order = wc_get_order($order_id);

        // Create a login for the user and assign the new order to them.
        $email_address = $order->get_billing_email();
        $user_id = email_exists( $email_address );

        // Generate password only letters and numbers
        $random_password = wp_generate_password( 8, false );

        if( ! $user_id ) {
            // Create a new WordPress user, WooCommerce function wc_create_new_customer uses a stipped version of the email address for the username.
            $username = sanitize_user( current( explode( '@', $email_address ) ), true );

            // Ensure username is unique.
            $append     = 1;
            $o_username = $username;

            while ( username_exists( $username ) ) {
                $username = $o_username . $append;
                $append++;
            }

            $userdata = array(
                'user_login'    => $username,
                'user_pass'     => $random_password,
                'user_email'    => $email_address,
                'first_name'    => $order->get_billing_first_name(),
                'last_name'     => $order->get_billing_last_name(),
                'role'          => 'customer',
            );

            $user_id = wp_insert_user( $userdata ) ;

            if( $user_id ) {
                // Add WooCommerce billing details
                $customer = new WC_Customer( $user_id );

                $customer->set_billing_first_name( $order->get_billing_first_name() );
                $customer->set_billing_last_name( $order->get_billing_last_name() );
                $customer->set_billing_address_1( $order->get_billing_address_1() );
                $customer->set_billing_city( $order->get_billing_city() );
                $customer->set_billing_postcode( $order->get_billing_postcode() );
                $customer->set_billing_state( $order->get_billing_state() );
                $customer->set_billing_country( $order->get_billing_country() );
                $customer->set_billing_phone( $order->get_billing_phone() );
                $customer->set_billing_email( $email_address );

                $customer->save();
            }
        }

        // Update order to new customer id
        $order->set_customer_id( $user_id );
        $order->save();
    }
}

add_action( 'woocommerce_thankyou', 'es_create_new_customer' );

【讨论】:

  • 感谢 Andrew,这确实很好用,但发生了一些奇怪的事情。我创建了一个没有名称或详细信息的管理员用户帐户,并使用此帐户处理车间订单。我处理了一个订单,果然它创建了一个新帐户并将订单分配给它。我检查了管理员帐户,它保持不变。都好。然后我处理第二个订单,帐单地址和送货地址显示在结帐时的前一个订单,这很奇怪,因为该信息不在用户帐户上,并且用户帐户上没有订单。我在考虑缓存还是浏览器缓存?
  • 这可能与您的浏览器缓存有关,因为我没有遇到过。
  • 我会在几个不同的设备和浏览器上试用它。它甚至可能是一个自动填充功能。我在 Windows 上使用 chrome。
  • 我使用的是 Chrome,它只会自动填充我的管理员人口统计数据。有一个钩子可以将其从默认值中删除,但在您的情况下,它似乎是从浏览器中获取它们的。
【解决方案2】:

我在浏览器上尝试了所有方法,除了清除购物篮、注销并关闭浏览器、重新登录,然后才清除表单之外没有运气。很奇怪。最后,我使用第二个添加过滤器稍微调整了您的代码,这会在下订单后清除所有内容。非常感谢你的帮助安德鲁。真的很感激:D

if( es_check_sales_assistant_user() ) {
    add_filter( 'woocommerce_checkout_update_customer_data', '__return_false' );
    add_filter('woocommerce_checkout_get_value','__return_empty_string',10);
}

【讨论】:

    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    相关资源
    最近更新 更多