简体   繁体   中英

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

I'm trying to display the attribute description of an attribute on a single product page in an accordeon. I've read a lot of tutorials and about term description, i tried it via ACF fields, but i didnt' find a way to handle this.

I found this thread here on stackoverflow but it is just for the title of an attribute: Woocommerce - Display single product attribute(s) with shortcodes in Frontend

Is there a solution to display the descripton of an attribute in Woocommerce as a shortcode? Maybe there is one standard woocommerce shortcode?

Here is an example code, I am not making an accordion here, but I am providing $values array with term_id , name , slug & description You can use the array and create an accordion with your own design.

/**
 * 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' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM