簡體   English   中英

輸出WooCommerce中特定產品類別的自定義簡碼

[英]Output a custom shortcode for a specific product category in WooCommerce

我試圖在我的商店頁面上為我的產品添加一個額外的按鈕,但僅添加到特定類別。 我已經走了這么遠,但我似乎無法正確地稱呼它。

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

任何幫助查明我的錯誤的幫助將不勝感激。

您的代碼中有很多錯誤

您需要在您的條件中以這種方式設置has_term()使其起作用:

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

對於產品ID,也可以這樣: $product->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文件中,或者在任何插件文件中。

現在應該可以工作了……

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM