在哪里可以修改woocomerce显示产品附加信息的方式?

时间:2020-07-13 18:12:44

标签: wordpress woocommerce

我正在为一个销售二手车的客户的网站工作,他们列出了各种不同的汽车部件的状态。例如:轮胎:破旧,变速箱:还可以。

我刚刚将这些字段添加为属性(在“附加信息”标签中显示的字段) 我们希望以更图形化的方式显示这些值,并根据这些属性使用图像和突出显示的文本,我已经准备好了代码。

在哪里可以修改模板以将其添加到其中?还是可以为我指出实现该目标的正确方向?

1 个答案:

答案 0 :(得分:1)

我的建议是使用ACF | Advanced Custom Fields,它是免费的,具有许多功能并不断更新,并且您也有很多有关该插件的文档。

在您的情况下,我将使用字段select

还取决于您拥有多少字段,但是我相信这是要走的路。 然后,您可以调用您创建的字段并通过PHP输出逻辑。

<p>Tires: <?php the_field('tires'); ?></p>

如果您想自定义代码,那么这些属性最后是可以放入模板文件并对其进行修改的PHP。

<?php $attributes = $product->get_attributes();?>
    <table class="shop_attributes">
        <?php foreach ( $attributes as $attribute ) : ?>
            <tr>
                <th style="text-align: left; padding-right: 20px"><?php echo wc_attribute_label( $attribute->get_name() ); ?></th>
                        <td style="text-align: left;">
                            <?php
                            $values = array();

                            if ( $attribute->is_taxonomy() ) {
                                $attribute_taxonomy = $attribute->get_taxonomy_object();
                                $attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), array( 'fields' => 'all' ) );

                                foreach ( $attribute_values as $attribute_value ) {
                                    $value_name = esc_html( $attribute_value->name );

                                    if ( $attribute_taxonomy->attribute_public ) {
                                        $values[] = '<a href="' . esc_url( get_term_link( $attribute_value->term_id, $attribute->get_name() ) ) . '" rel="tag">' . $value_name . '</a>';
                                    } else {
                                        $values[] = $value_name;
                                    }
                    }
                } else {
                    $values = $attribute->get_options();

                    foreach ( $values as &$value ) {
                        $value = make_clickable( esc_html( $value ) );
                    }
                }

            echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
                            ?>
            </td>
        </tr>
   <?php endforeach; ?>
</table>
相关问题