• Resolved kerosen2023

    (@kerosen2023)


    Hi is there a way to display when stock is below 4 pcs text Only 4,3,2 lleft in stock in this code, and when is only one to display last in stock?

    thank you

    //* Add stock status to archive pages
    function envy_stock_catalog() {
        global $product;
        //check the variable products
        if ( $product->get_type() == 'variable' ) { 
        	foreach ( $product->get_available_variations() as $key ) {
                $attr_string = array();
                foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                    $attr_string[] = $attr_value;
                }
                if ( $key['max_qty'] > 0 ) { 
                  $var_result = true;
                } else { 
                  $var_result = false;
                }
            }
        }
        //var_dump($var_result);
    	//var_dump($product->get_available_variations());
        if ( ($product->is_in_stock() && $product->get_stock_quantity() > 0) || $var_result == true ) {
            echo '<div class="stock" >' . __( ' Skladom', 'envy' ) . '</div>';
        } elseif($product->is_in_stock() && $product->get_stock_quantity() < 1) {
    		echo '<div class="stock stock-order-only" >' . __( ' Na objednávku', 'envy' ) . '</div>';
    	} else {
            echo '<div class="out-of-stock" >' . __( 'Nie je na sklade', 'envy' ) . '</div>';
        }
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi @kerosen2023,

    I have modified the code to display ‘Only x left in stock’, when there are 2,3 or 4 pieces left in stock and ‘Last stock’ where there is 1 piece left.

    //* Add stock status to archive pages
    function envy_stock_catalog() {
        global $product;
        //check the variable products
        if ( $product->get_type() == 'variable' ) { 
        	foreach ( $product->get_available_variations() as $key ) {
                $attr_string = array();
                foreach ( $key['attributes'] as $attr_name => $attr_value ) {
                    $attr_string[] = $attr_value;
                }
                if ( $key['max_qty'] > 0 ) { 
                  $var_result = true;
                } else { 
                  $var_result = false;
                }
            }
        }
    
        if ( ($product->is_in_stock() && $product->get_stock_quantity() > 0) || $var_result == true ) {
    		if ($product->get_stock_quantity() < 5 && $product->get_stock_quantity() > 1) {
    			echo '<div class="stock" >' . __( ' Only '. $product->get_stock_quantity() . ' left in stock', 'envy' ) . '</div>';
    		}
    		if ($product->get_stock_quantity() === 1) {
    			echo '<div class="stock" >' . __( 'Last stock', 'envy' ) . '</div>';
    		}
        } elseif($product->is_in_stock() && $product->get_stock_quantity() < 1) {
    		echo '<div class="stock stock-order-only" >' . __( 'On request'. $product->get_stock_quantity(). 'lol', 'envy' ) . '</div>';
    	} else {
            echo '<div class="out-of-stock" >' . __( 'Not in stocl', 'envy' ) . '</div>';
        }
    }
    add_action( 'woocommerce_after_shop_loop_item_title', 'envy_stock_catalog' );

    Please let me know if this works the way you expect it to!

    You can also display stock availability using WooCommerce settings > Products > Inventory. Please refer to the WooCommerce official documentation for more information.

    I hope this helps!

    Hi @kerosen2023

    Thanks for reaching out!

    I did some research and found the code snippet below (modified to your text) from this article that worked on my site:

    /**
     * @snippet       Display "Quantity: #" @ WooCommerce Single Product Page
     * @how-to        Get CustomizeWoo.com FREE
     * @author        Rodolfo Melogli
     * @testedwith    WooCommerce 4.4
     * @donate $9     https://businessbloomer.com/bloomer-armada/
     */
      
    add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
      
    function bbloomer_custom_get_availability_text( $availability, $product ) {
       $stock = $product->get_stock_quantity();
       if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Only ' . $stock . ' left in stock.';
       return $availability;
    }

    Output:

    Hope this helps!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Custom code stock’ is closed to new replies.