【问题标题】:Woocommerce customize single product viewWoocommerce 自定义单个产品视图
【发布时间】:2015-10-08 09:18:57
【问题描述】:

我正在进行 woocommerce 自定义。但是我现在还不够好如何获取产品的属性。我还想获取属性存档页面的链接。

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

此文件用于打印产品描述。之后,我输入代码 sn-p 以打印类别名称及其描述。我还希望它打印产品的属性。 (最好带有指向其存档页面的链接)。

有没有办法使用部分sn-p来呼应产品的属性?

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;

【问题讨论】:

  • 你想要哪些属性?
  • 我的产品变体中有一个名为颜色的属性术语,具有红色、绿色、蓝色等属性...
  • 我给了你一些代码,没有经过测试,但你会从中得到一些方法。

标签: php wordpress woocommerce


【解决方案1】:

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

  <?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();
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多