问题描述
我的产品具有以下尺寸参数:
I have a products with the following size parameters:
- 长度(Woocomerce标准)
- 宽度(Woocomerce标准)
- 高度(Woocomerce标准)
- 直径
- 厚度
- 电路
- Length (Woocomerce standard)
- Width (Woocomerce standard)
- Height (Woocomerce standard)
- Diameter
- Thickness
- Circuit
在产品编辑页面中,我只有长度,宽度,高度(Woocomerce标准)
In the product edit page I have only Length, Width, Height (Woocomerce standard)
我想添加我的其他尺寸参数
如何正确添加这些额外的尺寸?
How can I add this extra sizes properly?
对此有任何过滤器吗?
推荐答案
有多种方法……
1)使用产品属性:(无需任何编码):
- 您应该创建3个新产品属性(每个缺少的维度一个).
- 您应在每个产品中为每个产品设置正确的选项(在产品选项上显示)和值.
- You should create 3 new product attributes (one for each other missing dimension).
- You should set each of them in each product with the correct options (displayed on products option) and values.
优势:那些其他尺寸属性将显示在产品页面上.
Advantage: Those other dimention attributes will be displayed on product pages.
缺点:
- 需要为每个产品设置每个属性,每个值都将成为该产品属性的一个术语.
- 没有特定的现有
WC_Product
方法来获取那些自定义产品属性,例如默认尺寸属性.将它们带出产品页面会更加复杂.
- Each attribute Needs to be set for each product and each value is going to be a term of this product attribute.
- There are no specific existing
WC_Product
methods to get those custom product attributes like the default dimension ones. So is going to be more complicated to get them outside the product page.
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文件中.
This code goes on function.php file of your active child theme (or theme).
对于产品,您将得到它:
You will get that for products:
对于产品版本(在可变产品中,版本设置"):
And that for product variations (in variable products "variations settings):
现在要显示和获取这些值,您将必须通过您的主题覆盖woocommerce模板(如本文档中所述).
Now to display and get those values, you will have to override the woocommerce template via your theme as explained in this documentation.
要通过您的主题复制和覆盖的文件是:single-product/product-attributes.php
模板.
The file to copy and override via your theme is: single-product/product-attributes.php
template.
您将不得不向其中添加一些代码,以在显示的默认尺寸之后显示自定义的其他尺寸标签和值.
You will have to add some code to it to display your custom additional dimension labels and values just after the displayed default dimentions.
要输出正确的值,您将使用get_post_meta()
功能.
To output the correct values you will use get_post_meta()
function.
例如,要显示diameter
值,您将使用:
For example to display the diameter
value you will use:
<?php echo get_post_meta( $product->get_id(), '_diameter', true ); ?>
这篇关于Woocommerce中产品的其他自定义维度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!