• emilioicare1

    (@emilioicare1)


    Hi to all.

    I have this function that add a second button to all products in woocomerce and i need that button only in a specific category. I know that i need to use the contitional tag is_product_category () but i dont know to use it.

    All help is welcome.

    Thanks in advance.

    —————————————————————————
    function patricks_woocommerce_call_to_order_button(){
    global $product; //get the product object
    if ( $product ) { // if there’s a product proceed
    $url = esc_url( $product->get_permalink() ); //get the permalink to the product
    echo ‘Pedir info‘; //display a button that goes to the product page
    }
    }
    add_action(‘woocommerce_after_shop_loop_item’,’patricks_woocommerce_call_to_order_button’, 10);
    —————————————————————————-

Viewing 5 replies - 1 through 5 (of 5 total)
  • kursora

    (@kursora)

    is_product_category ( category_slug )

    lorro

    (@lorro)

    $terms = get_the_terms( $product->id, 'product_cat' );
    foreach ($terms as $term) {
      switch ($term->term_id) {
        case 20: // my category id
          // do something for this category only
          break;
        default:
      }
    }
    
    Thread Starter emilioicare1

    (@emilioicare1)

    Ok but ¿how can apply this to the function?

    Thanks!!!

    lorro

    (@lorro)

    <?php
      add_action( 'woocommerce_after_shop_loop_item', 'patricks_woocommerce_call_to_order_button', 10 );
      function patricks_woocommerce_call_to_order_button() {
        if ( is_product_category() ) {
          global $product; // get the product object
          if ( $product ) { // if there’s a product proceed
            $terms = get_the_terms( $product->id, 'product_cat' );
            foreach ($terms as $term) {
              switch ($term->term_id) {
                case 19: // my category id
                  // do something for this category only
                  $url = esc_url( $product->get_permalink() ); // get the permalink to the product
                  echo '<a rel="nofollow" href="'.$url.'" class="call_to_order button">Pedir info</a>'; // display a button that goes to the product page
                  break;
                default:
              }
            }  
          }
        }
      }
    
    Thread Starter emilioicare1

    (@emilioicare1)

    Lorro you are the best!!!

    But for some reason is not working. I will try to research more.

    Thanks a lot!!!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘ADD A BUTTON TO SPECIFIC CATEGORY PRODUCTS WOOCOMERCE’ is closed to new replies.