为什么复选框不保存值?

Why doesn't the checkbox saving the values?

以前我有一个复选框,它的默认值是自动选中的。

然后我希望复选框根据如下所述的特定条件出现。

 function action_woocommerce_product_options_inventory_product_data($product) {

 if (is_product()){
 return;
 }
 $product = wc_get_product();
 $id = $product->get_id();

//calculating the active period of the product by attribute
$package =  $product->get_attribute( 'pa_package' );
//echo $package;
    switch($package){
            case 'Silver':
            $var = 1*24*60*60;
            break;              
            case 'Gold':
            $var = 60*24*60*60;
            break;
            case 'Platinum':
            $var = 90*24*60*60;
            break;      
            default:
            $var = 1*24*60*60;
            break;
            }

    // Get the date for the product published and current date
    $datetime_created  = $product->get_date_created(); // Get product created datetime
    $timestamp_created = $datetime_created->getTimestamp(); // product created timestamp

    $datetime_now      = new WC_DateTime(); // Get now datetime (from Woocommerce datetime object)
    $timestamp_now     = $datetime_now->getTimestamp(); // Get now timestamp

    $time_delta        = $timestamp_now - $timestamp_created; // Difference in seconds

    if( $time_delta > $var ){
                
 add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data', 10 );                   
global $product_object;
// Get meta
$value = $product_object->get_meta( '_ads_expired' );
// Checkbox
woocommerce_wp_checkbox( array( 
    'id'            => '_ads_expired', // Required, it's the meta_key for storing the value (is checked or not)
    'label'         => __( 'Ads Expired', 'woocommerce' ), // Text in the editor label
    'desc_tip'      => false, // true or false, show description directly or as tooltip
    'description'   => __( 'Check if product expired', 'woocommerce' ), // Provide something useful here
    'value'         => empty( $value ) ? 'yes' : $value // Checked by default
        
) );

 add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );        
 // Save Field
 function action_woocommerce_admin_process_product_object( $product ) {
 // Update meta
 $product->update_meta_data( '_ads_expired', isset( $_POST['_ads_expired'] ) ? 'yes' : 'no' );

 }

 }else{
                
            }
 }

正确显示在每个产品上的复选框(示例在这里>复选框显示在具有银值的包裹属性的产品上,默认复选框显示/已被选中)。

但实际上它并不存储价值。

我在这里错过了什么?

基本上我希望产品有一个复选框,它会根据产品创建时间自动选中。

请帮帮我,

谢谢

我修改了你的代码。试试下面的代码。

function action_woocommerce_product_options_inventory_product_data($product) {

    if (is_product()){
        return;
    }

    $product = wc_get_product();
    $id      = $product->get_id();

    //calculating the active period of the product by attribute
    $package =  $product->get_attribute( 'pa_package' );
    
    switch($package){
        case 'Silver':
            $var = 1*24*60*60;
        break;              
        case 'Gold':
            $var = 60*24*60*60;
        break;
        case 'Platinum':
            $var = 90*24*60*60;
        break;      
        default:
            $var = 1*24*60*60;
        break;
    }

    // Get the date for the product published and current date
    $datetime_created  = $product->get_date_created(); // Get product created datetime
    $timestamp_created = $datetime_created->getTimestamp(); // product created timestamp

    $datetime_now      = new WC_DateTime(); // Get now datetime (from Woocommerce datetime object)
    $timestamp_now     = $datetime_now->getTimestamp(); // Get now timestamp

    $time_delta        = $timestamp_now - $timestamp_created; // Difference in seconds

    if( $time_delta > $var ){
                
        global $product_object;
        // Get meta
        $value = $product_object->get_meta( '_ads_expired' );
        // Checkbox
        woocommerce_wp_checkbox( array( 
            'id'            => '_ads_expired', // Required, it's the meta_key for storing the value (is checked or not)
            'label'         => __( 'Ads Expired', 'woocommerce' ), // Text in the editor label
            'desc_tip'      => false, // true or false, show description directly or as tooltip
            'description'   => __( 'Check if product expired', 'woocommerce' ), // Provide something useful here
            'value'         => empty( $value ) ? 'yes' : $value // Checked by default        
        ) );


    }else{
                
    }
}
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data', 10 );                   

// Save Field
function action_woocommerce_admin_process_product_object( $product ) {
    // Update meta
    $product->update_meta_data( '_ads_expired', isset( $_POST['_ads_expired'] ) ? 'yes' : 'no' );
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object', 10, 1 );