vendredi 14 janvier 2022

Add checkbox to product inventory tab in WooCommerce and have the checkbox checked based on certains condition

I have added a checkbox on the inventory tab in which is checked by default and it works fine.

But I need to create another checkbox where the box must be checked based on certain conditions, such as:

  1. The checkbox can be checked by manually before product age expired.
  2. The checkbox will be checked automatically when the product age has expired.
  3. When the product has expired (the checkbox has checked), the checkbox should not be unchecked by manually.

Efforts I've put in to achieve this:

add_action( 'woocommerce_product_options_inventory_product_data','product_custom_fields_package',10,0);
function product_custom_fields_package() {
global $product_object;
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 = 30*24*60*60;
                break;
                case 'Gold':
                $var = 90*24*60*60;
                break;
                case 'Platinum':
                $var = 180*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 ){
                    $valuetest = 'yes';
                }
                elseif($time_delta < $var){
                    $valuetest = 'no';
                }



        $values = $product_object->get_meta('_expired');
        //var_dump($values);
        woocommerce_wp_checkbox( array(
                'id'            => '_expired',
                'label'         => __( 'Statuses', 'woocommerce' ),
                'value'         => empty($values) ? $valuetest : $values,
                'description'   => __( 'Tick if the product has expired ', 'woocommerce' ),
                ) );
        }

//save fields
add_action('woocommerce_admin_process_product_object', 'product_custom_fields_expired_save',10,1);
function product_custom_fields_expired_save( $product ) {

$product->update_meta_data( '_expired', isset($_POST['_expired']) ? 'yes' : 'no' );

}

On the front end (admin), the checkbox has been checked.

And when I change the existing value on the package, the tick in the checkbox also changes dynamically.

But it doesn't store value.

I've also tried other ways like:

function checked_test( $post )
{

$custom_meta = get_post_meta($post->ID, '_expired', true);
}

add_action( 'wp_insert_post', 'save_checkbox_test' );
function save_checkbox_test()
{

global $post;
// Get our form field
if(isset( $_POST['_expired'] ))
{
    $custom = $_POST['_expired'];
    $old_meta = get_post_meta($post->ID, '_expired', true);
    // Update post meta
    if(!empty($old_meta)){
        update_post_meta($post->ID, '_expired', $custom);
    } else {
        add_post_meta($post->ID, '_expired', $custom, true);
      }
    }
  }

But it hasn't worked.

Please guide me how I can reach points 1,2 and 3.

Thank you.




Aucun commentaire:

Enregistrer un commentaire