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

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

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化