douzangdang2225 2018-09-20 15:54
浏览 78

在Woocommerce 3中为产品设置最小默认权重

I'm trying to add a default product shipping weight to WooCommerce in my functions.php file but is not working, I'm not sure if I'm using the right snippets or missing parts of the snippet.

Here's that I'm using,

add_filter('woocommerce_product_get_weight', 'woocommerce_product_get_weight_filter');

function woocommerce_product_get_weight_filter($weight) {
  $default_weight = 0.1; 
  if ((!is_numeric($weight)) || ($weight <= 0.001)) {
    return $default_weight;
  }
  return $weight;
}
  • 写回答

1条回答 默认 最新

  • douyan8772 2018-09-20 16:47
    关注

    Try with empty() and the hook for product variations :

    add_filter('woocommerce_product_variation_get_weight', 'woocommerce_product_get_weight_filter', 10, 2 );
    add_filter('woocommerce_product_get_weight', 'woocommerce_product_get_weight_filter', 10, 2 );
    function woocommerce_product_get_weight_filter( $weight, $product ) {
        if ( empty($weight) || $weight <= 0.001 ) {
            return 0.1;
        }
        return $weight;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    This will not work in admin product edit pages in the weight setting field. The only available hooks are working for "view" context and not in "edit" context (as in backend)

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?