How do I exclude specific product ids from woocommerce_product_meta_start function

Viewed 51

I have a function that adds an image to the product meta section on the single product shop page. But now I need to exclude 2 products (by product id) from this function. But I can't figure out how to do that. Here's my current working function:

    add_action( 'woocommerce_product_meta_start', 'content_after_addtocart_button' );
function content_after_addtocart_button() {
    echo '<div class="additional-info"><p><a href="tel:+1-623-931-9965"><span id="et-info-phone">623-931-9965</span></a></p><p>Call us for questions, custom finishes, or to place an order.</p></div><div class="product-summary-image"><a href="https://mcpartsflexpay.com/smt-motorcycle-flexpay/?__hstc=88695364.a91afb3082b76bb991834c6a2202ee7b.1610739322192.1610739322192.1610739322192.1&amp;__hssc=88695364.4.1610739322192&amp;__hsfp=3981434298" target="_blank"><img src="https://www.smtmachining.com/wp-content/uploads/2021/01/FlexPay_Motorcycle-Parts_Financing-CTA.jpg" alt="FlexPay Custom Motorcycle Parts Financing"></a></div>';
}
1 Answers

You can access the global variable $product, which contains the product data. From this global variable, you can access the product ID.

global $product;
if( in_array($product->get_id(), ["13", "14"] ) ){
    return;
}

So your function would be,

add_action( 'woocommerce_product_meta_start', 'content_after_addtocart_button' );
function content_after_addtocart_button() {
    global $product;
    if( in_array($product->get_id(), ["product_id1", "product_id2"] ) ){
      return;
    }
    echo '<div class="additional-info"><p><a href="tel:+1-623-931-9965"><span id="et-info-phone">623-931-9965</span></a></p><p>Call us for questions, custom finishes, or to place an order.</p></div><div class="product-summary-image"><a href="https://mcpartsflexpay.com/smt-motorcycle-flexpay/?__hstc=88695364.a91afb3082b76bb991834c6a2202ee7b.1610739322192.1610739322192.1610739322192.1&amp;__hssc=88695364.4.1610739322192&amp;__hsfp=3981434298" target="_blank"><img src="https://www.smtmachining.com/wp-content/uploads/2021/01/FlexPay_Motorcycle-Parts_Financing-CTA.jpg" alt="FlexPay Custom Motorcycle Parts Financing"></a></div>';
}
Related