Previously I had a checkbox whose default value was checked automatically.
Then I want the checkbox to appear based on certain conditions as described below.
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{
}
}
The check box correctly showing on each product (Example is here > the check box has showing on the product that has a packages attribute with a silver value with the default check box display / has been checked).
But in reality it doesn't store value.
What am I missing here?
Essentially I want the product to have a checkbox which is auto-checked based on the time of product creation.
Please help me,
Thank You
Aucun commentaire:
Enregistrer un commentaire