0

I am using the following code for displaying a custom attribute on the Woocommerce shop page. But for some reason, the year is displayed above the product image instead of after the product title (In between the product title and the price).

add_action('woocommerce_after_shop_loop_item_title', 'YearOfMake', 10);

function YearOfMake()
{
    global $product;

    $abv = $product->get_attribute('year-made');
    if (empty($abv))
        return;
    echo __($abv, 'woocommerce');
}

Link: https://groovygarbs.com/cars/

0
add_action('woocommerce_after_shop_loop_item_title', 'YearOfMake', 15);
function YearOfMake()
{
    global $product;
    $attribute  = 'year-made';
    $abv        = $product->get_attribute($attribute);
    if(!empty($abv))
    {
        echo '<p>' . $abv . '</p>';
    }
}
4
  • Hi, thanks for the reply. Unfortunately, it didn't work. :( – TheEvilGuardian Jan 29 '19 at 11:16
  • Please double check this year-made attribute spelling and also check its present or not – Zeshan Jan 29 '19 at 11:45
  • Hi, the attribute becomes visible and I can see the year with my code. But it is not getting displayed after the product title (In between the product title and the price). The result is the same with the code you have given too. Please see the link: groovygarbs.com/cars – TheEvilGuardian Jan 29 '19 at 19:42
  • I saw your link, its product tag not attribute. I posted new answer for that – Zeshan Jan 30 '19 at 6:14
0
add_filter( 'the_title', 'YearOfMake', 10, 2 );
function YearOfMake( $title, $post_id ) 
{

    if ( get_post_type( $post_id ) != 'product' && ! is_archive() )
        return $title;

    if ( ! ( is_shop() || is_product_category() || is_product_tag() ) )
        return $title;
    /*
        global $product;
        $attribute  = 'year-made';
        $abv        = $product->get_attribute($attribute);
        if(!empty($abv))
        {
            $title .= '<br /><p>' . $abv . '</p>';
        }
    */
    $terms  = wp_get_post_terms( $post_id, 'product_tag' );
    $term   = reset( $terms );
    if ( !empty( $term->name ))
    {
        $title .= '<br /><p>' . strtoupper( $term->name ) . '</p>';
    }
    return $title;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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