October 22, 2024
Chicago 12, Melborne City, USA
PHP

how change schema product yoast on image?


I noticed that the Google snippet does not display a photo of the product. I changed the plugin code, but the test showed no results.

plugin Yoast SEO: WooCommerce code

protected function filter_variations( $data, $product ) {
        if ( ! isset( $data['offers'] ) || $data['offers'] === [] ) {
            return $data;
        }

        $data['@type'] = 'ProductGroup';
        if ( isset( $data['sku'] ) ) {
            $data['productGroupID'] = $data['sku'];
        }
        $data['hasVariant'] = [];
        unset( $data['offers'] );

        $product_variations = $product->get_available_variations();
        foreach ( $product_variations as $key => $variation ) {
            $variant_schema = $this->add_individual_product_variation( $product, $variation, $key );
            if ( isset( $variant_schema['image'] ) ) {
                $this->variation_images[] = [ '@id' => $variant_schema['image']['@id'] ];
            }
            $data['hasVariant'][] = $variant_schema;
        }

        // We don't want an array with keys, we just need the offers.
        $data['hasVariant'] = array_values( $data['hasVariant'] );

        return $data;
    }

my code:

protected function filter_variations( $data, $product ) {
    if ( ! isset( $data['offers'] ) || $data['offers'] === [] ) {
        return $data;
    }

    $data['@type'] = 'ProductGroup';
    if ( isset( $data['sku'] ) ) {
        $data['productGroupID'] = $data['sku'];
    }
    $data['hasVariant'] = [];
    unset( $data['offers'] );

    $product_variations = $product->get_available_variations();
    foreach ( $product_variations as $key => $variation ) {
        $variant_schema = $this->add_individual_product_variation( $product, $variation, $key );
        if ( isset( $variant_schema['image'] ) ) {
            // Изменение структуры для использования простого URL вместо объекта с '@id'
            $this->variation_images[] = $variant_schema['image'];  // Просто URL вместо массива
        }
        $data['hasVariant'][] = $variant_schema;
    }

    // We don't want an array with keys, we just need the offers.
    $data['hasVariant'] = array_values( $data['hasVariant'] );

    return $data;
}

I need the micro markup with the picture to be like this:

"image": "https://dapamoga.by/wp-content/uploads/book.png"

it shows like this:

"image":{"@id":"https://domain/#primaryimage"}



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video