首页 文章

Woocommerce定制单一产品视图

提问于
浏览
1

我正在进行woocommerce定制 . 但我现在还不够好,如何获得产品的属性 . 我还想获得属性存档页面的链接 .

我把woocommerce文件夹放在我的主题文件夹中 . 这样我就可以做出自定义行为 . 在这个woocommerce文件夹中是一个文件theme-name / woocommerce / single-product / tabs / tabs.php

此文件使用打印产品说明 . 之后,我输入了代码片段来打印类别名称及其描述 . 我还希望它打印产品的属性 . (最好是链接到它的存档页面) .

有没有办法使用片段的一部分来回应产品的属性?

global $post;
    $terms = get_the_terms( $post->ID, 'product_cat' );
    $attribute_names = array( 'Producten');
        foreach ( $attribute_names as $attribute_name ) {
                    $taxonomy = get_taxonomy( $attribute_name );
                    if ( $taxonomy && ! is_wp_error( $taxonomy ) ) {
                            $terms = wp_get_post_terms( $post->ID, $attribute_name );
                            $terms_array = array();
                    if ( ! empty( $terms ) ) {
                            foreach ( $terms as $term ) {
                                   $archive_link = get_term_link( $term->slug, $attribute_name );
                                   $full_line = '<a href="' . $archive_link . '">'. $term->name . '</a>';
                                   array_push( $terms_array, $full_line );
                            }
                            echo $taxonomy->labels->name . ' ' . implode( $terms_array, ', ' );
                    }
            }
        }
    foreach ($terms as $term) {                             
        $name = $term->name;
        $desc = $term->description;

        break;
    }

echo $name;
echo '<br>';
echo $desc;

1 回答

  • 0

    你需要编写这样的代码,不完全是这样的,你需要在这里进行一些修改,我只在我的工作中发布了一些代码部分,在那里我正在检索重量和价格 . 在参数中你可以根据需要添加类别 . 如果您遇到任何错误或问题,请在使用此代码后告诉我,因为我还没有测试过 .

    <?php
             $arg = array('posts_per_page' => -1, 'numberposts' => -1, 'post_type' => 'product');
            $products = get_posts($arg);
            <?php $product_obj = new WC_Product_Factory();  ?>  <?php if ($products): ?>
            <?php foreach($products as $product): ?><?php $product = $product_obj->get_product($product); ?>
    
           <?php if ($product->product_type == 'variable'): ?>
            <?php $children = $product->get_children( $args = '', $output = OBJECT ); ?>
    
            <?php
            foreach ($children as $key=>$value) {
                $product_variatons = new WC_Product_Variation($value);
                if ( $product_variatons->exists() && $product_variatons->variation_is_visible() ) {
                $variations[$value] = $product_variatons->get_variation_attributes();
                    }
    

相关问题