簡體   English   中英

WooCommerce 產品附加信息簡碼

[英]WooCommerce product additional information shortcode

我是 WooCommerce 的新手,正在尋找一種在帖子頁面上顯示產品屬性的解決方案。 我進行了研究和一些測試,但似乎沒有任何效果。

理想情況下,我想使用短代碼獲取產品 ID並在我的帖子頁面上顯示他的所有產品屬性。 類似於[product_page id="99" display_only_attributes ]

這是在自定義簡碼中獲取產品屬性的方法,您將在其中將產品 ID 定義為簡碼參數id

功能代碼:

if ( ! function_exists( 'display_product_additional_information' ) ) {

    function display_product_additional_information($atts) {

        // Shortcode attribute (or argument)
        $atts = shortcode_atts( array(
            'id'    => ''
        ), $atts, 'product_additional_information' );

        // If the "id" argument is not defined, we try to get the post Id
        if ( ! ( ! empty($atts['id']) && $atts['id'] > 0 ) ) {
           $atts['id'] = get_the_id();
        }

        // We check that the "id" argument is a product id
        if ( get_post_type($atts['id']) === 'product' ) {
            $product = wc_get_product($atts['id']);
        }
        // If not we exit
        else {
            return;
        }

        ob_start(); // Start buffering

        do_action( 'woocommerce_product_additional_information', $product );

        return ob_get_clean(); // Return the buffered outpout
    }

    add_shortcode('product_additional_information', 'display_product_additional_information');

}

代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作。

短代碼用法(更新)

  1. 具有定義的產品 ID:

     [product_additional_information id='37']

    或者在 php 中:

     echo do_shortcode("[product_additional_information id='37']");
  2. 在現有產品頁面中(例如,當“附加信息”產品選項卡被刪除時)

     [product_additional_information]

    或者在 php 中:

     echo do_shortcode("[product_additional_information]");

你會得到這樣的東西:

在此處輸入圖片說明

暫無
暫無

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

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