在产品为空时添加产品维度 - Add/edit 产品 - Woocommerce
Add product dimension while they are empty - Add/edit product - Woocommerce
运费是根据我的 Woocommerce 网站上的产品重量和尺寸计算的。但是,我的库存系统仅使用产品重量计算运费。因此,从库存系统同步到网站的新产品一般没有尺寸。
经过几次测试,我发现为网站上的产品添加长度、宽度和高度 0.001 会生成最准确的运费。
我需要编写一个函数,将产品尺寸设置为 .001(如果它们在创建时为空)。
这是我尝试过的:
function add_default_dimension_if_empty( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == '_edit_lock' ) {
if ( get_post_type( $post_id ) == 'product' ) {
//get product
$product = wc_get_product( $post_id );
$newDim = .001;
if($product->has_dimensions()){
if(empty( $product->get_length() )){
update_post_meta($post_id, '_length', $newDim);
}
if(empty( $product->get_width() )){
update_post_meta($post_id, '_width', $newDim);
}
if(empty( $product->get_height() )){
update_post_meta($post_id, '_height', $newDim);
}
}
}
}
}
add_action( 'added_post_meta', 'add_default_dimension_if_empty', 10, 4 );
add_action( 'updated_post_meta', 'add_default_dimension_if_empty', 10, 4 );
我使用了 this 文章中的结构,但它似乎不起作用。有什么想法吗?
您的代码没问题,只需要更改一个检查维度是否存在的条件。
将 if($product->has_dimensions())
行替换为以下
$dimensions = $product->get_dimensions();
if(! empty( $dimensions ))
这对你有用。
运费是根据我的 Woocommerce 网站上的产品重量和尺寸计算的。但是,我的库存系统仅使用产品重量计算运费。因此,从库存系统同步到网站的新产品一般没有尺寸。
经过几次测试,我发现为网站上的产品添加长度、宽度和高度 0.001 会生成最准确的运费。
我需要编写一个函数,将产品尺寸设置为 .001(如果它们在创建时为空)。
这是我尝试过的:
function add_default_dimension_if_empty( $meta_id, $post_id, $meta_key, $meta_value ) {
if ( $meta_key == '_edit_lock' ) {
if ( get_post_type( $post_id ) == 'product' ) {
//get product
$product = wc_get_product( $post_id );
$newDim = .001;
if($product->has_dimensions()){
if(empty( $product->get_length() )){
update_post_meta($post_id, '_length', $newDim);
}
if(empty( $product->get_width() )){
update_post_meta($post_id, '_width', $newDim);
}
if(empty( $product->get_height() )){
update_post_meta($post_id, '_height', $newDim);
}
}
}
}
}
add_action( 'added_post_meta', 'add_default_dimension_if_empty', 10, 4 );
add_action( 'updated_post_meta', 'add_default_dimension_if_empty', 10, 4 );
我使用了 this 文章中的结构,但它似乎不起作用。有什么想法吗?
您的代码没问题,只需要更改一个检查维度是否存在的条件。
将 if($product->has_dimensions())
行替换为以下
$dimensions = $product->get_dimensions();
if(! empty( $dimensions ))
这对你有用。