WooCommerce – 更改单个产品页面上属性的位置

huangapple go评论60阅读模式
英文:

WooCommerce - change the placement of Attribute on single product page

问题

我正在尝试将一个属性("难度")添加到产品页面的主要列表中。虽然我成功添加了它,但我希望它在"类别"和"标签"之上,而不是在"添加到购物车"按钮下面。请参见截图。

我使用了以下代码(在线找到的),并将其添加到我的functions.php文件中:

function njengah_woo_attribute(){
 
    global $product;
    $attributes = $product->get_attributes();
 
    if ( ! $attributes ) {
        return;
    }
 
    $display_result = '';
 
    foreach ( $attributes as $attribute ) {
        if ( $attribute->get_variation() ) {
            continue;
        }
 
        $name = $attribute->get_name();
        if ( $attribute->is_taxonomy() ) {
 
            $terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
            $njengahtax = $terms[0]->taxonomy;
            $njengah_object_taxonomy = get_taxonomy($njengahtax);
            if ( isset ($njengah_object_taxonomy->labels->singular_name) ) {
 
                $tax_label = $njengah_object_taxonomy->labels->singular_name;
 
            } elseif ( isset( $njengah_object_taxonomy->label ) ) {
 
                $tax_label = $njengah_object_taxonomy->label;
                if ( 0 === strpos( $tax_label, 'Product ' ) ) {
                    $tax_label = substr( $tax_label, 8 );
                }
            }
 
            $display_result .= $tax_label . ': ';
            $tax_terms = array();
 
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term->name );
                array_push( $tax_terms, $single_term );
            }
 
            $display_result .= implode(', ', $tax_terms) .  '<br />';
 
        } else {
 
            $display_result .= $name . ': ';
            $display_result .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
        }
    }
 
    echo $display_result;
}
add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 25);
英文:

I'm trying to add an Attribute ("Difficulty") to the main listing of a product page. While I was successful in adding it, I'd like it to be higher and included with the "Category" and "Tags" instead of under the "Add to Cart" button. See screenshot.

WooCommerce – 更改单个产品页面上属性的位置

I used the following code (found online), and added it to my functions.php:

function njengah_woo_attribute(){
 
    global $product;
    $attributes = $product-&gt;get_attributes();
 
    if ( ! $attributes ) {
        return;
    }
 
    $display_result = &#39;&#39;;
 
    foreach ( $attributes as $attribute ) {
        if ( $attribute-&gt;get_variation() ) {
            continue;
        }
 
        $name = $attribute-&gt;get_name();
        if ( $attribute-&gt;is_taxonomy() ) {
 
            $terms = wp_get_post_terms( $product-&gt;get_id(), $name, &#39;all&#39; );
            $njengahtax = $terms[0]-&gt;taxonomy;
            $njengah_object_taxonomy = get_taxonomy($njengahtax);
            if ( isset ($njengah_object_taxonomy-&gt;labels-&gt;singular_name) ) {
 
                $tax_label = $njengah_object_taxonomy-&gt;labels-&gt;singular_name;
 
            } elseif ( isset( $njengah_object_taxonomy-&gt;label ) ) {
 
                $tax_label = $njengah_object_taxonomy-&gt;label;
                if ( 0 === strpos( $tax_label, &#39;Product &#39; ) ) {
                    $tax_label = substr( $tax_label, 8 );
                }
            }
 
            $display_result .= $tax_label . &#39;: &#39;;
            $tax_terms = array();
 
            foreach ( $terms as $term ) {
                $single_term = esc_html( $term-&gt;name );
                array_push( $tax_terms, $single_term );
            }
 
            $display_result .= implode(&#39;, &#39;, $tax_terms) .  &#39;&lt;br /&gt;&#39;;
 
        } else {
 
            $display_result .= $name . &#39;: &#39;;
            $display_result .= esc_html( implode( &#39;, &#39;, $attribute-&gt;get_options() ) ) . &#39;&lt;br /&gt;&#39;;
        }
    }
 
    echo $display_result;
}
add_action(&#39;woocommerce_single_product_summary&#39;, &#39;njengah_woo_attribute&#39;, 25);

答案1

得分: 1

不需要太久了,所以可能不再需要,但以防万一或者如果其他人想要做同样的事情。你可以这样做:

将:

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

改为:

add_shortcode('difficultyAttribute', 'njengah_woo_attribute');

然后,你可以在文本正文中添加短代码[difficultyAttribute],放在你想要的任何位置。

英文:

It's been a while so probably not needed anymore, but just in case or if someone else wants to do the same. You can do this:

Change:

add_action(&#39;woocommerce_single_product_summary&#39;, &#39;njengah_woo_attribute&#39;, 25);

to

add_shortcode( &#39;difficultyAttribute&#39;, &#39;njengah_woo_attribute&#39;);

Then you can add the shortcode [difficultyAttribute] in the body text wherever you want to place it

答案2

得分: 0

将代码中的部分翻译如下:

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

更改为

add_action('woocommerce_single_product_summary', 'njengah_woo_attribute', 10);

末尾的数字越小,项目显示得越高。

英文:

Change

add_action(&#39;woocommerce_single_product_summary&#39;, &#39;njengah_woo_attribute&#39;, 25);

to

add_action(&#39;woocommerce_single_product_summary&#39;, &#39;njengah_woo_attribute&#39;, 10);

The smaller the number at the end, the higher the item will be displayed.

huangapple
  • 本文由 发表于 2023年2月24日 02:05:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75548670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定