Search code examples
phpwordpresswoocommerceproducthook-woocommerce

How to add text underneath WooCommerce add to cart button based on product category


I try to add a div underneath the WooCommerce Add To Cart button on product pages within certain categories.

I'm a bit at a loss here. This code is not breaking anything, but the text is not showing.

I've tried: .woocommerce div.product form.cart::after, but that applies to all products.

Here's the PHP snippet:

function add_polarity_info() {
    global $product;
    if( is_product() && has_term( ['telecaster-pickups', 'strat-pickups', 'bass-pickups', 'p90-pickups', 'humbuckers', 'dynasonic-pickups', 'mini-humbuckers'], 'product_cat' )) {
    echo 'If you are pairing with pickups from another manufacturer and are concerned about polarity and phasing, please let us know in the NOTES field on the checkout screen';
}};

add_action( 'woocommerce_after_add_to_cart_button', add_polarity_info() );

Can someone point out the problem with my code?


Solution

  • Your code contains some small errors

    function add_polarity_info() {
        global $product;
    
        // Get product id
        $product_id = $product->get_id();
    
        // Set categories
        $cats = array( 'telecaster-pickups', 'strat-pickups', 'bass-pickups', 'p90-pickups', 'humbuckers', 'dynasonic-pickups', 'mini-humbuckers' );
    
        // Condition
        if ( has_term( $cats , 'product_cat', $product_id ) ) {
            echo '<div>If you are pairing with pickups from another manufacturer and are concerned about polarity and phasing, please let us know in the NOTES field on the checkout screen</div>';
        }
    }
    add_action( 'woocommerce_after_add_to_cart_button', 'add_polarity_info' );