From here :
How to add custom stock status to products in WooCommerce 4+
Woocommerce - Check if product was created less than 60 days ago
Target > Check the product's age, if it has reached x days make the stock 'expired' (costume).
Here the code to make stock custom
//PART1
// Add new stock status options
function filter_woocommerce_product_stock_status_options( $status ) {
// Add new statuses
$status['pre_order'] = __( 'Pre order', 'woocommerce' );
$status['expired'] = __( 'Expired', 'woocommerce' );
return $status;
}
add_filter( 'woocommerce_product_stock_status_options', 'filter_woocommerce_product_stock_status_options', 10, 1 );
// Availability text
function filter_woocommerce_get_availability_text( $availability, $product ) {
// Get stock status
//i try change this to : switch ($product->get_stock_status){
//also deleted
switch( $product->get_stock_status() ) {
case 'pre_order':
$availability = __( 'Pre order', 'woocommerce' );
break;
case 'expired':
$availability = __( 'Expired', 'woocommerce' );
break;
}
return $availability;
}
add_filter( 'woocommerce_get_availability_text', 'filter_woocommerce_get_availability_text', 10, 2 );
// Availability CSS class
function filter_woocommerce_get_availability_class( $class, $product ) {
// Get stock status
//i try change this to : switch ($product->get_stock_status){
//also deleted
switch( $product->get_stock_status() ) {
case 'pre_order':
$class = 'pre-order';
break;
case 'expired':
$class = 'expired';
break;
}
return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );
//PART2
// Admin stock html
function filter_woocommerce_admin_stock_html( $stock_html, $product ) {
// Simple
if ( $product->is_type( 'simple' ) ) {
// Get stock status
//i try change this to : $product->get_stock_status;
//also deleted
$product_stock_status = $product->get_stock_status();
// Variable
} elseif ( $product->is_type( 'variable' ) ) {
foreach( $product->get_visible_children() as $variation_id ) {
// Get product
$variation = wc_get_product( $variation_id );
// Get stock status
//i try change this to : $product->get_stock_status;
//also deleted
$product_stock_status = $variation->get_stock_status();
/*
Currently the status of the last variant in the loop will be displayed.
So from here you need to add your own logic, depending on what you expect from your custom stock status.
By default, for the existing statuses. The status displayed on the admin products list table for variable products is determined as:
- Product should be in stock if a child is in stock.
- Product should be out of stock if all children are out of stock.
- Product should be on backorder if all children are on backorder.
- Product should be on backorder if at least one child is on backorder and the rest are out of stock.
*/
}
}
// Stock status
switch( $product_stock_status ) {
case 'pre_order':
$stock_html = '<mark class="pre-order" style="background:transparent none;color:#33ccff;font-weight:700;line-height:1;">' . __( 'Pre order', 'woocommerce' ) . '</mark>';
break;
case 'expired':
$stock_html = '<mark class="expired" style="background:transparent none;color:#cc33ff;font-weight:700;line-height:1;">' . __( 'Contact us', 'woocommerce' ) . '</mark>';
break;
}
return $stock_html;
}
add_filter( 'woocommerce_admin_stock_html', 'filter_woocommerce_admin_stock_html', 10, 2 );
So, i has edited the code and here's the complete code.
add_filter( 'woocommerce_product_get_stock_status', 'conditional_product_status', 10, 2 );
add_filter( 'woocommerce_product_variation_get_stock_status', 'conditional_product_status', 10, 2 );
function conditional_product_status( $product_stock_status, $product ){
global $product;
$package = $product->get_attribute( 'pa_package' );
switch($package){
case 'Silver':
$var = 14*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($product->is_type('simple')&& $time_delta > $var ){
$product_stock_status = 'expired';
}elseif($product->is_type('variable')&& $time_delta > $var ){
$product_stock_status = 'expired';
}
return $product_stock_status;
}
On the back end, it has changed the stock state and like the way I want it to.
I enabled wp_debug and didn't see any error messages.
The problem is that every time I editing a product (example: change the price, or change the attribute value), and then i update it, that to be always loading and never finishes.
I'm stuck here and don't know what to do. Can someone direct me to the right path?
Thank you