帮助,我使用了woo-commerce,我能够显示商店/存档的变化。我的客户想要的是更改后端没有的每个变体的标题/名称。它唯一的sku,图像等没有标题。如何为标题添加字段?
答案 0 :(得分:1)
您可以通过在woocommerce_product_after_variable_attributes
操作
add_action( 'woocommerce_product_after_variable_attributes', function( $loop, $variation_data, $variation ) { ?>
<div>
<p class="form-row form-row-full">
<label>Variation Title</label>
<input type="text" name="variable_post_title[<?php echo $loop; ?>]" value="<?php echo esc_attr( $variation->post_title ); ?>" placeholder="Variation title">
</p>
</div><?php
}, 10, 3 );
然后将帖子标题保存在woocommerce_save_product_variation
add_action( 'woocommerce_save_product_variation', function() {
if ( 'variable' !== $_POST['product-type'] ) {
return;
}
if ( ! isset( $_POST['variable_post_title'][ $i ] ) ) {
return;
}
$variation_post_title = wc_clean( $_POST['variable_post_title'][ $i ] );
wp_update_post( [
'ID' => $variation_id,
'post_title' => $variation_post_title,
] );
clean_post_cache( $variation_id );
}, 10, 2 );