簡體   English   中英

Woocommerce - 在前端顯示帶有簡碼的單個產品屬性描述

[英]Woocommerce - Display single product attribute(s) description with shortcodes in Frontend

我正在嘗試在手風琴的單個產品頁面上顯示屬性的屬性描述。 我已經閱讀了很多教程和術語描述,我通過 ACF 字段進行了嘗試,但我沒有找到處理這個問題的方法。

我在stackoverflow上找到了這個線程,但它只是一個屬性的標題: Woocommerce - 在前端顯示帶有短代碼的單個產品屬性

是否有解決方案將 Woocommerce 中的屬性描述顯示為簡碼? 也許有一個標准的 woocommerce 短代碼?

這是一個示例代碼,我不是在這里制作手風琴,而是提供帶有term_idnameslugdescription$values數組。您可以使用該數組並根據自己的設計創建手風琴。

/**
 * Callback to `vkh_display_attribute_accordian` shortcode.
 *
 * @param array $atts Shortcode attributes.
 * @return string Shortcode output.
 * @throws Exception Throws an error when available.
 */
function vkh_display_attribute_accordian_shortcode( $atts ) {
    // Don't run if admin page or not a single product page.
    if ( is_admin() || ! is_product() ) {
        return;
    }

    // Prepare shortcode attributes.
    $atts = shortcode_atts(
        array(
            'name' => '',
        ),
        $atts,
        'vkh_display_attribute_accordian'
    );

    try {
        if ( empty( $atts['name'] ) ) {
            throw new Exception( __( 'Please pass the attribute slug as name attribute. e.g. [vkh_display_attribute_accordian name="#YOUR_ATTRIBUTE_SLUG"].', 'text-domain' ) );
        }

        global $product;
        $attributes = $product->get_attributes();
        if ( empty( $attributes ) ) {
            throw new Exception( __( 'Product does not have any attributes.', 'text-domain' ) );
        }

        $selected_attr = '';

        foreach ( $attributes as $attribute ) {
            if ( $attribute->is_taxonomy() && $attribute->get_name() === $atts['name'] ) {
                $selected_attr = $attribute;
                break;
            }
        }

        if ( empty( $selected_attr ) || ! $selected_attr->is_taxonomy() ) {
            throw new Exception( __( 'Unable to find matching attributes.', 'text-domain' ) );
        }

        $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );
        if ( empty( $attribute_values ) ) {
            throw new Exception( __( 'Product does not have any attribute values.', 'text-domain' ) );
        }

        $values = array_map(
            function( $_term ) {
                return (object) array(
                    'term_id'     => $_term->term_id,
                    'name'        => $_term->name,
                    'slug'        => $_term->slug,
                    'description' => term_description( $_term ),
                );
            },
            $attribute_values
        );

        ob_start();
        var_dump( $values );
        $content = ob_get_clean();

        return '<pre>'.$content.'</pre>';
    } catch ( Exception $e ) {
        if ( $e->getMessage() ) {
            if ( current_user_can( 'manage_options' ) ) {
                return '<p>' . esc_html( $e->getMessage() ) . '</p>';
            }
        }
    }
}
add_shortcode( 'vkh_display_attribute_accordian', 'vkh_display_attribute_accordian_shortcode' );

暫無
暫無

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

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