php - How to add text underneath WooCommerce add to cart button based on product category
Get the solution ↓↓↓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?
Answer
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' );
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to a member function store() on null
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.