我有一个产品具有以下尺寸参数:
>长度(Woocomerce标准)
>宽度(Woocomerce标准)
>高度(Woocomerce标准)
>直径
>厚度
>电路
在产品编辑页面中,我只有长度,宽度,高度(Woocomerce标准)
我想添加其他尺寸参数
如何正确添加这些额外的尺寸?
这有过滤器吗?
解决方法:
有多种方式……
1)使用产品属性:(无需任何编码需求):
>您应该创建3个新的产品属性(一个用于彼此缺少的维度).
>您应该使用正确的选项(在产品选项上显示)和值设置每个产品中的每个产品.
优点:其他维度属性将显示在产品页面上.
缺点:
>每个产品都需要设置每个属性,每个值都将成为此产品属性的术语.
>没有特定的WC_Product方法来获取默认维度属性的自定义产品属性.因此,将它们放在产品页面之外会更复杂.
2)使用自定义设置字段:
// Add custom fields to product shipping tab
add_action( 'woocommerce_product_options_dimensions', 'add_product_options_other_dimensions');
function add_product_options_other_dimensions(){
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' => '_diameter',
'label' => __( 'Diameter', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Diameter description help.', 'woocommerce' )
) );
woocommerce_wp_text_input( array(
'id' => '_thickness',
'label' => __( 'Thickness', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Thickness description help.', 'woocommerce' )
) );
woocommerce_wp_text_input( array(
'id' => '_circuit',
'label' => __( 'Circuit', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Circuit description help.', 'woocommerce' )
) );
}
// Save the custom fields values as meta data
add_action( 'woocommerce_process_product_meta', 'save_product_options_other_dimensions' );
function save_product_options_other_dimensions( $post_id ){
if( isset( $_POST['_diameter'] ) )
update_post_meta( $post_id, '_diameter', esc_attr( $_POST['_diameter'] ) );
if( isset( $_POST['_thickness'] ) )
update_post_meta( $post_id, '_thickness', esc_attr( $_POST['_thickness'] ) );
if( isset( $_POST['_circuit'] ) )
update_post_meta( $post_id, '_circuit', esc_attr( $_POST['_circuit'] ) );
}
// Add custom fields to product variation settings
add_action( 'woocommerce_product_after_variable_attributes','add_variation_options_other_dimensions', 10, 3 );
function add_variation_options_other_dimensions( $loop, $variation_data, $variation ){
$variation_diameter = get_post_meta($variation->ID,"_diameter", true );
if( ! $variation_diameter ) $variation_diameter = "";
$variation_thickness = get_post_meta($variation->ID,"_thickness", true );
if( ! $variation_thickness ) $variation_thickness = "";
$variation_circuit = get_post_meta($variation->ID,"_circuit", true );
if( ! $variation_circuit ) $variation_circuit = "";
echo '<p class="form-field dimensions_field">';
woocommerce_wp_text_input( array(
'id' => '_diameter' . '_' . $loop,
'label' => __( 'Diameter', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Diameter description help.', 'woocommerce' ),
'value' => $variation_diameter
) );
woocommerce_wp_text_input( array(
'id' => '_thickness' . '_' . $loop,
'label' => __( 'Thickness', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Thickness description help.', 'woocommerce' ),
'value' => $variation_thickness
) );
woocommerce_wp_text_input( array(
'id' => '_circuit' . '_' . $loop,
'label' => __( 'Circuit', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Circuit description help.', 'woocommerce' ),
'value' => $variation_circuit
) );
echo '</p>';
}
// Save product variation custom fields values
add_action( 'woocommerce_save_product_variation','save_variation_options_other_dimensions', 10 ,2 );
function save_variation_options_other_dimensions( $variation_id, $loop ){
$built_lenght = $_POST["_diameter_$loop"];
if( isset($built_lenght) )
update_post_meta( $variation_id, '_built_lenght', esc_attr($built_lenght) );
$built_width = $_POST["_thickness_$loop"];
if( isset($built_width) )
update_post_meta( $variation_id, '_built_width', esc_attr($built_width) );
$built_height = $_POST["_circuit_$loop"];
if( isset($built_height) )
update_post_meta( $variation_id, '_built_height', esc_attr($built_height) );
}
此代码位于活动子主题(或主题)的function.php文件中.
您将获得产品:
对于产品变化(在可变产品中“变化设置”):
现在要显示并获取这些值,您必须按照本文档中的说明进行override the woocommerce template via your theme.
要通过主题复制和覆盖的文件是:single-product / product-attributes.php模板.
您必须向其添加一些代码,以便在显示的默认维度之后显示自定义的其他维度标签和值.
要输出正确的值,您将使用get_post_meta()函数.
例如,要显示您将使用的直径值:
<?php echo get_post_meta( $product->get_id(), '_diameter', true ); ?>