• Resolved serviceweb

    (@serviceweb)


    Hello i use this code to put a badge under featured image but i like to restricte to not display for some specific prouct ID.

    This is my start code and function good

    function skyverge_add_below_featured_image() {
        echo '<h4 class="targhettaprodotto" style="text-align:center;margin-top:10px; background: #c2d69a; padding-top: 8px; padding-bottom: 8px; max-width: 80%; margin:auto; margin-top: 2%;">Targhetta INCLUSA nel prezzo</h4>';
    }
    add_action( 'woocommerce_product_thumbnails' , 'skyverge_add_below_featured_image', 9 );

    If i try to add

    if ( !is_product_ID(868) ) {
    function skyverge_add_below_featured_image() {
    echo ‘<h4 class=”targhettaprodotto” style=”text-align:center;margin-top:10px; background: #c2d69a; padding-top: 8px; padding-bottom: 8px; max-width: 80%; margin:auto; margin-top: 2%;”>Targhetta INCLUSA nel prezzo</h4>’;
    }
    add_action( ‘woocommerce_product_thumbnails’ , ‘skyverge_add_below_featured_image’, 9 );
    }

    Not work. Anyone can help me?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    @serviceweb,

    You need to get the product ID first before running a check for it.

    This StackOverflow answer shows you how to get the product ID – https://stackoverflow.com/a/43472722.

    You can then compare the value in your If statement, e.g:

    if ( $id == 868 ) {
        // do something
    } else {
       // do something else
    }
    Thread Starter serviceweb

    (@serviceweb)

    Hello and thanks for reply but i need not display the function for one specific category and not by ID product. Is not possibile? Thanks

    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    @serviceweb,

    Hello and thanks for reply but i need not display the function for one specific category and not by ID product. Is not possibile? Thanks

    Are you saying that you no longer want to do this based on the product ID and instead want to target a specific category?

    You can use the wc_get_product_category_list function to get a list of categories that a product belongs to and then go from there – https://docs.woocommerce.com/wc-apidocs/function-wc_get_product_category_list.html

    Thread Starter serviceweb

    (@serviceweb)

    Yeah right!

    So i try with this

    function wc_get_product_category_list( $product_id, $sep = ‘, ‘, $before = ”, $after = ” ) {
    return get_the_term_list( $product_id, ‘product_cat’, $before, $sep, $after );
    }
    if ( $id == 868, 857, 788 ) {
    function skyverge_add_below_featured_image() {
    echo ‘<h4 class=”targhettaprodotto” style=”text-align:center;margin-top:10px; background: #c2d69a; padding-top: 8px; padding-bottom: 8px; max-width: 80%; margin:auto; margin-top: 2%;”>Targhetta INCLUSA nel prezzo</h4>’;
    }
    add_action( ‘woocommerce_product_thumbnails’ , ‘skyverge_add_below_featured_image’, 9 );
    } else {
    // do nothing
    }

    But not work form me ??

    Luminus Alabi

    (@luminus)

    Automattic Happiness Engineer

    @serviceweb,

    Copying the code for the wc_get_product_category_list function into your snippet isn’t going to do anything for you. You’re supposed to call that function and pass a product ID to it.

    I’ll suggest that you speak to a developer instead of trying to cobble together bits of code from all over the internet that you don’t have a firm grasp on.

    If you don’t have a go-to developer, you can contact one of the services on the WooCommerce Customizations Page.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add functions for specific product’ is closed to new replies.