我正在尝试在产品属性标签中添加一个自定义字段。在我们的情况下,我们已将其重命名为“规范”,不确定是否重要。
这是创建自定义字段的代码,我可以成功地对其进行操作并将其放置在产品页面上,而不是在product_attributes标签中。
product_attributes标签始终存在于我们网站上的每个产品及其构建方式中。
代码:
// Test in Additional Info
// -----------------------------------------
// 1. Add custom field input @ Product Data > Variations > Single Variation
add_action( 'woocommerce_variation_options_pricing', 'bbloomer_add_test_cf_to_variations', 10, 3 );
function bbloomer_add_test_cf_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
'id' => 'test_cf[' . $loop . ']',
'class' => 'short',
'label' => __( 'Test', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'test_cf', true )
)
);
}
// -----------------------------------------
// 2. Save custom field on product variation save
add_action( 'woocommerce_save_product_variation', 'bbloomer_save_test_cf_variations', 10, 2 );
function bbloomer_save_test_cf_variations( $variation_id, $i ) {
$test_cf = $_POST['test_cf'][$i];
if ( ! empty( $test_cf ) ) {
update_post_meta( $variation_id, 'test_cf', esc_attr( $test_cf ) );
} else delete_post_meta( $variation_id, 'test_cf' );
}
// 3. Store custom delivery value into variation data
add_filter( 'woocommerce_available_variation', 'bbloomer_add_test_cf_variation_data' );
function bbloomer_add_test_cf_variation_data( $variations ) {
$test = get_post_meta( $variations[ 'variation_id' ], 'test_cf', true );
if( ! empty( $test ) ) {
$variations['test_cf'] = '<div>Test: <span>' . get_post_meta( $variations[ 'variation_id' ], 'test_cf', true ) . '</span></div>';
}
return $variations;
}
通常,以这种方式添加custom_fields时,我会将其添加到variant.php覆盖文件中,然后可以使用css对其进行操作。
我尝试将以下内容添加到product-attributes.php:
<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-test">{{{ data.variation.test_cf }}}</div>
</script>
<script type="text/template" id="tmpl-unavailable-variation-template">
<p><?php _e( 'Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce' ); ?></p>
</script>
但是我根本无法显示它们。
答案 0 :(得分:1)
我不知道在那里应该确切显示什么,看起来您想共享一些有关交货时间的信息吗?
STEP 1
在主题functions.php中创建一个函数。我已经发表了一些评论,以解释发生了什么事情。
/**
* Template hook points at product_attributes.php
*
* @template product-attributes.php
*/
add_action('custom_template_hook', 'display_custom_field' , 10 , 1);
function display_custom_field($product) {
// Check if product is variable
if( $product->is_type( 'variable' ) ){
// If so, get all available variations!
$variations = $product->get_available_variations();
// Loop through variations
foreach ($variations as $variation) {
// Check if meta field isn't empty, if not display the field!
if (!empty(get_post_meta( $variation['variation_id'], 'test_cf', true))) {
// get product object from variation ID
$product_obj = wc_get_product( $variation['variation_id'] );
?>
<tr class="woocommerce-product-attributes-item">
<th class="woocommerce-product-attributes-item__label"><?php echo $product_obj->get_name() ?></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( get_post_meta( $variation['variation_id'], 'test_cf', true ) ); ?></td>
</tr>
<?
}
}
}
}
第2步
在product-attributes中不是可以使用的钩子,因此必须在product-attributes.php中放入一行代码 我个人建议使用儿童主题,否则您将来的更新可能会丢失更改。
do_action('custom_template_hook', $product);
您的product-attributes.php文件应如下所示!
<?php
/**
* Product attributes
*
* Used by list_attributes() in the products class.
*
* This template can be overridden by copying it to yourtheme/woocommerce/single-product/product-attributes.php.
*
* HOWEVER, on occasion WooCommerce will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @package WooCommerce/Templates
* @version 3.6.0
*/
defined( 'ABSPATH' ) || exit;
if ( ! $product_attributes ) {
return;
}
?>
<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;
do_action('custom_template_hook', $product);
?>
</table>
希望这会有所帮助,如果您遇到任何问题,请告诉我!