【发布时间】:2018-06-06 04:16:53
【问题描述】:
我有简单和可变的产品。我还有一个插件,用于在商店中单独显示每个变体,而不仅仅是它的父代。我设置了 4 个属性,每个属性都有几个可能的值,所有产品都可以共享这些属性的全部或部分。未来可能会有更多属性,它们可能适用于所有产品或仅适用于部分产品。
我需要在每个变体(以及简单产品)和变体父项下仅显示相应产品具有的标签和值属性,而不是可能适用于该属性的所有可能值。
最后,必须考虑到该网站使用 WPML 的语言不止一种。
为此,我使用了以下代码,它可以工作,但这意味着对每个属性重复相同的代码(你可以看到我重复了相同的 4 次)。肯定有一种方法可以合并一个 foreach 循环来完成这项工作。我用在 StackExchange 中看到的相同示例多次尝试过此操作,但无济于事。
add_action( 'woocommerce_after_shop_loop_item_title', 'add_attributes_terms', 1);
function add_attributes_terms() {
$tax_bar = 'pa_bar-colour';
$tax_but = 'pa_button-colour';
$tax_caps = 'pa_caps-colour';
$tax_cas = 'pa_lock-cases-colour';
$att_bar = get_the_terms( $product->id, $tax_bar);
$att_but = get_the_terms( $product->id, $tax_but);
$att_caps = get_the_terms( $product->id, $tax_caps);
$att_cas = get_the_terms( $product->id, $tax_cas);
?><div style="font-size: 11px; overflow: overlay; padding: 0px 5px 15px 5px; line-height: 13px;"><?php
if (! empty($att_bar)) {
?><div style="overflow: overlay;"><?php
echo '<span style=" float: left; color: #444;">' . str_replace(" colour", "", get_taxonomy($tax_bar)->labels->singular_name) . ': </span>';
?><div style="overflow: overlay; color: #959595;"><?php
if ( $att_bar && ! is_wp_error( $$att_bar )): foreach ( $att_bar as $attributes_name ){
echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
}
endif;
?></div><?php
?></div><?php
}
if (! empty($att_but)) {
?><div class="b2" style="overflow: overlay;"><?php
echo '<span style=" float: left; color: #444;">' . str_replace(" colour","", get_taxonomy($tax_but)->labels->singular_name) . ': </span>';
?><div style="overflow: overlay; color: #959595;"><?php
if ( $att_but && ! is_wp_error( $$att_but )): foreach ( $att_but as $attributes_name ){
echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
}
endif;
?></div><?php
?></div><?php
}
if (! empty($att_caps)) {
?><div style="overflow: overlay;"><?php
echo '<span style=" float: left; color: #444;">' . str_replace(" colour","", get_taxonomy($tax_caps)->labels->singular_name) . ': </span>';
?><div style="overflow: overlay; color: #959595;"><?php
if ( $att_caps && ! is_wp_error( $$att_caps )): foreach ( $att_caps as $attributes_name ){
echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
}
endif;
?></div><?php
?></div><?php
}
if (! empty($att_cas)) {
?><div style="overflow: overlay;"><?php
echo '<span style=" float: left; color: #444;">' . str_replace(" colour","", get_taxonomy($tax_cas)->labels->singular_name) . ': </span>';
?><div style="overflow: overlay; color: #959595;"><?php
if ( $att_cas && ! is_wp_error( $$att_cas )): foreach ( $att_cas as $attributes_name ){
echo '<span>' . ' ' . $attributes_name->name . ' ' . '</span>';
}
endif;
?></div><?php
?></div><?php
}
?></div><?php
}
如何压缩此代码以避免 html 块重复?
如何获取所有产品类型的所有属性,而仅获取产品变体的选定属性值?
在我收到所有输入之后,我的最终代码已经可以正常工作了:
add_action( 'woocommerce_after_shop_loop_item_title', 'add_attributes_terms', 1);
function add_attributes_terms() {
global $product;
?><div style="font-size: 10px; overflow: overlay; padding: 6px 10px 6px 10px; line-height: 13px; box-shadow: inset 0 0 5px rgba(0,0,0,0.1), inset 0 3px 2px rgba(0,0,0,0.1);"><?php
// Loop through each defined attribute
foreach( $product->get_attributes() as $taxonomy => $values ){
$terms = $product->get_attribute( $taxonomy );
if(!empty($terms)){
?><div style="overflow: overlay;"><?php
echo '<span id="shop_labels" style=" float: left; color: #444;">' . str_replace([" colour", "Cor da ", "Cor do ", "Cor das ", " de parede"], ["","","","",""], wc_attribute_label($taxonomy)) . ': </span>';
echo '<div style="color: #959595;">' . $terms . '</div>';
?></div><?php
}
}
?></div><?php
}
【问题讨论】:
标签: php html woocommerce attributes variations