wordpresswoocommercestockcustom-taxonomyproduct-variations

Change WooCommerce stock quantity text based on Product Tags and Product Variations


I'm looking how for a woocommerce custom stock quantity tekst based on tags, that is working with variable products, back-orders and stock mangament with the following parameters:

If a product has a specific product tag, I would like to change the "instock" text to 'Stock: ' . $stock. ' pieces. Shipping in 2-3 days' and the "out of stock" text to 'Not on stock, delivery time 20 to 25 working days."

This should work with single products, and variable products on the same time on which back-orders and stock management is enabled. I have at certain products more than 230 variables. As in product: ****

I have tried the following test, and it was working on single products. However on variation level, the products with 1 random tag and the 1101, the line of the 1104 tag gets displayed.

add_filter( 'woocommerce_get_availability_text', 'woo_cust_stock_quantity_text', 99, 2 );

function woo_cust_stock_quantity_text( $availability, $product ) {
    $stock = $product->get_stock_quantity();


    if ( has_term( 1100 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 20 t/m 25 werkdagen voor chroom, 40 t/m 60 werkdagen voor kleur.';
    }
    if ( has_term( 1101 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 20 t/m 25 werkdagen voor chroom, 40 t/m 60 werkdagen voor kleur.';
    }
    if ( has_term( 1102 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 20 t/m 25 werkdagen voor chroom, 40 t/m 60 werkdagen voor Mat-zwart.';
    }
    if ( has_term( 1103 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 1 t/m 5 werkdagen.';
    }
    if ( has_term( 1104 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 4 t/m 7 werkdagen.';
    }
    if ( has_term( 1105 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 7 t/m 14 werkdagen.';
    }
    if ( has_term( 1106 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 3 tot 5 weken.';
    }
    if ( has_term( 1107 , 'product_tag' ) ) {
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 8 tot 12 weken.';
    }
    return $availability;
}

I have tried a lot, but my knowledge is not too deep into PHP to figure it out. Could someone please help me?


Solution

  • In WooCommerce Custom taxonomies like product categories or product tags are not managed by Product Variations, but by their parent Variable Product. Then I have revisited your code a bit to make it work with Product variations too:

    add_filter( 'woocommerce_get_availability_text', 'woo_cust_stock_quantity_text', 99, 2 );
    function woo_cust_stock_quantity_text( $availability, $product ) {
        $stock      = $product->get_stock_quantity();
    
        // Getting the parent product Id for product variations
        $product_id = ( $product->get_type() == 'variation' ) ? $product->get_parent_id() : $product->get_id();
    
        if ( has_term( 1100 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 20 t/m 25 werkdagen voor chroom, 40 t/m 60 werkdagen voor kleur.';
        }
        if ( has_term( 1101 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 20 t/m 25 werkdagen voor chroom, 40 t/m 60 werkdagen voor kleur.';
        }
        if ( has_term( 1102 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 20 t/m 25 werkdagen voor chroom, 40 t/m 60 werkdagen voor Mat-zwart.';
        }
        if ( has_term( 1103 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 1 t/m 5 werkdagen.';
        }
        if ( has_term( 1104 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 4 t/m 7 werkdagen.';
        }
        if ( has_term( 1105 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 7 t/m 14 werkdagen.';
        }
        if ( has_term( 1106 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 3 tot 5 weken.';
        }
        if ( has_term( 1107 , 'product_tag', $product_id ) ) {
           if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Voorraad: ' . $stock. ' stuks. Verwachte levertijd 8 tot 12 weken.';
        }
        return $availability;
    }  
    

    Code goes in functions.php file of the active child theme (or active theme). It should work.