• Kennt sich hier einer mit Woocommerce und dessen Hooks aus?

    Folgendes Szenario:
    Ich möchte nach der Kurzbeschreibung vom Produkt einen Button mit Link einfügen. Damit ich das aber nicht "per Hand" machen muss, habe ich einen Action Hook gefunden, der genau das macht was ich möchte:

    add_action( 'woocommerce_single_product_summary', 'woo_text_link', 30 );
    function woo_text_link() {
    echo( '<a class="button-category" href="hier-der-link">Zurück zur Übersicht</a>');
    }

    Das Problem dabei ist, das ich für verschiedene Kategorien verschieden Links benötige. Also dachte ich, ich füge noch eine Zeile hinzu, in der nach Kategorien gefiltert wird:

    add_action( 'woocommerce_single_product_summary', 'woo_text_link', 30 );
    function woo_text_link() {
    if( is_product_category( array( 'category1', 'category2' ) ) ) {
    echo( '<a class="button-category" href="hier-der-link">Zurück zur Übersicht</a>');
    }
    }

    Beim ersten wird der Button angezeigt und funktioniert auch. Aber halt bei jedem Produkt. Ich möchte aber, das der Button nur in ganz bestimmten Kategorien zu sehen ist bzw. in anderen Kategorien andere Links. Aber sobald ich die entsprechende Zeile einfüge, verschwindet der Button.

    D.h. das es da einen Fehler gibt, den ich aber nicht erkenne. Kann mir da jmd. von euch auf die Sprünge helfen wo der Fehler liegt?
    ----------
    Ist der Grafiker gut drauf, macht er einen Farbverlauf!
    Geht nicht gibt's nicht. Alles geht. Nur der Frosch hüpft!
    ----------------------------------------------------
    Oh Schreck, Oh Graus, ich fürcht' mich so
    Es gibt ihn doch, den Grüffelo!
      • Ich hab das mal getan...

        phind.com bietet mir folgende Antwort. Keine Ahnung, ob das passt. Ich verstehe nur "Bahnhof":

        ----------------------------------------

        ANSWER | FAST MODEL
        The issue with the code you provided seems to be with the is_product_category function. One possible reason why the button disappears when you add this function is that the current product is not in the specified categories. Here are some steps that may help you:

        • First, make sure that the categories you want to target are correct and exist in your WooCommerce store.
        • You may also try using the has_term function instead of is_product_category to check if the current product belongs to a specific category. Here is an example:

        Show Plain Text
        1. function woo_text_link() {
        2.   global $post;
        3.   if( has_term( array( 'category1', 'category2' ), 'product_cat', $post->ID ) ) {
        4.     echo( '<a class="button-category" href="hier-der-link">Zurück zur Übersicht</a>');
        5.   }
        6. }
        7. add_action( 'woocommerce_single_product_summary', 'woo_text_link', 30 );


        • If you want to add different links for different categories, you can create an associative array with the category slug as the key and the link as the value. You can then check which category the product belongs to and display the corresponding link. Here is an example:

        Show Plain Text
        1. function woo_text_link() {
        2.   global $post;
        3.   $category_links = array(
        4.     'category1' => 'https://link-for-category1.com',
        5.     'category2' => 'https://link-for-category2.com'
        6.   );
        7.   $product_categories = wp_get_post_terms( $post->ID, 'product_cat', array( 'fields' => 'slugs' ) );
        8.   foreach ( $product_categories as $category ) {
        9.     if ( array_key_exists( $category, $category_links ) ) {
        10.       echo '<a class="button-category" href="' . $category_links[ $category ] . '">Zurück zur Übersicht</a>';
        11.       break;
        12.     }
        13.   }
        14. }
        15. add_action( 'woocommerce_single_product_summary', 'woo_text_link', 30 );


        ----------------------------------------

        Vielleicht hilft's ja...
        ----------
        Macerer vom Dienst
        • Das hilft mir tatsächlich weiter.

          Danke, da kann man draus machen.
          ----------
          Ist der Grafiker gut drauf, macht er einen Farbverlauf!
          Geht nicht gibt's nicht. Alles geht. Nur der Frosch hüpft!
          ----------------------------------------------------
          Oh Schreck, Oh Graus, ich fürcht' mich so
          Es gibt ihn doch, den Grüffelo!
      • Hä? Wenn soll ich fragen? Wo oder wie macht man das denn bitte?

        Ich glaube ich werde alt…
        ----------
        Ist der Grafiker gut drauf, macht er einen Farbverlauf!
        Geht nicht gibt's nicht. Alles geht. Nur der Frosch hüpft!
        ----------------------------------------------------
        Oh Schreck, Oh Graus, ich fürcht' mich so
        Es gibt ihn doch, den Grüffelo!
    • Falls hier jmd die Lösung interessiert

      Der Tipp von MarkInTosh war grundsätzlich schon mal nicht verkehrt, hat aber so nicht geklappt. Was mir bei aller Hilfe immer gefehlt hat war ein zweiter Link. Ich wusste, das es was mit "else {" zu tun haben muss und habe mir dann selber was zusammengebastelt und über chatGPT checken lassen. Das ist das funktionierende Ergebnis:

      add_action( 'woocommerce_single_product_summary', 'custom_button_by_categories', 36 ,0 );
      function custom_button_by_categories(){
      global $product;
      // Define your categories in this array (can be IDs, slugs or names)
      $product_cats = array('category1', 'category2', 'category3', 'category4');
      if( has_term( $product_cats, 'product_cat', $product->get_id() ) ){
      $demo_url = get_post_meta( $product->get_id(), 'demo_url', true );
      echo '<a class="button-category" href="hier-der-erste-Link">Zurück zur Übersicht</a>';
      }
      else {
      // Define your other categories in this array (can be IDs, slugs or names)
      $product_cats = array('category5', 'category6', 'category7');
      if ( has_term( $product_cats, 'product_cat', $product->get_id() ) ) {
      $demo_url = get_post_meta( $product->get_id(), 'demo_url', true );
      echo( '<a class="button-category" href="hier-der-zweite-Link">Zurück zur Übersicht</a>');
      }
      }
      }
      ----------
      Ist der Grafiker gut drauf, macht er einen Farbverlauf!
      Geht nicht gibt's nicht. Alles geht. Nur der Frosch hüpft!
      ----------------------------------------------------
      Oh Schreck, Oh Graus, ich fürcht' mich so
      Es gibt ihn doch, den Grüffelo!