【问题标题】:Add product dimension while they are empty - Add/edit product - Woocommerce在产品维度为空时添加产品维度 - 添加/编辑产品 - Woocommerce
【发布时间】:2018-06-12 18:51:50
【问题描述】:

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

经过几次测试,我发现为网站上的产品添加 0.001 的长度、宽度和高度会产生最准确的运费。

如果产品尺寸在创建时为空,我需要编写一个函数将产品尺寸设置为 0.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 文章中的结构,但它似乎不起作用。有什么想法吗?

【问题讨论】:

    标签: php wordpress woocommerce metadata product


    【解决方案1】:

    您的代码很好,只需要更改一个检查维度是否存在的条件。 将if($product->has_dimensions()) 行替换为以下内容

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

    这对你有用。

    【讨论】:

      猜你喜欢
      • 2019-08-02
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 2017-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多