skip to Main Content

I need to override Schema ‘availability’ option to Preorder for Woocommerce (3.9.2) products on backorder. Currently WC generates ‘InStock’ for them. Which filters and how do I apply? My current code (below) ruins WordPress, seems like filter is wrong:

function tt_WC_change_schema_availability( $availability ) {
    if ( is_product() && ! is_admin() ) {

        $stock_status = $product->is_in_stock();
        $output = 'OutOfStock'; // default, out of stock

        if ($stock_status){
            $output = 'InStock';
            $qty = $product->get_stock_quantity();
            if  ( ! ($qty > 0) ) {
                $output = 'PreOrder';
            }
        }
        return 'http://schema.org/' . $output;
    }
}

add_filter( 'woocommerce_structured_data_product_offer', 'tt_WC_change_schema_availability' );

2

Answers


  1. Chosen as BEST ANSWER

    Many thanks to @Howard E! I've found out that $stock_status returns a string, also added extra check if product stock's managed. Here's working function:

    function tt_WC_change_schema_availability( $markup_offer, $product ) {
        if ( is_product() && ! is_admin() ) {
            $managed = $product->managing_stock();
    
            if ($managed === TRUE) {
                $stock_status = $product->get_stock_status();
                if ($stock_status == 'instock'){                
                    $output = 'InStock';
                }
                if ($stock_status == 'onbackorder') {
                    $output = 'PreOrder';
                }
                if ($stock_status == 'outofstock') {
                    $output = 'OutOfStock';
                }
            }
            $markup_offer['availability'] = 'http://schema.org/' . $output;
        }
        return $markup_offer;
    }
    
    add_filter( 'woocommerce_structured_data_product_offer', 'tt_WC_change_schema_availability' , 10, 2 );
    

  2. It looks like you are close, but the $product object also has to be included in your function. It’s the second parameter that can be passed to this filter. Also, you want to return part of the array that gets sent to the woocommerce_structured_data_product_offer filter. This would be $availability['availability'] since you’re passing the variable as $availability

    function tt_WC_change_schema_availability( $availability, $product ) {
        if ( is_product() && ! is_admin() ) {
            $stock_status = $product->get_stock_status();
            $output = 'OutOfStock'; // default, out of stock
            if ($stock_status){
                $output = 'InStock';
                $qty = $product->get_stock_quantity();
                if  ( ! ($qty > 0) ) {
                    $output = 'PreOrder';
                }
            }
            return $availability['availability'] = 'http://schema.org/' . $output;
        }
    }
    
    add_filter( 'woocommerce_structured_data_product_offer', 'tt_WC_change_schema_availability' , 10, 2 );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search