WooCommerce schema for variable product

1.2k views Asked by At

I'm trying to make a schema with variation product to output for every variation, price, availability, SKU and itemcondition.
File is place at theme-child/woocommerce/single-product/price.php Is this the right call to make?
Assuming the get_child output is an array?

Thank you!

<?php
if ($product->is_type('variable'))
{
    // do stuff for everything else
    $ids = $product->get_children();
    foreach ($ids as $value)
    {
        $product = wc_get_product($value);
        ?>
        <p class="price"><?php echo $product->get_price_html(); ?></p>
        <meta itemprop="price" content="<?php echo $product->get_price(); ?>" /> 
        <meta itemprop="priceCurrency" content="<?php echo get_woocommerce_currency(); ?>" />
        <link itemprop="availability" href="http://schema.org/<?php echo $product->is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />
        <meta itemprop="sku" content="<?php echo $product->get_sku(); ?>" /> 
        <meta itemprop="itemCondition" content="http://schema.org/NewCondition" /><?php
    }
}
?>
1

There are 1 answers

0
Stefan TwistedDesign On
  1. I don't think is OK to use this code in price.php, because itemscope itemtype="http://schema.org/Product" is already declared for one product.
  2. You should delete the line with $product->get_price_html(), because will add a html output for each variation
  3. Try to use something like this in price.php, to get the schema for one product global $product; if ($product->is_type('variable')){ $ids = $product->get_children(); $prodID = $ids[0]; $producT = wc_get_product($prodID); } ?> get_price_html(); ?>

    is_type('variable')){ echo $producT->get_price(); } else { echo $product->get_price(); } ?>" /> " /> is_in_stock() ? 'InStock' : 'OutOfStock'; ?>" />