我正在尝试为每个产品变体设置添加“内置尺寸”字段。
我已经按照以下提示进行了操作,但他们并没有按照我的意愿行事:
这些是将其添加到其他数据选项卡之一。我需要每个变化。每个变体都有一个内置维度和一个出货维度。
答案 0 :(得分:1)
使用下面的2个钩子函数,你将得到你在模拟中的期望:
// Add variation custom "dimentions" fields
add_action( 'woocommerce_variation_options_dimensions','add_variation_options_built_dimensions', 10, 3 );
function add_variation_options_built_dimensions( $loop, $variation_data, $variation ){
$variation_built_lenght = get_post_meta($variation->ID,"_built_lenght", true );
if( ! $variation_built_lenght ) $variation_built_lenght = "";
$variation_built_width = get_post_meta($variation->ID,"_built_width", true );
if( ! $variation_built_width ) $variation_built_width = "";
$variation_built_height = get_post_meta($variation->ID,"_built_height", true );
if( ! $variation_built_height ) $variation_built_height = "";
?>
<p class="form-field form-row dimensions_field built_dimensions hide_if_variation_virtual form-row-last">
<label for="product_built_length"><?php
// translators: %s: dimension unit
printf(
__( 'Built dimensions (L×W×H) (%s)', 'woocommerce' ),
get_option( 'woocommerce_dimension_unit' )
);
?></label>
<?php echo wc_help_tip( __( 'Built length x width x height in decimal form', 'woocommerce' ) ); ?>
<span class="wrap">
<input id="product_built_length" placeholder="<?php esc_attr_e( 'Built length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_lenght_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_lenght ); ?>" />
<input placeholder="<?php esc_attr_e( 'Built width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_width_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_width ); ?>" />
<input placeholder="<?php esc_attr_e( 'Built height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="built_height_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_height ); ?>" />
</span>
</p>
<?php
}
//Save variation custom "dimentions" fields
add_action( 'woocommerce_save_product_variation','save_variation_options_built_dimensions', 10 ,2 );
function save_variation_options_built_dimensions( $variation_id, $loop ){
$built_lenght = $_POST["built_lenght_$loop"];
if(!empty($built_lenght))
update_post_meta( $variation_id, '_built_lenght', sanitize_text_field($built_lenght) );
$built_width = $_POST["built_width_$loop"];
if(!empty($built_width))
update_post_meta( $variation_id, '_built_width', sanitize_text_field($built_width) );
$built_height = $_POST["built_height_$loop"];
if(!empty($built_height))
update_post_meta( $variation_id, '_built_height', sanitize_text_field($built_height) );
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
此代码经过测试,适用于WooCommerce 2.6.x和3+。
你会得到这个: