wordpress - 在 WooCommerce 感谢页面上使用 update_meta_data
问题描述
我正在尝试限制对 WooCommerce 感谢页面的访问,因此用户只能查看一次(目前您可以将 URL 粘贴到另一个浏览器中并仍然可以看到它。)
我正在考虑在感谢页面加载后创建/附加一个自定义订单元数据,然后将整个页面模板包装在一个 if 语句中,以检查该元数据是否存在。因此,当他们将其粘贴到另一个浏览器/窗口时,模板会看到此元数据存在并向他们显示不同的消息。
这是正确的方法吗?这是我到目前为止所做的,但它不起作用!
//functions.php
add_action( 'wp_footer', 'hasLoadedPlayPage', 20 );
function hasLoadedPlayPage( $order ){
if( !is_wc_endpoint_url( 'order-received' ) ) return;
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}
任何人都可以给我的任何指导将不胜感激!
谢谢
詹姆士
解决方案
对我来说看起来不错,除了你需要使用add_action('woocommerce_thankyou'...
而不是wp_footer
. 但由于woocommerce_thankyou
只接收订单 id 作为参数,您需要使用$order = wc_get_order($order_id)
来获取 WC_Order 对象。就像是:
//functions.php
add_action( 'woocommerce_thankyou', 'hasLoadedPlayPage', 20, 1);
function hasLoadedPlayPage( $order_id ){
$order = wc_get_order($order_id);
$order->update_meta_data('hasLoadedPlayPage', 'Yes');
$order->save();
}
//thankyou.php
$hasAnswered = get_post_meta($order->ID, 'hasLoadedPlayPage', true);
if(! $hasAnswered){
echo "NOT SEEN";
} else {
echo "SEEN";
}
推荐阅读
- c# - 定位指定的服务器/实例时出错 | 视觉工作室
- swift - 如何在 SKScene 中设置自定义光标?(XCode、Swift4、Spritekit)
- android - 找不到符号类
- jquery - 以下下拉菜单出现在我的 PC 浏览器上,但在我的手机浏览器上不起作用
- python - Spark数据框需要逐行检查值
- office365 - 添加具有生日的新联系人不会添加生日事件
- javascript - 在 Javascript 对象中重构
- git - 是否可以在不更改`git blame`报告的信息的情况下更改代码?
- ios - 在没有主机应用程序的情况下进行单元测试时,我将文件放在哪里?
- python - Python 代码输入验证字典