Юри*_*ван 7 php wordpress attributes product woocommerce
更新WooCommerce后,属性不再显示.
在模板中content-single-product.php如果我使用var_dump($attribute_names);我得到一个对象数组.
在foreach( $attribute_names as $attribute_name )数据范围内受到保护.
以下是此模板的代码:
$attributes = $product->get_attributes();
<?php if($attributes) {echo "<p class='product-desc-title'>?????????</p>";} ?>
<?php foreach ( $attributes as $attribute ) : ?>
<?php
if ( $attribute['is_taxonomy'] ) {
global $post;
$attribute_names = $attribute;
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();
$attrID = $attribute['name'];
$paPMat = 'pa_product-material';
$paPColor = 'pa_product-color';
// ??? ?????????? ????? ????????? ??? ??????? ???????? ????? ?????? ? ????????? ???????? ? ?????? ? ?????????? "pa_"
$pAttributes_array = array(
array(
'label' => '???????? ???????',
'slug' => 'pa_product-material',
),
array(
'label' => '????',
'slug' => 'pa_product-color',
),
array(
'label' => '????????????',
'slug' => 'pa_konfiguraciya',
),
array(
'label' => '???????? ???????',
'slug' => 'pa_material-kuxni',
),
array(
'label' => '?????',
'slug' => 'pa_forma',
),
array(
'label' => '??? ??????',
'slug' => 'pa_tip-dverej',
),
array(
'label' => '???????',
'slug' => 'pa_stvorki',
),
array(
'label' => '???????',
'slug' => 'pa_razmery',
),
);
foreach ($pAttributes_array as $key => $value) {
if ( ! empty( $terms ) && $attrID === $value['slug'] ) {
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 '<p class="pa-string">'. $value['label'] .': '. implode( $terms_array, ', ' ) . '</p>';
}
}
}
}
} else {
$values = array_map( 'trim', explode( '|', $attribute['value'] ) );
echo apply_filters( 'woocommerce_attribute', wpautop( wptexturize( implode( ', ', $values ) ) ), $attribute, $values );
}
?>
<?php endforeach; ?>
Run Code Online (Sandbox Code Playgroud)
更新:压缩代码进行测试:
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_names = $attribute;
// testing output
var_dump($attribute_name);
endforeach;
Run Code Online (Sandbox Code Playgroud)
该var_dump($attribute_name);原始输出为您提供有关它们的对象指征WC_Product_Attribute的对象,这意味着你必须使用可用的方法为此类。
有两种方法:
1)您可以使用以下get_data()方法访问未受保护数组中的属性:
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_data = $attribute->get_data();
// testing pre-formatted output
echo '<pre>'; print_r($attribute_data); echo '</pre>';
// We stop the loop to get the first object only (for testing)
break;
endforeach;
Run Code Online (Sandbox Code Playgroud)
这会给你一个原始输出,如:
Array (
[id] => 1
[name] => pa_color
[options] => Array (
[0] => 8
[1] => 9
)
[position] => 0
[visible] =>
[variation] => 1
[is_visible] => 0
[is_variation] => 1
[is_taxonomy] => 1
[value] =>
)
Run Code Online (Sandbox Code Playgroud)
然后你可以这样使用它:
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_data = $attribute->get_data(); // Get the data in an array
$attribute_name = $attribute_data['name']; // The taxonomy slug name
$attribute_terms = $attribute_data['options']; // The terms Ids
endforeach;
Run Code Online (Sandbox Code Playgroud)
2)您可以使用以下WC_Product_Attribute方法:
$attributes = $product->get_attributes();
foreach ( $attributes as $attribute ):
$attribute_name = $attribute->get_taxonomy(); // The taxonomy slug name
$attribute_terms = $attribute->get_terms(); // The terms
$attribute_slugs = $vaattributeues->get_slugs(); // The term slugs
endforeach;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6272 次 |
| 最近记录: |