English 中文(简体)
拥有数据库的Troubles:表格在数据库中是空的,但该表运行。
原标题:Troubles with database : table is empty in database but it works

我在编码上说得很新,我难以确定我守则为什么不适当运作。

在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模板时,数据库填满了正确的索引。 我避开了海滩,没有结果。

问题回答

在你最近的法典中,首先,你可以节省费用。 BO_access_special_contenttom field as user data, but not as order data, 因此,你无需查询客户订单。

此外,《 h条码》> wo商-排序_status_changed有4 现有论点。 <>strong>last debate is the WC_Order Object $ order/code>, so, 你无需像以前的法典那样获得订单标的。

而是试图:

// 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 ) {
   // 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  );
function TB_add_content() {
    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 阁下需要使用一切可以利用的《世界人权宣言》方法,而不是旧的“语言”后台功能。 新WooCommerce 《高性能储存令》(HPOS)

订阅WooCommerce公司总是试图使用其方法和职能。

Here is the complete revised version from your previous and last code, merged…

测试如下(因为缺乏习惯功能和习惯元数据使用而未测试):

// 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/> ;
            }
        }
    }
}

It should work.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...