【问题标题】:Output a custom shortcode for a specific product category in WooCommerce在 WooCommerce 中为特定产品类别输出自定义简码
【发布时间】:2018-01-25 18:28:16
【问题描述】:

我正在尝试在我的商店页面上为我的产品添加一个额外的按钮,但仅限于特定类别。我已经做到了这一点,但我似乎无法正确调用它。

if (!function_exists('add_accessory_button')){
function add_accessory_button() {
  global $product;
  $product_cat = $product->product_cat;

  if( has_term( 'dealqualify',$product_cat ) && in_the_loop() ) {
    $link = get_post_meta( $product->ID, ‘acc_link’, true );
    echo do_shortcode('<br>[button link="' . esc_attr($link) . '"]View Accessory[/button]');
} }
add_action('woocommerce_after_shop_loop_item','add_accessory_button');
}

任何帮助我指出我的错误将不胜感激。

【问题讨论】:

  • 刚刚更新了短代码行代码(这样看起来更好)......但如果它对你有用,不要改变任何东西。

标签: php wordpress woocommerce categories shortcode


【解决方案1】:

你的代码有很多错误

您需要在您的条件下设置 has_term() 以使其工作:

has_term( 'dealqualify', 'product_cat', $product->get_id() )

对于产品 ID 也这样做:$product-&gt;get_id()...我也尝试更正其他错误...

所以你的代码应该是这样的:

if ( ! function_exists('add_accessory_button')){

    function add_accessory_button() {
        global $product;
        if( has_term( 'dealqualify', 'product_cat', $product->get_id() ) && in_the_loop() ) {
            $link = get_post_meta( $product->get_id(), 'acc_link', true );
            echo '<br>' . do_shortcode("[button link='$link']View Accessory[/button]");
    }
    add_action('woocommerce_after_shop_loop_item','add_accessory_button');
}

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

现在应该可以了……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2018-08-19
    • 2023-04-08
    • 2017-11-13
    • 1970-01-01
    • 2016-07-16
    相关资源
    最近更新 更多