skip to Main Content

Im working with the single product page and I need to have a different image (depending on the category) after the add to cart button.

I tried this code but it doesn’t show me the image that I need

add_action ( 'woocommerce_after_add_to_cart_button', 'content_after_button' );
function content_after_button() {
if (has_term( 'Categoria', 'Accesorios' ) ) {
echo 'https://prueba.soygorrion.com.ar/wp-content/uploads/2019/08/iconos2.jpg';
}

I think im using has_term in the wrong way.

What im trying to accomplish is:
I have a parent category that is “Accesorios” and inside that I have other child categories like “Billeteras”. For each one of this child categories it has to show a diferent image.

thank you

2

Answers


  1. Since you mentioned that the problem is with has_term function. If you can get the product ID inside add to cart, then you can use this code to get categories and check them:

    $categories = wp_get_post_terms($product_id, 'product_cat');
    if(in_array("Accesorios", $categories))
    {
        echo "bla bla";
    }
    

    I have tested this code for another purpose, it works fine. I hope this will help. Please inform me when you test it and if you face any errors update your question with your new code.

    Login or Signup to reply.
  2. First of all there is issue with your has_term checking. If you check has_term docs, you can find it takes first parameter as category term and second parameter as taxonomy.
    Add secondly you are just echo image url that is not going to display image.
    So, do as follows –

    add_action ( 'woocommerce_after_add_to_cart_button', 'content_after_button' );
    function content_after_button() {
        // do check with has_term( $term = '', $taxonomy = '', $post = null )
        if( has_term( 'Accesorios', 'product_cat' ) ) {
            echo '<img src="https://prueba.soygorrion.com.ar/wp-content/uploads/2019/08/iconos2.jpg" />';
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search