I am trying to display a generated link on the thankyou page. The link will take the customer to a page to create a password for an account.
I hook on to woocommerce_before_thankyou and gather the data I need to generate the link.
Then I call update_post_meta() to save the link on the order object.
I then add a woocommerce_thankyou_order_received_text filter where I try to get the link with get_meta(). But the data returned from get_meta is an empty string. And if I look in the db the value is stored correctly and if I refresh the thankyou page the link will show correctly.
// Set a default value
add_action(
'woocommerce_checkout_update_order_meta',
function ($orderId) {
update_post_meta($orderId, '_order_poster_builder_url', '');
}, 1, 1);
add_action('woocommerce_before_thankyou', function ($orderId) {
// get order billing data to generate link from
// I use order billing data to create a new user and a token.
// I use the token as part of the link I am generating.
// This part (the user and token creation) works fine.
$link = BASE_URL . "/activate/$token";
// Set the meta
update_post_meta($orderId, '_order_poster_builder_url', $link);
}, 1, 1);
add_filter('woocommerce_thankyou_order_received_text', function ($thank_you_title, $order)
{
$html = "<div><p>";
$html .= NB_THANK_YOU_TEXT;
$html .= "</p>";
$html .= "<p>Link: " . $order->get_meta('_order_poster_builder_url') . "</p>";
$html .= "</div>";
return $html;
}, 20, 2);
So it seems the meta value is stored in db, but not updated on the order object when trying to get the meta in the title filter.
I have tried to mangle the priorities on the different actions and filters but with no luck.
What am I doing wrong?
question from:
https://stackoverflow.com/questions/65862022/order-meta-stored-with-update-post-meta-is-not-readable-until-after-page-refre 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…