提问者:小点点

如何根据产品类别在“添加到购物车”按钮下添加文本


我尝试在某些类别的产品页面上的WooCommerce添加到购物车按钮下面添加一个div。

我有点不知所措。这段代码没有破坏任何东西,但是文本没有显示。

我试过:。woocommerce部门产品表单。购物车::在之后,但这适用于所有产品。

下面是PHP片段:

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() );

有人能指出我代码的问题吗?


共1个答案

匿名用户

您的代码包含一些小错误

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' );