如何在WooCommerce中显示具有变化的所有自定义产品属性

时间:2019-08-11 15:38:44

标签: php wordpress woocommerce

code显示了如何显示自定义产品属性,而没有变化。我也希望在典型的下拉选择菜单中显示所有变体。我该怎么办?

P / S:我不能在此处发布code,因为此网站说我的代码太多,细节也太少。

已更新:

 /**
    * Show all product attributes on the product page
    */
    function isa_woocommerce_all_pa(){

        global $product;
        $attributes = $product->get_attributes();
        if ( ! $attributes ) {
            return;
        }

        $out = '';

        foreach ( $attributes as $attribute ) {

                // skip variations
                if ( $attribute->get_variation() ) {
                    continue;
                }
            $name = $attribute->get_name();
            if ( $attribute->is_taxonomy() ) {

                $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
                // get the taxonomy
                $tax = $terms[0]->taxonomy;
                // get the tax object
                $tax_object = get_taxonomy($tax);
                // get tax label
                if ( isset ($tax_object->labels->singular_name) ) {
                    $tax_label = $tax_object->labels->singular_name;
                } elseif ( isset( $tax_object->label ) ) {
                    $tax_label = $tax_object->label;
                    // Trim label prefix since WC 3.0
                    if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                       $tax_label = substr( $tax_label, 8 );
                    }                
                }
                $out .= $tax_label . ': ';
                $tax_terms = array();
                foreach ( $terms as $term ) {
                    $single_term = esc_html( $term->name );
                    array_push( $tax_terms, $single_term );
                }
                $out .= implode(', ', $tax_terms) .  '<br />';

            } else {
                $out .= $name . ': ';
                $out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
            }
        }
        echo $out;
    }

    add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);

0 个答案:

没有答案