将分配的唯一客户代码保存到WooCommerce中的访客订单中。

huangapple go评论50阅读模式
英文:

Save assigned unique Customer Code to orders for Guest in WooCommerce

问题

我正在进行一项电子商务项目。当客户购买产品时,根据他们的名字、姓氏和顺序数字,会分配一个客户代码。这个代码会被会计软件使用。

在StackOverflow的帮助下,我纠正了我的代码,并添加了额外的代码来创建一个访客用户和自定义生成的代码。问题在于代码与订单没有关联,例如,当新客户购买产品时(如订单号WC0001979),没有任何东西显示客户代码与订单号(反之亦然)。

我尝试了一下代码。以下是代码。

添加动作('woocommerce_checkout_update_order_meta','include_customer_code_on_order',10,2);

在订单中包括客户代码($ order_id,$ posted_data){
$ order = wc_get_order($ order_id);
$ user_id = $ order->get_user_id();

if($ user_id){
    $ customer_code = get_user_meta($ user_id,'ipx_customer_code',true);
    if($ customer_code){
        $ order->update_meta_data('ipx_customer_code',$ customer_code);
        $ order->save();
    }
}

}

我也尝试将其添加到WooCommerce中的订单屏幕。标签成功 - 我可以看到电子邮件和电话号码下方的“IPX客户代码”。问题在于它显示实际的代码本身。

任何帮助都将不胜感激。

###更新###

在得到一些帮助后,我意识到新代码(来自Loic)只将访客用户链接到订单。应该是每个角色。我修改了代码,现在可以为注册用户和未注册(访客)用户工作。

添加动作('woocommerce_order_status_changed','link_ipx_customer_code_to_the_order',10,4);
链接访客ipx客户代码到订单($ order_id,$ status_from,$ status_to,$ order ){
//检查是否分配了“IPX客户代码”
if(!$ order->get_meta('ipx_customer_code')){
//从账单电子邮件获取WP_User对象
$ user = get_user_by('email',$ order->get_billing_email());

    //检查用户是否存在并具有“IPX客户代码”
    if($ user && isset($ user->ipx_customer_code)){
        $ order->update_meta_data('ipx_customer_code',esc_attr($ user->ipx_customer_code)); //设置IPX代码
        $ order->save(); //保存新的元数据
    }
}

}

在这一点上,我进一步进行了操作,并将客户代码集成到订单屏幕中,使用以下代码:

//在订单屏幕上显示IPX客户代码

添加动作('woocommerce_admin_order_data_after_billing_address','ipx_collection_weight_display_admin_order_meta');

function ipx_collection_weight_display_admin_order_meta($ order){

echo '<p><strong>IPX客户代码:</strong> ' . get_post_meta($ order-&gt;get_id(),'ipx_customer_code',true). '</p>';

}

我还开始将客户代码添加到发送给管理员的电子邮件中,但这还在进行中。

添加动作('woocommerce_email_order_meta','add_ipx_customer_code_to_email',10,3);
将ipx客户代码添加到电子邮件($ order,$ sent_to_admin,$ plain_text){

$ ipx_customer_code = get_post_meta($ order-&gt;get_order_number(),'ipx_customer_code',true);

//如果为空,不要显示任何内容
if(empty($ ipx_customer_code))
    返回;

if($ plain_text === false){
    echo '<ul>
            <li><strong>IPX客户代码:</strong>'。$ ipx_customer_code。'</li>
        </ul>';
} else {
    echo "IPX客户代码:"。$ ipx_customer_code; 
}

}

英文:

I'm working on an e-commerce project. When a customer purchases a product, they are assigned a Customer Code based on their first name, last name and sequential numbers. It's used by the accounting software.

With help from StackOverflow I've both corrected my code and added additional code that creates a Guest User and custom-generated code. The problem is that the code isn't linked to an order, eg. when a new customer buys a product (such as with the order number WC0001979) there isn't anything to show the Customer Code with order number (and vice versa).

I had a stab at the code. This is it.

add_action( &#39;woocommerce_checkout_update_order_meta&#39;, &#39;include_customer_code_on_order&#39;, 10, 2 );

function include_customer_code_on_order( $order_id, $posted_data ) {
    $order = wc_get_order( $order_id );
    $user_id = $order-&gt;get_user_id();

    if ( $user_id ) {
        $customer_code = get_user_meta( $user_id, &#39;ipx_customer_code&#39;, true );
        if ( $customer_code ) {
            $order-&gt;update_meta_data( &#39;ipx_customer_code&#39;, $customer_code );
            $order-&gt;save();
        }
    }
}

I also attempted to add it to the order screen in Woocommerce. The label was successful - I could see "IPX Customer Code" underneath the email and telephone number. The problem is that it display the actual code itself.

Any assistance would be appreciated.

Update

After some help with this, I realised that the new code (from Loic) only linked Guest users to orders. It should be every role. I amended the code and it works for both registered and unregistered (Guest) users.

add_action( &#39;woocommerce_order_status_changed&#39;, &#39;link_ipx_customer_code_to_the_order&#39;, 10, 4 );
function link_guest_ipx_customer_code_to_the_order( $order_id, $status_from, $status_to, $order  ) {
    // Check that no &quot;IPX Customer Code is assigned
    if ( ! $order-&gt;get_meta(&#39;ipx_customer_code&#39;) ) {
        // Get the WP_User object from the billing email
        $user  = get_user_by( &#39;email&#39;, $order-&gt;get_billing_email() );

        // Check that user exist and has a &quot;IPX Customer code&quot;
        if ( $user &amp;&amp; isset($user-&gt;ipx_customer_code) ) {
            $order-&gt;update_meta_data( &#39;ipx_customer_code&#39;, esc_attr($user-&gt;ipx_customer_code) ); // Set the IPX code
            $order-&gt;save(); // save new meta data
        }
    }
}

I went a stage further at that point and integrated the Customer Code into the order screen using the following code:

// Display IPX Customer Code on Order screen

add_action( &#39;woocommerce_admin_order_data_after_billing_address&#39;, &#39;ipx_collection_weight_display_admin_order_meta&#39; );
   
function ipx_collection_weight_display_admin_order_meta( $order ) {    
     
   echo &#39;&lt;p&gt;&lt;strong&gt;IPX Customer Code:&lt;/strong&gt; &#39; . get_post_meta( $order-&gt;get_id(), &#39;ipx_customer_code&#39;, true ) . &#39;&lt;/p&gt;&#39;;
     
}

I've also started to add the Customer Code into emails sent to the admin but it's a work in progress.

add_action( &#39;woocommerce_email_order_meta&#39;, &#39;add_ipx_customer_code_to_email&#39;, 10, 3 );
function add_ipx_customer_code_to_email( $order, $sent_to_admin, $plain_text ){

    $ipx_customer_code = get_post_meta( $order-&gt;get_order_number(), &#39;ipx_customer_code&#39;, true );
    
    // Don&#39;t display anything if it is empty
    if( empty( $ipx_customer_code ) )
        return;
    
    if ( $plain_text === false ) {
        echo &#39;&lt;ul&gt;
                &lt;li&gt;&lt;strong&gt;IPX Customer Code: &lt;/strong&gt;&#39; . $ipx_customer_code . &#39;&lt;/li&gt;
            &lt;/ul&gt;&#39;;
    } else {
        echo &quot;IPX Customer Code: &quot;. $ipx_customer_code; 
    }
    
}

答案1

得分: 1

在我的上一条回答中,我使用了一个自定义函数,通过woocommerce_checkout_order_processed动作钩子(优先级为10)注册了访客用户,使用了WordPress的wp_insert_user()函数,触发了您的函数来生成(并保存为用户元数据)一个唯一的“IPX客户代码”。

因此,我们可以尝试在相同的动作钩子中使用一个具有更高优先级的自定义函数,从用户元数据中获取已注册的“IPX客户代码”。

这样,我们可以确保访客已注册并分配了唯一的“IPX客户代码”。

add_action( 'woocommerce_checkout_order_processed', 'link_guest_ipx_code_to_the_order', 20, 3 );
function link_guest_ipx_code_to_the_order( $order_id, $posted_data, $order ) {
    // 通过账单邮箱获取WP_User对象
    $user  = get_user_by( 'email', $order->get_billing_email() );

    // 检查用户是否存在,并且具有“ipx客户代码”,并且拥有访客用户角色
    if ( $user && isset($user->ipx_customer_code) && in_array( 'guest', $user->roles ) ) {
        $order->update_meta_data( 'ipx_customer_code', esc_attr($user->ipx_customer_code) ); // 设置IPX代码
        $order->update_meta_data( 'guest_user_id', intval($user->ID) ); // 设置访客用户ID(有用)
        $order->save(); // 保存新的元数据
    }
}

将此代码放置在您的活动子主题(或活动主题)的functions.php文件中。未经测试。

如果这不起作用,我们需要在支付处理并且订单获得订单状态后获取它。

在这种情况下,让我们尝试使用一个不同的钩子来实现类似的功能:

add_action( 'woocommerce_order_status_changed', 'link_guest_ipx_code_to_the_order', 10, 4 );
function link_guest_ipx_code_to_the_order( $order_id, $status_from, $status_to, $order  ) {
    // 检查是否没有分配“IPX客户代码”
    if ( ! $order->get_meta('ipx_customer_code') ) {
        // 通过账单邮箱获取WP_User对象
        $user  = get_user_by( 'email', $order->get_billing_email() );

        // 检查用户是否存在,并且具有“ipx客户代码”,并且拥有访客用户角色
        if ( $user && isset($user->ipx_customer_code) && in_array( 'guest', $user->roles ) ) {
            $order->update_meta_data( 'ipx_customer_code', esc_attr($user->ipx_customer_code) ); // 设置IPX代码
            $order->update_meta_data( 'guest_user_id', intval($user->ID) ); // 设置访客用户ID(有用)
            $order->save(); // 保存新的元数据
        }
    }
}

将此代码放置在您的活动子主题(或活动主题)的functions.php文件中。未经测试。

因此,我们能够仅对于“访客”用户角色将“IPX客户代码”(以及“访客用户ID”)添加到每个订单中。

英文:

The context: In my previous answer, I use a custom function hooked in woocommerce_checkout_order_processed action hook with a priority of 10, that registers the guest user using the WordPress wp_insert_user() function, which trigger your function that generate (and save as user metadata) a unique "IPX customer code".

So what we can try is to use a custom function hooked in the same action hook, with a greater priority, to get that registered "IPX customer code" from user metadata.

This way, we are sure that the guest has been registered and has a unique "IPX customer code" assigned.

add_action( &#39;woocommerce_checkout_order_processed&#39;, &#39;link_guest_ipx_code_to_the_order&#39;, 20, 3 );
function link_guest_ipx_code_to_the_order( $order_id, $posted_data, $order ) {
    // Get the WP_User object from the billing email
    $user  = get_user_by( &#39;email&#39;, $order-&gt;get_billing_email() );

    // Check that user exist and has an &quot;ipx customer code&quot; and has a guest user role
    if ( $user &amp;&amp; isset($user-&gt;ipx_customer_code) &amp;&amp; in_array( &#39;guest&#39;, $user-&gt;roles ) ) {
        $order-&gt;update_meta_data( &#39;ipx_customer_code&#39;, esc_attr($user-&gt;ipx_customer_code) ); // Set the Ipx code
        $order-&gt;update_meta_data( &#39;guest_user_id&#39;, int_val($user-&gt;ID) ); // set guest user ID (useful)
        $order-&gt;save(); // save new meta data
    }
}

Code goes in functions.php file of your active child theme (or active theme). Untested.

Now if it doesn't work, we need to get that later on, when payment is processed and order gets an order status.

So in that case, let's try something similar using a different hook:

add_action( &#39;woocommerce_order_status_changed&#39;, &#39;link_guest_ipx_code_to_the_order&#39;, 10, 4 );
function link_guest_ipx_code_to_the_order( $order_id, $status_from, $status_to, $order  ) {
    // Check that no &quot;IPX customer coode is assigned
    if ( ! $order-&gt;get_meta(&#39;ipx_customer_code&#39;) ) {
        // Get the WP_User object from the billing email
        $user  = get_user_by( &#39;email&#39;, $order-&gt;get_billing_email() );

        // Check that user exist and has an &quot;ipx customer code&quot; and has a guest user role
        if ( $user &amp;&amp; isset($user-&gt;ipx_customer_code) &amp;&amp; in_array( &#39;guest&#39;, $user-&gt;roles ) ) {
            $order-&gt;update_meta_data( &#39;ipx_customer_code&#39;, esc_attr($user-&gt;ipx_customer_code) ); // Set the Ipx code
            $order-&gt;update_meta_data( &#39;guest_user_id&#39;, intval($user-&gt;ID) ); // set guest user ID (useful)
            $order-&gt;save(); // save new meta data
        }
    }
}

Code goes in functions.php file of your active child theme (or active theme). Untested.

So here we are able to add the "IPX customer code" (and the "guest user ID") to each order only for "guest" user role.

huangapple
  • 本文由 发表于 2023年6月30日 01:21:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定