【问题标题】:Using update_meta_data on WooCommerce thank you page在 WooCommerce 感谢页面上使用 update_meta_data
【发布时间】:2019-03-01 14:10:19
【问题描述】:

我正在尝试限制对 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";
}

任何人都可以给我任何指导将不胜感激!

谢谢

詹姆斯

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    对我来说看起来不错,只是您需要使用 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";
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-27
      • 2023-03-20
      • 2017-01-14
      • 2018-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多