我在编码上说得很新,我难以确定我守则为什么不适当运作。
在WordPress/Woobusiness,我想检查客户是否购买了产品(盒式订阅),如果是,客户可以获取特殊内容。
为此,我利用<编码>用户_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/> ;
}
}
}
}
}
此时此刻,我不知道问题在哪里,我在数据库和<编码>用户_meta<>/code>进行真正的测试时是空的。
我在数据库中检查,空洞。 当我使用以前的<代码>功能.php模板时,数据库填满了正确的索引。 我避开了海滩,没有结果。