【问题标题】:Woocommerce Category Specific Button on product page产品页面上的 Woocommerce 类别特定按钮
【发布时间】:2017-11-25 20:58:42
【问题描述】:

我下面的代码在单个产品页面上显示一个按钮:

add_action('woocommerce_after_add_to_cart_button','cmk_additional_button');
function cmk_additional_button() {
    echo '<button type=”submit” class=”button alt”>test</button>';
}

这工作正常,并在所有产品上显示此附加按钮。

现在,我该如何限制它,使其仅显示在特定类别内的单个产品页面上,例如仅“商业健身器材”?

谢谢。

【问题讨论】:

  • 试过了吗?也许我的回答会让你走上正轨……

标签: php wordpress button woocommerce product


【解决方案1】:

您可以在代码中添加一些条件,如下所示:

add_action( 'woocommerce_after_add_to_cart_button', 'cmk_additional_button' );
function cmk_additional_button() {
    global $product;

    // Set HERE your product categories (IDs, slugs or Names)
    $category = array( "Commercial Gym Equipment" );

    // Displaying button for single product pages and your defined product category
    if( is_product() && has_term( $category, 'product_cat', $product->get_id() ) ){
        echo '<button type="submit" class="button alt">' . __( "test", "woocommerce" ) . '</button>';
    }
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

此代码经过测试,适用于 WooCommerce 3.0+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 2021-07-05
    • 1970-01-01
    • 2014-11-11
    • 2019-04-29
    • 2018-04-23
    • 2021-02-25
    相关资源
    最近更新 更多