Chia Seeds

Category:
Description

Description

<?php


/**
 * Product Schema for WooCommerce - Good and Grow
 * Add this code to your theme's functions.php or create a custom plugin
 * 
 * This will add complete Product Schema markup for all WooCommerce products
 */

// Main function to add Product Schema
function goodandgrow_add_product_schema() {
    // Only run on single product pages
    if (!is_product()) {
        return;
    }
    
    global $product;
    
    if (!$product) {
        return;
    }
    
    // Get product data
    $product_id = $product->get_id();
    $product_name = $product->get_name();
    $product_description = $product->get_short_description() ? 
        wp_strip_all_tags($product->get_short_description()) : 
        wp_strip_all_tags($product->get_description());
    $product_url = get_permalink($product_id);
    $product_sku = $product->get_sku() ? $product->get_sku() : 'SKU-' . $product_id;
    
    // Get product images
    $product_images = array();
    $main_image = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), 'full');
    if ($main_image) {
        $product_images[] = $main_image[0];
    }
    
    // Get gallery images
    $gallery_ids = $product->get_gallery_image_ids();
    if (!empty($gallery_ids)) {
        foreach ($gallery_ids as $gallery_id) {
            $gallery_image = wp_get_attachment_image_src($gallery_id, 'full');
            if ($gallery_image) {
                $product_images[] = $gallery_image[0];
            }
        }
    }
    
    // Price information
    $regular_price = $product->get_regular_price();
    $sale_price = $product->get_sale_price();
    $price = $product->get_price();
    $currency = get_woocommerce_currency();
    
    // Stock availability
    $stock_status = $product->get_stock_status();
    switch ($stock_status) {
        case 'instock':
            $availability = 'https://schema.org/InStock';
            break;
        case 'outofstock':
            $availability = 'https://schema.org/OutOfStock';
            break;
        case 'onbackorder':
            $availability = 'https://schema.org/BackOrder';
            break;
        default:
            $availability = 'https://schema.org/InStock';
    }
    
    // Get ratings and reviews
    $rating_count = $product->get_rating_count();
    $average_rating = $product->get_average_rating();
    
    // Get product categories
    $categories = get_the_terms($product_id, 'product_cat');
    $category_name = '';
    if ($categories && !is_wp_error($categories)) {
        $category = array_shift($categories);
        $category_name = $category->name;
    }
    
    // Get product weight
    $weight = $product->get_weight();
    $weight_unit = get_option('woocommerce_weight_unit');
    
    // Get product dimensions
    $length = $product->get_length();
    $width = $product->get_width();
    $height = $product->get_height();
    $dimension_unit = get_option('woocommerce_dimension_unit');
    
    // Build the schema array
    $schema = array(
        '@context' => 'https://schema.org/',
        '@type' => 'Product',
        'name' => $product_name,
        'description' => $product_description,
        'url' => $product_url,
        'sku' => $product_sku,
        'mpn' => $product_sku, // Manufacturer Part Number
        'brand' => array(
            '@type' => 'Brand',
            'name' => 'Good and Grow'
        ),
    );
    
    // Add GTIN if available (you can add custom field for this)
    // Uncomment if you have GTIN custom field
    // $gtin = get_post_meta($product_id, '_gtin', true);
    // if ($gtin) {
    //     $schema['gtin13'] = $gtin;
    // }
    
    // Add images
    if (!empty($product_images)) {
        $schema['image'] = count($product_images) > 1 ? $product_images : $product_images[0];
    }
    
    // Add category
    if ($category_name) {
        $schema['category'] = $category_name;
    }
    
    // Add weight if available
    if ($weight) {
        $schema['weight'] = array(
            '@type' => 'QuantitativeValue',
            'value' => $weight,
            'unitCode' => strtoupper($weight_unit)
        );
    }
    
    // Add dimensions if available
    if ($length && $width && $height) {
        $schema['depth'] = array(
            '@type' => 'QuantitativeValue',
            'value' => $length,
            'unitCode' => strtoupper($dimension_unit)
        );
        $schema['width'] = array(
            '@type' => 'QuantitativeValue',
            'value' => $width,
            'unitCode' => strtoupper($dimension_unit)
        );
        $schema['height'] = array(
            '@type' => 'QuantitativeValue',
            'value' => $height,
            'unitCode' => strtoupper($dimension_unit)
        );
    }
    
    // Add offers section
    $schema['offers'] = array(
        '@type' => 'Offer',
        'url' => $product_url,
        'priceCurrency' => $currency,
        'price' => $price,
        'priceValidUntil' => date('Y-12-31', strtotime('+1 year')),
        'availability' => $availability,
        'itemCondition' => 'https://schema.org/NewCondition',
        'seller' => array(
            '@type' => 'Organization',
            'name' => 'Good and Grow',
            'url' => home_url()
        )
    );
    
    // Add shipping details
    $schema['offers']['shippingDetails'] = array(
        '@type' => 'OfferShippingDetails',
        'shippingRate' => array(
            '@type' => 'MonetaryAmount',
            'value' => '0', // Free shipping or add your shipping cost
            'currency' => $currency
        ),
        'shippingDestination' => array(
            '@type' => 'DefinedRegion',
            'addressCountry' => 'IN',
            'addressRegion' => 'Madhya Pradesh'
        ),
        'deliveryTime' => array(
            '@type' => 'ShippingDeliveryTime',
            'handlingTime' => array(
                '@type' => 'QuantitativeValue',
                'minValue' => 0,
                'maxValue' => 1,
                'unitCode' => 'DAY'
            ),
            'transitTime' => array(
                '@type' => 'QuantitativeValue',
                'minValue' => 1,
                'maxValue' => 3,
                'unitCode' => 'DAY'
            )
        )
    );
    
    // Add return policy
    $schema['offers']['hasMerchantReturnPolicy'] = array(
        '@type' => 'MerchantReturnPolicy',
        'returnPolicyCategory' => 'https://schema.org/MerchantReturnFiniteReturnWindow',
        'merchantReturnDays' => 7,
        'returnMethod' => 'https://schema.org/ReturnByMail',
        'returnFees' => 'https://schema.org/FreeReturn'
    );
    
    // Add sale price if exists
    if ($sale_price && $regular_price && $sale_price < $regular_price) {
        $schema['offers']['priceSpecification'] = array(
            '@type' => 'UnitPriceSpecification',
            'price' => $sale_price,
            'priceCurrency' => $currency,
            'referenceQuantity' => array(
                '@type' => 'QuantitativeValue',
                'value' => '1',
                'unitCode' => 'EA'
            )
        );
    }
    
    // Add aggregate rating if available
    if ($rating_count > 0 && $average_rating > 0) {
        $schema['aggregateRating'] = array(
            '@type' => 'AggregateRating',
            'ratingValue' => $average_rating,
            'reviewCount' => $rating_count,
            'bestRating' => '5',
            'worstRating' => '1'
        );
    }
    
    // Add individual reviews if available
    $reviews = get_comments(array(
        'post_id' => $product_id,
        'status' => 'approve',
        'type' => 'review',
        'number' => 10 // Limit to 10 reviews
    ));
    
    if (!empty($reviews)) {
        $schema['review'] = array();
        foreach ($reviews as $review) {
            $review_rating = get_comment_meta($review->comment_ID, 'rating', true);
            if ($review_rating) {
                $schema['review'][] = array(
                    '@type' => 'Review',
                    'reviewRating' => array(
                        '@type' => 'Rating',
                        'ratingValue' => $review_rating,
                        'bestRating' => '5',
                        'worstRating' => '1'
                    ),
                    'author' => array(
                        '@type' => 'Person',
                        'name' => $review->comment_author
                    ),
                    'reviewBody' => wp_strip_all_tags($review->comment_content),
                    'datePublished' => date('Y-m-d', strtotime($review->comment_date))
                );
            }
        }
    }
    
    // Output the schema
    echo "\n<!-- Product Schema by Good and Grow -->\n";
    echo '<script type="application/ld+json">';
    echo wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
    echo '</script>';
    echo "\n<!-- End Product Schema -->\n";
}
add_action('wp_footer', 'goodandgrow_add_product_schema');


/**
 * Optional: Add schema for product variants (Variable Products)
 */
function goodandgrow_variable_product_schema() {
    if (!is_product()) {
        return;
    }
    
    global $product;
    
    if (!$product || $product->get_type() !== 'variable') {
        return;
    }
    
    $variations = $product->get_available_variations();
    $currency = get_woocommerce_currency();
    
    $offers = array();
    
    foreach ($variations as $variation) {
        $variation_obj = wc_get_product($variation['variation_id']);
        if ($variation_obj) {
            $offers[] = array(
                '@type' => 'Offer',
                'url' => get_permalink($product->get_id()),
                'priceCurrency' => $currency,
                'price' => $variation_obj->get_price(),
                'availability' => $variation_obj->is_in_stock() ? 
                    'https://schema.org/InStock' : 
                    'https://schema.org/OutOfStock',
                'sku' => $variation_obj->get_sku(),
                'seller' => array(
                    '@type' => 'Organization',
                    'name' => 'Good and Grow'
                )
            );
        }
    }
    
    if (!empty($offers)) {
        $schema = array(
            '@context' => 'https://schema.org/',
            '@type' => 'Product',
            'name' => $product->get_name(),
            'url' => get_permalink($product->get_id()),
            'offers' => $offers
        );
        
        echo "\n<!-- Variable Product Schema -->\n";
        echo '<script type="application/ld+json">';
        echo wp_json_encode($schema, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
        echo '</script>';
        echo "\n<!-- End Variable Product Schema -->\n";
    }
}
// Uncomment below line if you have variable products
// add_action('wp_footer', 'goodandgrow_variable_product_schema', 11);


/**
 * Optional: Remove WooCommerce default schema if conflicts occur
 */
function goodandgrow_remove_default_wc_schema() {
    // Remove default WooCommerce structured data
    remove_action('wp_footer', array(WC()->structured_data, 'output_structured_data'), 10);
}
// Uncomment below line if you want to remove WooCommerce default schema
// add_action('init', 'goodandgrow_remove_default_wc_schema');
Reviews (0)

Reviews

There are no reviews yet.

Be the first to review “Chia Seeds”

Your email address will not be published. Required fields are marked *