提问人:Mauro Beretta 提问时间:8/15/2023 最后编辑:Mauro Beretta 更新时间:8/16/2023 访问量:126
在 Woocommerce 中为空时隐藏显示的变体 cutom 字段数据
Hide displayed variation cutom fields data when empty in Woocommerce
问:
我将自定义字段添加到产品变体中(感谢这篇文章)
我在wp-content\themes\my_theme\woocommerce\single-product\add-to-cart\product-attributes.php模板中添加了以下代码:
<?php
/**
* Product attributes
* @version 3.6.0
*/
defined( 'ABSPATH' ) || exit;
if ( ! $product_attributes ) {
return;
}
?>
<ul id="new_product_attributes" class="accordion" data-accordion data-allow-all-closed="true">
<li class="accordion-item" data-accordion-item>
<a href="#" class="accordion-title">Caratteristiche tecniche</a>
<div class="accordion-content" data-tab-content>
<div class="attributes_container">
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div class="single_variation_wrap">
<div id="variation_clone_box" class="woocommerce-variation single_variation">
<script type="text/template" id="tmpl-variation-template">
<table class="woocommerce-product-attributes shop_attributes">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--my_field">
<th class="woocommerce-product-attributes-item__label">My Field Label</th>
<td id="my_field" class="woocommerce-product-attributes-item__value">{{{ data.variation.my_field }}}</td>
</tr>
</table>
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
<div id="old_box_Price" class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
</script>
</div>
</div>
<div class="datasheets_box">
<a class="button">(i) Scarica la scheda tecnica</a>
</div>
<div class="richiedi_info_box">
<p class="richiedi_info_title">Richiedi informazioni</p>
<p><strong>Assistenza Clienti</strong></p>
<p>
ORARI<br>
Dal Lunedì al Venerdì<br>
8:00 / 13:00 - 14:00 / 17:00
<p>
<a class="button">(i) [email protected]</a>
<a class="button">(i) +39 0131 718477</a>
</p>
</div>
</div><!-- accordion-content -->
</li>
</ul><!-- #new_product_attributes -->
<div id="new_box_AddToCart">
<?php
woocommerce_quantity_input(
array(
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $product->get_min_purchase_quantity(), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->get_max_purchase_quantity(), $product ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( wp_unslash( $_POST['quantity'] ) ) : $product->get_min_purchase_quantity(), // WPCS: CSRF ok, input var ok.
)
);
?>
<div class="shipment_info">
<p>Disponibilità immediata</p>
<p>I costi di spedizione sono esclusi</p>
</div>
<button type="submit" class="single_add_to_cart_button button alt<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>"><?php echo esc_html( $product->single_add_to_cart_text() ); ?></button>
</div>
这很有效!
在上面的代码中,我添加了许多行。每行对应一个新的自定义字段。 为简化起见,在这篇文章中,我只粘贴了一行,只有一个自定义字段。 下面的代码是表格中要隐藏的部分......如果自定义字段为空。
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--my_field">
<th class="woocommerce-product-attributes-item__label">My Field Label</th>
<td id="my_field" class="woocommerce-product-attributes-item__value">
{{{ data.variation.my_field }}}
</td>
</tr>
如果 {{{ data.variation.my_field }}} 为空,则 Woocommerce 将显示该行。我在产品变体中有许多自定义字段(见附件),因此我尝试仅显示带有值的行。
如果 {{{ data.variation.my_field }}} 为空,我想隐藏该行。
我试过了这个,但它不起作用:
{{{ #if data.variation.my_field }}}
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--my_field">
<th class="woocommerce-product-attributes-item__label">My Field</th>
<td id="your_field" class="woocommerce-product-attributes-item__value">
{{{ data.variation.my_field }}}
</td>
</tr>
{{{/if }}}
如何向产品变体添加自定义字段(这部分有效)
现在,我向您编写用于向产品变体添加自定义字段的所有代码。我不知道它是否可以帮助您解决问题;)
为了添加变体的自定义字段,我在 function.php 中编写了这个。
我为变体创建了新的自定义字段(此处称为“我的字段”):
function my_field_settings( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => "my_field{$loop}",
'label' => __( 'My Field', 'woocommerce' ),
'name' => "my_field[{$loop}]",
'value' => get_post_meta( $variation->ID, 'my_field', true ),
'wrapper_class' => 'form-row form-row-33',
)
);
}
add_action( 'woocommerce_product_after_variable_attributes', 'my_field_settings', 10, 3 );
之后,我保存了新的自定义字段:
function my_field_save( $variation_id, $loop ) {
$text_field = $_POST['my_field'][ $loop ];
update_post_meta( $variation_id, 'my_field', esc_attr( $text_field ));
}
add_action( 'woocommerce_save_product_variation', 'my_field_save', 10, 2 );
最后,我添加新的自定义字段:
function my_field_load( $variation ) {
$variation['my_field'] = get_post_meta( $variation[ 'variation_id' ], 'my_field', true );
return $variation;
}
add_filter( 'woocommerce_available_variation', 'my_field_load' );
答:
我已经使用 WooCommerce CRUD 方法和最新的 WooCommerce 钩子重新审视了您的所有代码。
我添加了一些jQuery代码,以允许显示或隐藏“Diametro nominale”显示。
我已经缩短了模板上的代码......
下面是该代码:
function add_admin_variation_custom_fields( $loop, $variation_data, $variation ) {
$variation_object = wc_get_product($variation->ID);
woocommerce_wp_text_input( array(
'id' => "diametro_nominale_{$loop}",
'label' => __( 'Diametro nominale', 'woocommerce' ),
'value' => $variation_object->get_meta('diametro_nominale'),
'wrapper_class' => 'form-row form-row-33',
) );
}
add_action( 'woocommerce_product_after_variable_attributes', 'add_admin_variation_custom_fields', 10, 3 );
function save_admin_variation_custom_fields( $variation, $i ) {
if ( isset($_POST['diametro_nominale_'.$i]) ) {
$variation->update_meta_data('diametro_nominale', sanitize_text_field($_POST['diametro_nominale_'.$i]));
}
}
add_action( 'woocommerce_admin_process_variation_object', 'save_admin_variation_custom_fields', 10, 2 );
function display_variation_custom_field_values( $variation_data, $product, $variation ) {
$diametro_nominale = $variation->get_meta('diametro_nominale');
$variation_data['diametro_nominale'] = empty($diametro_nominale) ? 0 : $diametro_nominale;
return $variation_data;
}
add_filter( 'woocommerce_available_variation', 'display_variation_custom_field_values', 10, 3 );
function variation_custom_fields_js() {
?>
<script>
jQuery(function($){
// After DOM (on load)
$('.shop_attributes .diametro-nominale').hide();
// On Selected variation or Unselected variation
$('form.cart').on('show_variation', function(event, data) {
// On variation "show"
if ( data.diametro_nominale == '0' ) {
// When "diametro_nominale" is empty
$('.shop_attributes .diametro-nominale').hide();
} else {
// When "diametro_nominale" has data
$('.shop_attributes .diametro-nominale').show();
}
}).on('hide_variation', function(){
// On variations "Hide"
$('.shop_attributes .diametro-nominale').hide();
});
});
</script>
<?php
}
add_action( 'woocommerce_after_variations_form', 'variation_custom_fields_js' );
代码进入子主题的 functions.php 文件(或插件)。
以及模板文件的缩短代码:single-product/product-attributes.php
?>
<table class="woocommerce-product-attributes shop_attributes">
<?php foreach ( $product_attributes as $product_attribute_key => $product_attribute ) : ?>
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--<?php echo esc_attr( $product_attribute_key ); ?>">
<th class="woocommerce-product-attributes-item__label"><?php echo wp_kses_post( $product_attribute['label'] ); ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( $product_attribute['value'] ); ?></td>
</tr>
<?php endforeach; ?>
</table>
<div class="single_variation_wrap">
<div id="variation_clone_box" class="woocommerce-variation single_variation">
<script type="text/template" id="tmpl-variation-template">
<table class="woocommerce-product-attributes shop_attributes">
<tr class="woocommerce-product-attributes-item woocommerce-product-attributes-item--diametro_nominale diametro-nominale">
<th class="woocommerce-product-attributes-item__label"><?php _e( 'Diametro nominale', 'woocommerce' ); ?></th>
<td class="woocommerce-product-attributes-item__value">{{{ data.variation.diametro_nominale }}}</td>
</tr>
</table>
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
<div id="old_box_Price" class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
</script>
</div>
</div>
经过测试并有效。
评论