【问题标题】:For specific products on WooCommerce orders with completed status, change information from a specific custom page对于具有已完成状态的 WooCommerce 订单上的特定产品,从特定自定义页面更改信息
【发布时间】:2019-10-09 23:43:07
【问题描述】:

我正在使用 "For specific products on WooCommerce orders with completed status, perform an action" 回答我之前的一个问题。

现在,如果用户从列表中购买了产品,并且该产品的订单已完成,我想显示一些特定的内容。如果他没有,用户将在该页面上看到另一个内容。 内容将位于该自定义页面的一个部分中。

感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce restriction user-data


    【解决方案1】:

    所以你需要执行的操作是在满足条件时添加一些自定义的用户元数据,比如:

    add_action('woocommerce_order_status_completed', 'action_on_order_status_completed', 10, 2 );
    function action_on_order_status_completed( $order_id, $order ){
        // Here below your product list
        $products_ids = array('11', '12', '13', '14', '15', '16');
        $found = false;
    
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            if ( in_array($item->product_id(), $products_ids) ) {
                $found = true;
                break;
            }
        }
    
        if ( $found ) {
            $user_id = $order->get_customer_id(); // Get the user ID
            // Add custom user meta data
            update_user_meta( $user_id, 'show_specific_content', '1' );
        }
    }
    

    现在您可以使用以下内容来显示一些特定的内容:

    if( get_user_meta( get_current_user_id(), 'show_specific_content', true ) ) {
        // Display your custom content here
    } else {
        // Display something else here
    }
    

    【讨论】:

    • 太棒了!非常感谢!
    猜你喜欢
    • 2019-10-09
    • 2021-05-23
    • 1970-01-01
    • 2017-12-16
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 2019-08-23
    • 1970-01-01
    相关资源
    最近更新 更多