从 functions.php 中的代码检索 wordpress 中的当前 orderid
Retrieving the current orderid in wordpress from code in functions.php
我在 Wordpress / Woocommerce 项目的 functions.php
中有一些代码,我尝试根据用户 ID 和订单 ID 更新自定义数据库 table:
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!");
如您所见,我可以检索当前用户 ID 并使用它,但我无法获取当前订单 ID。
我在 Whosebug 上进行了搜索并尝试了类似的方法:
$order->get_id();
但这不起作用。
我想将当前订单 ID 分配给 $order_id,然后在我目前已注释掉的更新函数中进一步使用它。
订单ID仅存在于前端的订单收到(谢谢)页面和我的帐户>订单查看页面(对于当前用户)。所以你的代码有点不完整,因为它应该在一个钩子函数中,如果在你的主题的 function.php 文件中使用。
现在您可以尝试其中一个挂钩函数,您需要在其中添加与 $updateStuff
变量相关的必要代码。结帐后创建订单时将触发这两个功能。
第一个选择 (你可以直接使用 $order_id
作为参数) :
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) );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。
或者这个(你可以直接使用$order_id
作为参数) :
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) );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。
我在 Wordpress / Woocommerce 项目的 functions.php
中有一些代码,我尝试根据用户 ID 和订单 ID 更新自定义数据库 table:
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!");
如您所见,我可以检索当前用户 ID 并使用它,但我无法获取当前订单 ID。
我在 Whosebug 上进行了搜索并尝试了类似的方法:
$order->get_id();
但这不起作用。
我想将当前订单 ID 分配给 $order_id,然后在我目前已注释掉的更新函数中进一步使用它。
订单ID仅存在于前端的订单收到(谢谢)页面和我的帐户>订单查看页面(对于当前用户)。所以你的代码有点不完整,因为它应该在一个钩子函数中,如果在你的主题的 function.php 文件中使用。
现在您可以尝试其中一个挂钩函数,您需要在其中添加与 $updateStuff
变量相关的必要代码。结帐后创建订单时将触发这两个功能。
第一个选择 (你可以直接使用 $order_id
作为参数) :
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) );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。
或者这个(你可以直接使用$order_id
作为参数) :
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) );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。