2018-06-12 18:51
浏览 137

在空的时候添加产品尺寸 - 添加/编辑产品 - Woocommerce

Shipping rates are calculated using product weight and dimension on my Woocommerce website. However, my inventory system only uses product weight for shipping rates. Because of this, new products synchronized from the inventory system to the website generally don't have dimensions.

After a few tests, I found that adding a length, width and height of .001 to products on the site generates the most accurate shipping rates.

I need to write a function that sets product dimensions to .001 if they're empty at the point of creation.

Here is what I've attempted:

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(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 );

I used the structure from this article, but it doesn't seem to work. Any thoughts?

图片转代码服务由CSDN问答提供 功能建议

运费是使用我的Woocommerce网站上的产品重量和尺寸计算的。 但是,我的库存系统仅使用产品重量来计算运费。 因此,从库存系统到网站同步的新产品通常没有尺寸。

经过几次测试后,我发现添加了长度,宽度和高度为.001 对网站上的产品产生最准确的运费。



  function add_default_dimension_if_empty($ meta_id,$ post_id,$ meta_key,$ meta_value){\  n if($ meta_key =='_ initit_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); \  n} 
 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('updated_post_meta','add_default_dimension_  if_empty',10,4); 

我使用了这篇文章,但它似乎不起作用。 有什么想法吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doulandai0641 2018-06-13 06:01

    Your code is fine just need to change one condition which check the dimension is exist or not. Replace if($product->has_dimensions()) line with following

        $dimensions = $product->get_dimensions();
        if(! empty( $dimensions ))

    This will work for you.

    解决 无用
    打赏 举报

相关推荐 更多相似问题