我添加了此代码:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_shipping', 'add_custom_shipping_option_to_products');
function add_custom_shipping_option_to_products(){
global $product_object;
$product_id = method_exists( $product_object, 'get_id' ) ? $product_object->get_id() : $product_object->id;
echo '</div><div class="options_group">'; // New option group
woocommerce_wp_text_input( array(
'id' => 'custom_text_field1',
'label' => __( 'Weight without Foam', 'woocommerce' ),
'placeholder' => 'something',
'desc_tip' => 'true',
'description' => __( 'Weight in decimal form.', 'woocommerce' ),
'value' => get_post_meta($product_id, '_custom_meta_field1', true),
) );
woocommerce_wp_text_input( array(
'id' => 'custom_text_field2',
'label' => __( 'Volume', 'woocommerce' ),
'placeholder' => 'something',
'desc_tip' => 'true',
'description' => __( 'Volume in decimal form.', 'woocommerce' ),
'value' => get_post_meta($product_id, '_custom_meta_field2', true),
) );
woocommerce_wp_text_input( array(
'id' => 'custom_text_field3',
'label' => __( 'Buoyancy', 'woocommerce' ),
'placeholder' => 'something',
'desc_tip' => 'true',
'description' => __( 'Buoyancy in decimal form.', 'woocommerce' ),
'value' => get_post_meta($product_id, '_custom_meta_field3', true),
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_custom_shipping_option_to_products' );
function save_custom_shipping_option_to_products( $post_id ){
$custom_text_field1 = $_POST['custom_text_field1'];
if( !empty( $custom_text_field1 ) )
update_post_meta( $post_id, '_custom_meta_field1', esc_attr( $custom_text_field1 ) );
$custom_text_field2 = $_POST['custom_text_field2'];
if( !empty( $custom_text_field2 ) )
update_post_meta( $post_id, '_custom_meta_field2', esc_attr( $custom_text_field2 ) );
$custom_text_field3 = $_POST['custom_text_field3'];
if( !empty( $custom_text_field3 ) )
update_post_meta( $post_id, '_custom_meta_field3', esc_attr( $custom_text_field3 ) );
}
// Add variation external "dimentions" fields
add_action( 'woocommerce_product_options_shipping','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 dimensions_field">
<label for="product_built_length"><?php printf( __( 'Ext. dimensions (%s)', 'woocommerce' ), get_option( 'woocommerce_dimension_unit' ) ); ?></label>
<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>
<?php echo wc_help_tip( __( 'LxWxH in decimal form.', 'woocommerce' ) ); ?>
</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) );
} `
到WooCommerce产品页面的后端,我需要这个新添加的字段和参数将由比较功能自动使用。我使用YITH WooCommerce Compare插件来比较产品。现在我只能在那里使用尺寸和重量从bak-end产品发货标签和所有属性,但我需要在那里使用新添加的字段。如果可能,这个新数据应该显示为默认的woocommerce维度参数,该参数已经显示在比较表中。
可以看到该网站HERE。
感谢。