woocommerce – trying to return class for out of stock item

I’m having an issue with WooCommerce, trying to return a specific class “sold-out” on an item that is sold out. I’m using a plugin that returns divs instead of a classic drop down. So you can select [S][M][L][XL], etc. Take a look at what I mean – http://www.shultzilla.com/product/tees/greater-than/

So I have a div that I want to add this class to. I will style it so that you can’t click on it, and will make it so that the item cannot be even clicked if the product is sold out.

Read More

Here is what I’m trying to do, but it doesn’t seem to return anything at all. Not even an error:

public function is_in_stock() {
        if ( $this->managing_stock() ) :
            if ( ! $this->backorders_allowed() ) :
                if ( $this->get_total_stock() <  1 ) :
                    return false;
                else :
                    if ( $this->stock_status == 'instock' ) return true;
                    return false;
                endif;
            else :
                return true;
            endif;
        endif;
        if ( $this->stock_status == 'instock' ) return true;
        return false;
    }

function is_sold_out () {
         if ($product->is_in_stock()) {
          $soldOutClass = 'in-stock';
         } else {
          $soldOutClass = 'sold-out';
         }  
    }

public function get_output($placeholder = true, $placeholder_src = 'default') {
        global $woocommerce;


        $out = '<div class="select-option swatch-wrapper '.$soldOutStatus .'" data-value="' . $this->term_slug . '" ' . ($this->selected ? 'data-default="true"' : '') . '>';

        if ($this->type == 'photo' || $this->type == 'image') {

            $out .= '<a href="#" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" title="' . $this->term_label . '">';
            $out .= '<img src="' . $this->thumbnail_src . '" alt="Thumbnail" class="wp-post-image swatch-photo' . $this->meta_key() . '" width="' . $this->width . '" height="' . $this->height . '"/>';
            $out .= '</a>';
        } elseif ($this->type == 'color') {
            $out .= '<a href="#" style="text-indent:-9999px;width:' . $this->width . 'px;height:' . $this->height . 'px;background-color:' . $this->color . ';" title="' . $this->term_label . '">' . $this->term_label  . '</a>';
        } elseif ($placeholder) {
            if ($placeholder_src == 'default') {
                $src = $woocommerce->plugin_url() . '/assets/images/placeholder.png';
            } else {
                $src = $placeholder_src;
            }

            $out .= '<a href="#" style="width:' . $this->width . 'px;height:' . $this->height . 'px;" title="' . $this->term_label . '">';
            $out .= '<img src="' . $src . '" alt="Thumbnail" class="wp-post-image swatch-photo' . $this->meta_key() . '" width="' . $this->width . '" height="' . $this->height . '"/>';
            $out .= '</a>';
        } else {
            return '';
        }

        $out .= '</div>';

        return $out;
    }'

So as you can see, what I’m trying isn’t working. I’m not exactly sure what I’m doing wrong.

Related posts

Leave a Reply

2 comments

  1. Try this function in your template php:

    function is_out_of_stock() {
        global $post;
        $post_id = $post->ID;
        $stock_status = get_post_meta($post_id, '_stock_status',true) == 'outofstock';
    }
    

    And then you can do something like:

    <img class="<?php echo is_out_of_stock()? 'outofstock':''; ?>" src="..."></img>
    
  2. Create a folder in your theme’s directory called /woocommerce, then create another folder inside of this called /loop and finally place the add-to-cart.php file from the plugin inside of this, modifying line 17 to:
    ..class="out-of-stock mycustomclasshere"><?php echo apply_filters('out_of_stock_add_to_cart_text',__( 'Out of Stock','woocommerce'));?>..

    The mycustomclasshere would obviously be your own CSS styling the output.