dooid3005 2018-08-14 17:24
浏览 35
已采纳

从functions.php中的代码中检索wordpress中的当前orderid

I have some code in the functions.php of a Wordpress / Woocommerce project, where I try to update a custom database table based on USER ID and ORDER ID:

global $wpdb;
$table = $table="wp_thegraffitiwall";
$user = get_current_user_id();


if($user == 0)
    exit();

echo "Current User ID is ";
echo $user;

echo "Current Order ID is ";


$wpdb->update($table,$updateStuff,['userid'=>$user]);

// $wpdb->update($table,$updateStuff,['userid'=>$user] AND 'orderid'=>#orderid);

exit("Saved!");

As you can see I can retrieve the current USER ID and use it, but I am having trouble getting the current ORDER ID.

I have searched on Stackoverflow and tried things like:

$order->get_id();

But this does not work.

I want to assign the current ORDER ID to the $order_id and then use it in the update function further down which I have currently commented out.

  • 写回答

1条回答 默认 最新

  • doufu5747 2018-08-14 19:32
    关注

    The order ID only exist in Order received (thankyou) page and My Account > Order view pages in front end (for the current user). So your code is a bit incomplete as it should be in a hooked function, if used in function.php file of your theme.

    Now you could try one of this hooked functions instead, where you will need to add the necessary code related to the $updateStuff variable. Both functions will get triggered when order is created after checkout.

    The first alternative (where you can use directly the $order_id as it's an argument):

    add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_update_order', 25, 2 );
    function custom_checkout_update_order( $order_id, $data ) {
        global $wpdb;
    
        // Get the user ID from the Order
        $user_id = get_post_meta( $order_id, '_customer_user', true );
    
        $updateStuff = 'something'; // <===  <===  <===  <=== HERE you add your code
    
        $wpdb->update( 'wp_thegraffitiwall', $updateStuff, array('userid' => $userid) );
    }
    

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

    Or this one (where you can use directly the $order_id as it's an argument):

    add_action( 'woocommerce_thankyou', 'custom_thankyou_action', 20, 1 );
    function custom_thankyou_action( $order_id ) {
        if ( ! $order_id ) return; // Exit
    
        global $wpdb;
    
        // Get the user ID from the Order
        $user_id = get_post_meta( $order_id, '_customer_user', true );
    
        $updateStuff = 'something'; // <===  <===  <===  <=== HERE you add your code
    
        $wpdb->update( 'wp_thegraffitiwall', $updateStuff, array('userid' => $userid) );
    }
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)