woocommerce所有产品尺寸列表

huangapple go评论109阅读模式
英文:

woocommerce list of all products sizes

问题

我需要获取所有产品的尺寸(宽度和高度)。我使用以下代码:

  1. $allProducts = wc_get_products(array(
  2. 'status' => 'publish',
  3. ));
  4. $allWidthArr = array();
  5. $allHeightArr = array();
  6. foreach ( $allProducts as $product ) {
  7. $allWidthArr[] = $product->get_width();
  8. $allHeightArr[] = $product->get_height();
  9. }

我可以在不循环所有产品的情况下获取这些数据吗?

英文:

I need to get of all products sizes(width and height). I use this code:

  1. $allProducts = wc_get_products(array(
  2. 'status' => 'publish',
  3. ));
  4. $allWidthArr = array();
  5. $allHeightArr = array();
  6. foreach ( $allProducts as $product ) {
  7. $allWidthArr[] = $product->get_width();
  8. $allHeightArr[] = $product->get_height();
  9. }

Can I get this data without loop all products?

答案1

得分: 2

我建议使用MYSQL查询。

尝试以下代码行,

  1. global $wpdb;
  2. $list_of_heights = $wpdb->get_results('SELECT post_meta.meta_value FROM '.$wpdb->prefix.'posts posts INNER JOIN '.$wpdb->prefix.'postmeta post_meta
  3. ON post_meta.post_id=posts.ID where post_type="product" and post_meta.meta_key="_height"', ARRAY_N);
  4. $list_of_widths = $wpdb->get_results('SELECT post_meta.meta_value FROM '.$wpdb->prefix.'posts posts INNER JOIN '.$wpdb->prefix.'postmeta post_meta
  5. ON post_meta.post_id=posts.ID where post_type="product" and post_meta.meta_key="_width"', ARRAY_N);
英文:

I would recommend using a MYSQL Query.

Try the following lines of code,

  1. global $wpdb;
  2. $list_of_heights = $wpdb->get_results('SELECT post_meta.meta_value FROM '.$wpdb->prefix.'posts posts INNER JOIN '.$wpdb->prefix.'postmeta post_meta
  3. ON post_meta.post_id=posts.ID where post_type="product" and post_meta.meta_key="_height"',ARRAY_N);
  4. $list_of_widths = $wpdb->get_results('SELECT post_meta.meta_value FROM '.$wpdb->prefix.'posts posts INNER JOIN '.$wpdb->prefix.'postmeta post_meta
  5. ON post_meta.post_id=posts.ID where post_type="product" and post_meta.meta_key="_width"',ARRAY_N);

huangapple
  • 本文由 发表于 2020年1月7日 00:03:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定