0

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

carlozsan
  • 53
  • 7
  • The use of `get_stock_status()` in the hook you are using creates an infinite loop. That value is already passed as parameter to the hook so its use is unnecessary – 7uc1f3r Jan 12 '22 at 10:36
  • @7uc1f3r what do you mean a this $id = $product->get_id(); //$product_stock_status = $product->get_stock_status(); or this function conditional_product_status( $product_stock_status, $product ){ please give more clear instructions. – carlozsan Jan 12 '22 at 11:11
  • The use of `$product->get_stock_status();` function in a hook that contains `get_stock_status` causes the function to call the hook while the hook calls the function.. in short, an infinite loop – 7uc1f3r Jan 12 '22 at 11:15
  • @7uc1f3r I've added the custom stock code from your answer (just a little changed on name) as well have tried looking for what you've advice to but to no avail. Can you please mark where I should remove/fix? Thanks – carlozsan Jan 12 '22 at 12:28
  • i got this in [here](https://stackoverflow.com/questions/66074920/show-how-old-the-product-is-on-product-page-using-woocommerce-action-hook) if ( is_a( $product, 'WC_Product' ) ) { I added it at the beginning and now when updating the product it runs normally. Please anyone correct me if I am wrong. thank you @7uc1f3r – carlozsan Jan 12 '22 at 13:15
  • @7uc1f3r I was too happy and didn't check it thoroughly. It's true that the update button is not stuck/normal. On the product page (admin) the stock changes to special stock, but on the product detail page (admin) the stock status does not change. What am I missing here? – carlozsan Jan 12 '22 at 14:47

0 Answers0