Skip to Content
Menu

json_ld_ecommerce()

Include additional JSON Linked Data metadata on WooCommerce product pages.

PHP April 27, 2017

Usage

This function runs automatically, so it is not called manually. Is this incorrect?

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /libs/Ecommerce.php on line 278.

    1 Hook

    Find these filters and actions in the source code below to hook into them. Use do_action() and add_filter() in your functions file or plugin.

    Filters
    "pre_nebula_json_ld_ecommerce"
    Need a new filter hook? Request one here.

    Actions
    This function has no action hooks available. Request one?

    PHP
            public function json_ld_ecommerce(){
                $override = apply_filters('pre_nebula_json_ld_ecommerce', false);
                if ( !empty($override) ){echo $override; return;}
    
                if ( function_exists('is_product') && is_product() ){ //if is product
                    global $post;
                    $product = new WC_Product($post->ID);
    
                    $company_type = ( $this->get_option('business_type') )? $this->get_option('business_type') : 'LocalBusiness';
                    ?>
                    <script type="application/ld+json">
                        {
                            "@context": "http://schema.org/",
                            "@type": "Product",
                            "name": "<?php echo esc_html(get_the_title()); ?>",
    
                            <?php $post_thumbnail_meta = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full'); ?>
                            "image": {
                                "@type": "ImageObject",
                                "url": "<?php echo $post_thumbnail_meta[0]; ?>",
                                "width": "<?php echo $post_thumbnail_meta[1]; ?>",
                                "height": "<?php echo $post_thumbnail_meta[2]; ?>"
                            },
    
                            "description": <?php echo wp_json_encode($this->excerpt(array('words' => 100, 'more' => '', 'ellipsis' => false, 'structured' => false))); ?>,
    
                            "offers": {
                                "@type": "Offer",
                                "priceCurrency": "USD",
                                "price": "<?php echo $product->price; ?>",
                                "itemCondition": "http://schema.org/NewCondition",
                                "availability": "<?php echo ( $product->is_in_stock() )? 'http://schema.org/InStock' : 'http://schema.org/OutOfStock'; ?>",
                                "seller": {
                                    "@type": "<?php echo $company_type; ?>",
                                    "name": "<?php echo ( $this->get_option('site_owner') )? $this->get_option('site_owner') : get_bloginfo('name'); ?>",
                                    "image": "<?php echo get_theme_file_uri('/assets/img/logo.png'); ?>",
                                    "telephone": "+<?php echo $this->get_option('phone_number'); ?>",
                                    <?php if ( $company_type === 'LocalBusiness' ): ?>
                                        "priceRange": "",
                                    <?php endif; ?>
                                    "address": {
                                        "@type": "PostalAddress",
                                        "streetAddress": "<?php echo $this->get_option('street_address'); ?>",
                                        "addressLocality": "<?php echo $this->get_option('locality'); ?>",
                                        "addressRegion": "<?php echo $this->get_option('region'); ?>",
                                        "postalCode": "<?php echo $this->get_option('postal_code'); ?>",
                                        "addressCountry": "<?php echo $this->get_option('country_name'); ?>"
                                    }
                                }
                            }
                        }
                    </script>
                    <?php
                }
            }
    

    Override

    To override this PHP function, use this hook in your child theme or plugin ("my_custom" can be changed):

    PHP
    add_filter('pre_nebula_json_ld_ecommerce', 'my_custom_json_ld_ecommerce'); 
    function my_custom_json_ld_ecommerce(){ 
        //Write your own code here
    
        return true; //Return true to prevent the original function from running afterwords
    }

    You can completely disable this PHP function with a single line actions:

    PHP
     add_filter('pre_nebula_json_ld_ecommerce', '__return_false');