当变体发生变化时,更新可变产品的后期元数据

问题描述

我为我的变体添加了一个自定义元字段。这是一个文本输入字段。这有效并且它存在于db中。 I am trying to make this information behave as the weight parameter,that is updated in the additional info tab,when an other variation is selected:

//diameter
add_action( 'woocommerce_product_after_variable_attributes','variation_settings_fields',10,3 );

add_action( 'woocommerce_save_product_variation','save_variation_settings_fields',2 );

function variation_settings_fields( $loop,$variation_data,$variation ) {
// Text Field
echo '<p class="form-field dimensions_field">';
woocommerce_wp_text_input(
    array(
        'id'          => '_durchmesser[' . $variation->ID . ']','label'       => __( 'Durchmesser','woocommerce' ),'placeholder' => '','desc_tip'    => 'true','description' => __( 'Durchmesser einfügen.','value'       => get_post_meta( $variation->ID,'_durchmesser',true )
    )
);
echo '</p>';
}

function save_variation_settings_fields( $post_id ) {
$text_field = $_POST['_durchmesser'][ $post_id ];
if ( ! empty( $text_field ) ) {
    update_post_meta( $post_id,esc_attr( $text_field ) );
}
}

然后我尝试将此值用于附加信息选项卡:

add_filter('woocommerce_display_product_attributes','display_diameter',2);

function display_diameter($product_attributes,$product){

//this gets all variation id’s
$test = $product->get_children();
//echo '<pre>';
//print_r($test);
//echo '</pre>';

//hardcoded since i do not know how to dynamically add the current variation id
$product_attributes['customfield'] = [
    'label' => __('Durchmesser'),'value' => get_post_meta( $test[2],true ).' cm'
];

return $product_attributes;

}

变体 ID 是硬编码的 $test[2]。如何使当前的变体 ID 发挥作用? 这是通过ajax完成的吗?

我用模板变体.php 进行了另一个测试,这有效,但我不想让屏幕负担过重,而是在附加信息选项卡中显示特定信息。

add_filter( 'woocommerce_available_variation','test_diameter' );
function test_diameter( $variations ) {
$variations['durchmesser'] = get_post_meta( $variations[ 'variation_id' ],true );
return $variations;
}

和variation.php

<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-availability">Durchmesser: {{{data.variation.durchmesser}}} cm</div>
</script>

谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...