我将自定义域添加到产品变体(thanks to this post)
我在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) assistenza@grafoplast.it</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.varation.my_field}}为空,WooCommerce将显示该行.我在产品变体(see attachment)中有许多定制字段,因此我try 只显示带有值的行.
如果{data.varation.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 }}}
How to Add Custom Fields to Product Variations (this part works)个
现在,我为您编写我用来向产品变体添加自定义字段的所有代码.我不知道它是否能帮助你解决这个问题;)
为了为变体添加自定义字段,我在unction.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' );