根据 WooCommerce 订单状态更改更新用户自定义元数据

问题描述 投票:0回答:1

我在编码方面还很陌生,我很难找出我的代码无法正常工作的原因。

在 WordPress/Woocommerce 中,我想检查客户是否购买了产品(盒子订阅),如果是,则客户可以访问特殊内容。

为此,我曾经将信息放入数据库中

user_meta

这是我之前使用过的代码:

// Order check to activate private content
add_action( 'woocommerce_order_status_changed', 'TB_verify_order_content', 10, 3 );
function TB_verify_order_content( $order_id, $old_status, $new_status ) {
    if( $new_status != 'processing' ) return;
    
    $order = wc_get_order( $order_id );
    $user_id = $order->get_user_id();
    
    // Check the product in the order
    $items = $order->get_items();
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
        
        if( $product_id == 18899 ) {

            // Check date user put in form
            $item_meta_data = $item->get_formatted_meta_data( '_', true );
            foreach( $item_meta_data as $the_data ) {
                if( $the_data->key == 'End Date' ) $enddate = $the_data->value;
            }
            
            // Check if it's a renew
            $renew = get_post_meta( $order_id, 'is_a_renew', true );
            $is_a_renew = ( isset( $renew ) && $renew == 'yes' ) ? true : false;
            
            
            if( $enddate != '' ) {
                $first_date = date( 'd/m/Y' );
                
                // Access to private content : setup at first subscription
                if ( !$is_a_renew ) {
                    $tab_access = array( 'access_begining' => time(), 
                                        'access_first_date' => $first_date, 
                                        'access_end' => '-1', 
                                        'access_end_date' => '', 
                                        'end' => $enddate                               
                                        );
                    update_user_meta( $user_id, 'BO_access_content', $tab_access );
                    
                    // End of subscription : subscription ID = Order ID of first order +1
                    $time_end = TB_end_of_subscription( $first_date, $enddate );
                    update_post_meta( $order_id+1, 'expired_date' , $time_end );
                    
                    // Date of last payment : max 25 days before 3 years
                    $time_end = DateTime::createFromFormat( 'd/m/Y', $enddate );
                    $time_end->modify( '- 25 days' );
                    $time_thirtysix = TB_end_of_subscription( $first_date, $enddate, 36 ); // 36th subscription month
                    $tab_payments = array( 'date_dp' => date( 'd/m/Y', $time_end->getTimestamp() ),
                                            'time_adp' => $time_thirtysix
                                            );
                    update_post_meta( $order_id, 'last_payment' , $tab_payments );
                }
                
                // Data to check box and next box
                $num_box = TB_calculate_box( $first_date, $enddate );
                $next_month = date( 'd/m/Y', mktime(0, 0, 0, date('m')+1, 5, date('Y') ) );
                
                $tab_box = array( array( 'box' => $num_box, 'date' => $first_date ) );
                if( $num_box < 36 ) $tab_box[] = array( 'box' => $num_box+1, 'date' => $next_month );

                update_post_meta( $order_id, 'BO_envoi_box', $tab_box );
            }
                }
            }
            
        }
    }
}

// Evaluating subscription month and verify end of subscription
function TB_max_month_autorized( $tab_param ) {
    $return = -1;   
    
    // Ongoing month
    $return = TB_calculate_month( $tab_param['access_first_date'], $tab_param['end'] );
    
    // If subscription stops, changes content access autorization 
    if( $tab_param['access_end'] != '-1' && $tab_param['access_end_date'] != '' ) {
        $return = TB_calculate_month( $tab_param['access_first_date'], $tab_param['acces_end_date'] );
    }
    
    return $return;
}

// Add content in tab "My content"
add_action( 'woocommerce_account_downloads_endpoint', 'TB_add_content' );
function TB_add_content() {
    // Getting user data
    $current_user = wp_get_current_user();
    $access_autorized = false;
    $month_user = -1;
    
    if ( $current_user->ID > 0 ) {
                    
        $tab_access = get_user_meta( $current_user->ID, 'BO_access_content', true );
        
        // Calculation of subscription month and number of box
        if( isset( $tab_access ) && is_array( $tab_access ) && $tab_access['end'] != '' ) {
            $month_user = TB_max_month_autorized( $tab_access );
        } 
                            
        // Subscription ongoing, list of accessible content
        if( $month_user > 0 ) {
            $query = new WP_Query( array(
                'post_type'     => 'page',
                'meta_key'      => '_wp_page_template',
                'meta_value'    => 'templates/template-private_page.php',
                'order'         => 'DESC',
                'orderby'       => 'menu_order',
                'meta_key'      => 'access_month',
                'meta_value'    => $month_user,
                'meta_compare'  => '<=',
                'posts_per_page' => -1,
            ) );
            
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) : $query->the_post();
                    $menu_order = get_post_field('menu_order', get_the_ID());

                    if ($menu_order < 100) {
                        echo '<a class="button content_link" target="_blank" title="'.get_the_title().'" href="'.get_the_permalink().'">';
                            the_title();
                        echo '</a>';
                    
                        echo '<br/>';
                    }
                endwhile; // end of the loop.
            } 
            wp_reset_query();
        }
    }
}

然后我想为另一个产品添加另一个条件,但它停止工作了:(

这是我当前使用的代码:

// Order check to activate private content
add_action( 'woocommerce_order_status_changed', 'TB_verify_order_content', 10, 3 );
function TB_verify_order_content( $order_id, $old_status, $new_status ) {
   // Check New product in the order
    foreach ( $items as $item ) {
        $product_id = $item->get_product_id();
        
        if( $product_id == 19232 ) {
            update_user_meta( $user_id, 'BO_access_special_content', true );
        }
    }
}
    

// Add content in tab "My content"
add_action( 'woocommerce_account_downloads_endpoint', 'TB_add_content' );
function TB_add_content() {

    // Check if user bought product and Display Special Content
    $current_user = wp_get_current_user();
    if ( $current_user->ID > 0 ) {
        $user_orders = wc_get_orders( array( 'customer_id' => $current_user->ID ) );
        foreach ( $user_orders as $user_order ) {
            if ( get_post_meta( $user_order->get_id(), 'BO_access_special_content', true ) ) {                
                $article_ids = array(19232);
                foreach ($article_ids as $article_id) {
                    echo '<a class="button content_link" target="_blank" title="'.get_the_title($article_id).'" href="'.get_permalink($article_id).'">';
                    echo get_the_title($article_id);
                    echo '</a><br/>';
                }
            }
        }
    }
}

此时我不知道问题出在哪里,我检查了数据库,当我进行实际测试时

user_meta
是空的。

我检查了数据库,它是空的。当我使用以前的

function.php
模板时,数据库填充了正确的信息。我清空了缓存,仍然没有结果。

php wordpress woocommerce orders usermetadata
1个回答
0
投票

在您最近的代码中,首先将“BO_access_special_content”自定义字段保存为用户元数据,而不是订单元数据,因此您不需要查询客户订单。

此外,钩子

woocommerce_order_status_changed
4 个可用参数最后一个参数是 WC_Order 对象
$order
,因此您不需要像之前的代码一样获取订单对象)。

如果您不在以前的函数中添加新代码,则需要与以前的现有函数名称不同的唯一函数名称。另外,你的第一个新函数中还缺少一些东西。

相反,请尝试以下代码替换:

// Order check to activate private content
add_action( 'woocommerce_order_status_changed', 'TB_verify_order_content_2', 10, 4 );
function TB_verify_order_content_2( $order_id, $old_status, $new_status, $order ) {    
    if( $new_status !== 'processing' ) return;
    
    $user_id = $order->get_customer_id();

   // Check New product in the order
    foreach ( $order->get_items() as $item ) {
        $product_id = $item->get_product_id();
        
        if( $product_id == 19232 ) {
            update_user_meta( $user_id, 'BO_access_special_content', true );
        }
    }
}

// Add content in tab "My content"
add_action( 'woocommerce_account_downloads_endpoint', 'TB_add_content_2' );
function TB_add_content_2() {
    global $current_user;

    if ( $current_user->ID > 0 ) {

        if ( get_user_meta( $current_user->ID, 'BO_access_special_content', true ) ) {                
            $article_ids = array(19232);

            foreach ($article_ids as $article_id) {
                echo '<a class="button content_link" target="_blank" title="' . get_the_title($article_id) . '" href="'.get_permalink($article_id).'">';
                echo get_the_title($article_id);
                echo '</a><br/>';
            }
        }
    }
}

现在代码应该可以工作了……


由于 WooCommerce 正在将订单迁移到自定义表,因此从 WooCommerce 3 开始,您需要使用所有可用的 CRUD 方法,而不是旧的 WordPress 帖子元函数。对于新的 WooCommerce 高性能订单存储 (HPOS),这是强制性的。

对于 WooCommerce 订阅,也始终尝试使用他们的方法和功能。

这是您之前和最后一个代码的完整修订版本,已合并......

尝试以下操作(未经测试,因为缺少自定义函数和自定义元数据用法):

// Order check to activate private content
add_action( 'woocommerce_order_status_changed', 'TB_verify_order_content', 10, 4 );
function TB_verify_order_content( $order_id, $old_status, $new_status, $order ) {
    if( $new_status !== 'processing' ) return;
    
    $user_id = $order->get_customer_id();
    
    // Check the product in the order
    foreach ( $order->get_items() as $item ) {
        if( $item->get_product_id() == 18899 ) {
            // Check date user put in form
            $end_date = $item->get_meta('End Date');
            
            // Check if it's a renew
            $renew = $order->get_meta('is_a_renew');
            
            
            if( $end_date ) {
                $first_date = date( 'd/m/Y' );
                
                // Access to private content : setup at first subscription
                if ( $renew !== 'yes' ) {
                    update_user_meta( $user_id, 'BO_access_content', array( 
                        'access_begining' => time(), 
                        'access_first_date' => $first_date, 
                        'access_end' => '-1', 
                        'access_end_date' => '', 
                        'end' => $end_date                               
                    ) );
                    
                    // End of subscription : subscription ID = Order ID of first order +1
                    $time_end = TB_end_of_subscription( $first_date, $end_date );

                    // Get the current WC_Subscription from order
                    $subscription = current(wcs_get_subscriptions_for_order( $order_id, array('order_type' => 'any') ));
                    $subscription->update_meta_data( 'expired_date' , $time_end );
                    $subscription->save();
                    
                    // Date of last payment : max 25 days before 3 years
                    $time_end = DateTime::createFromFormat( 'd/m/Y', $end_date );
                    $time_end->modify( '- 25 days' );

                    $order->update_meta_data('last_payment' , array( 
                        'date_dp' => date( 'd/m/Y', $time_end->getTimestamp() ),
                        'time_adp' => TB_end_of_subscription( $first_date, $end_date, 36 ), // 36th subscription month
                    ));
                }
                
                // Data to check box and next box
                $num_box = TB_calculate_box( $first_date, $end_date );
                $next_month = date( 'd/m/Y', mktime(0, 0, 0, date('m')+1, 5, date('Y') ) );
                
                $tab_box = array( array( 'box' => $num_box, 'date' => $first_date ) );
                if( $num_box < 36 ) {
                    $tab_box[] = array( 'box' => $num_box+1, 'date' => $next_month );
                }
                $order->update_meta_data('BO_envoi_box', $tab_box );
                $order->save();
            }
        }
        elseif( $item->get_product_id() == 19232 ) {
            update_user_meta( $user_id, 'BO_access_special_content', true );
        }
    }
}

// Evaluating subscription month and verify end of subscription
function TB_max_month_autorized( $tab_param ) {
    // Ongoing month
    $return = TB_calculate_month( $tab_param['access_first_date'], $tab_param['end'] );
    
    // If subscription stops, changes content access autorization 
    if( $tab_param['access_end'] != '-1' && $tab_param['access_end_date'] != '' ) {
        $return = TB_calculate_month( $tab_param['access_first_date'], $tab_param['acces_end_date'] );
    }
    return $return;
}

// Add content in tab "My content"
add_action( 'woocommerce_account_downloads_endpoint', 'TB_add_content' );
function TB_add_content() {
    global $current_user;
    
    if ( $current_user->ID > 0 ) {
    
        $access_autorized = false;
        $month_user = -1;
                    
        $tab_access = get_user_meta( $current_user->ID, 'BO_access_content', true );
        
        // Calculation of subscription month and number of box
        if( isset( $tab_access ) && is_array( $tab_access ) && $tab_access['end'] != '' ) {
            $month_user = TB_max_month_autorized( $tab_access );
        } 
                            
        // Subscription ongoing, list of accessible content
        if( $month_user > 0 ) {
            $query = new WP_Query( array(
                'post_type'     => 'page',
                'meta_key'      => '_wp_page_template',
                'meta_value'    => 'templates/template-private_page.php',
                'order'         => 'DESC',
                'orderby'       => 'menu_order',
                'meta_key'      => 'access_month',
                'meta_value'    => $month_user,
                'meta_compare'  => '<=',
                'posts_per_page' => -1,
            ) );
            
            if ( $query->have_posts() ) {
                while ( $query->have_posts() ) : $query->the_post();
                    $menu_order = get_post_field('menu_order', get_the_ID());

                    if ($menu_order < 100) {
                        echo '<a class="button content_link" target="_blank" title="'.get_the_title().'" href="'.get_the_permalink().'">';
                            the_title();
                        echo '</a><br/>';
                    }
                endwhile; // end of the loop.
            } 
            wp_reset_query();
        }

        if ( get_user_meta( $current_user->ID, 'BO_access_special_content', true ) ) {                
            $article_ids = array(19232);

            foreach ($article_ids as $article_id) {
                echo '<a class="button content_link" target="_blank" title="' . get_the_title($article_id) . '" href="'.get_permalink($article_id).'">';
                echo get_the_title($article_id);
                echo '</a><br/>';
            }
        }
    }
}

应该可以。

© www.soinside.com 2019 - 2024. All rights reserved.