0

The shop page of WooCommerce is based on the archive-product.php file. In this file, a loop is called:

    <?php woocommerce_product_loop_start(); ?>

            <?php woocommerce_product_subcategories(); ?>

            <?php while ( have_posts() ) : the_post(); ?>

                <?php
                    /**
                     * woocommerce_shop_loop hook.
                     *
                     * @hooked WC_Structured_Data::generate_product_data() - 10
                     */
                    do_action( 'woocommerce_shop_loop' );
                ?>

                <?php wc_get_template_part( 'content', 'product' ); ?>

            <?php endwhile; // end of the loop. ?>

In this loop, product data is generated:

public function generate_product_data( $product = null ) {
        if ( ! is_object( $product ) ) {
            global $product;
        }

        if ( ! is_a( $product, 'WC_Product' ) ) {
            return;
        }

        $shop_name       = get_bloginfo( 'name' );
        $shop_url        = home_url();
        $currency        = get_woocommerce_currency();
        $markup          = array();
        $markup['@type'] = 'Product';
        $markup['@id']   = get_permalink( $product->get_id() );
        $markup['url']   = $markup['@id'];
        $markup['name']  = $product->get_name();

        if ( apply_filters( 'woocommerce_structured_data_product_limit', is_product_taxonomy() || is_shop() ) ) {
            $this->set_data( apply_filters( 'woocommerce_structured_data_product_limited', $markup, $product ) );
            return;
        }

        if ( '' !== $product->get_price() ) {
            $markup_offer = array(
                '@type'         => 'Offer',
                'priceCurrency' => $currency,
                'availability'  => 'https://schema.org/' . $stock = ( $product->is_in_stock() ? 'InStock' : 'OutOfStock' ),
                'sku'           => $product->get_sku(),
                'image'         => wp_get_attachment_url( $product->get_image_id() ),
                'description'   => $product->get_description(),
                'seller'        => array(
                    '@type' => 'Organization',
                    'name'  => $shop_name,
                    'url'   => $shop_url,
                ),
            );
...

There is a line in the generate_product_data function calling for the product description.

I'd like to adapt my template or add a function in my functions.php file so the description is no longer added to my product archive page. However, it should be added to my single product page. Product archive page & single product page use the same woocommerce_shop_loop however.

How should I adapt the template or add code to my functions file to remove this product description? I have long descriptions for my products & don't want these to be added to the product archive pages.

3 Answers 3

1

The solution was for the following for the7 template: Solution

If anybody knows how to implement this in the functions.php file so I don't have to update this when my theme updates, shoot!

0

For this specific theme you can add

remove_action('woocommerce_shop_loop_item_desc','dt_woocommerce_template_loop_product_short_desc', 15);

in functions.php of the child theme to remove the action, dt_woocommerce_template_loop_product_short_desc this function is added to woocommerce_shop_loop_item_desc, by using remove_action it'll be removed.

Update: It seems like you are removing action before init, child theme's function is executed before parent theme, kindly try

function remove_parent_action() {                                            
    remove_action('woocommerce_shop_loop_item_desc','dt_woocommerce_template_loop_product_short_desc', 15);
}
add_filter('init', 'remove_parent_action');
4
  • Hi Akshat, I tried to add this function to my functions.php file but it did not work unfortunately. In the meantime, I have gained some more insight about the hooks, which I have posted here: wordpress.stackexchange.com/questions/333556/…
    – BarrieO
    Commented Apr 5, 2019 at 9:36
  • Ideally this should have worked, you'll have to debug if there's anything missed, or you can try adding higher priority to remove action.
    – Akshat
    Commented Apr 6, 2019 at 14:11
  • OK Akshat, I'll give it another try later on. Shouldn't my priority be exactly the same as the which was used when adding the action?
    – BarrieO
    Commented Apr 6, 2019 at 18:40
  • no, the priority should be either equal or greater than the add_action priority.
    – Akshat
    Commented Apr 11, 2019 at 7:55
0

2021 working solution. In your child theme, add this to functions.php

/**
 * Products Shop Archive Short Description
 *
 * @param  string $string
 * @return string
 */

function woo_archive_desc( $string ) {
    global $post;

// Don't display the description on search results page.  

if ( is_search() ) {
    return;
}

// Only run function if on Woo Shop Page

if ( is_post_type_archive( 'product' ) ) {

    $shop_page = get_post( wc_get_page_id( 'shop' ) && in_array( absint( get_query_var( 'paged' ) ), array( 0, 1 ), true ) );
    if ( $shop_page ) {
        $des = $post->post_excerpt;

// do something like empty above var, then echo
     
        echo '<div class="woocommerce-product-details__short-description"><p>' . $des . '</p></div>';

    }
}
}
add_action( 'woocommerce_after_shop_loop_item_title', 'woo_archive_desc', 4, 1 );

Lastly move...

plugins/woocommerce/single-product/short-description.php to themes/{yourthemehere}/woocommerce/single-product/short-description.php

Change this code block adding ! is_product() to condition.

global $post;

$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );

if ( ! $short_description || ! is_product() ) {
    return;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.