预先选择首个有库存的 WooCommerce 产品变体。

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

Preselect first in stock product variation woocommerce

问题

我想在没有设置默认值的情况下预先选择第一个有库存的产品变体。

目前我正在使用这段代码https://stackoverflow.com/questions/70935529/automatically-pre-select-first-in-stock-value-in-woocommerce-variable-product-dr,它几乎可以工作,但如果变体已重新排列,它会出错,因为索引不再与变体匹配。

英文:

I’d like to preselect the first product variation that is in stock if no default is set.

Currently I’m using this code https://stackoverflow.com/questions/70935529/automatically-pre-select-first-in-stock-value-in-woocommerce-variable-product-dr and it almost works but if the variations have been rearranged it breaks as the indexs no longer match up with the variations

答案1

得分: 1

I have translated the code you provided:

成功修改了我之前使用的上述代码以找到解决方案

  1. function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
  2. // 检查下拉列表中可用选项的计数
  3. if ( count( $args['options'] ) > 0 ) {
  4. // 初始化
  5. $options = array();
  6. // 获取WC_Product_Variable对象
  7. $product = $args['product'];
  8. $has_default = count($product->get_default_attributes());
  9. if(!$has_default) {
  10. // 是WC产品变体
  11. if ( is_a( $product, 'WC_Product_Variable' ) ) {
  12. // 获取当前产品的可用变体的数组
  13. foreach ( $product->get_available_variations() as $key => $variation ) {
  14. // 是否有库存
  15. $is_in_stock = $variation['is_in_stock'];
  16. $attributes = $variation['attributes'];
  17. // 是的
  18. if ( $is_in_stock ) {
  19. // 设置键
  20. foreach ( $attributes as $key => $attribute ) {
  21. array_push($options, $attribute);
  22. }
  23. // 中断
  24. break;
  25. }
  26. }
  27. }
  28. // 查找变量是否为数字
  29. if ( count( $options ) > 0 ) {
  30. // 已选择
  31. $option_key = "";
  32. foreach ( $options as $key => $option ) {
  33. $i = 0;
  34. while($i < count($args['options'])) {
  35. if($option == $args['options'][$i]) {
  36. $option_key = $i;
  37. // 中断;
  38. }
  39. $i++;
  40. }
  41. // 中断;
  42. }
  43. $args['selected'] = $args['options'][$option_key];
  44. } else {
  45. $args['selected'] = "";
  46. }
  47. }
  48. }
  49. return $args;
  50. }
  51. add_filter( 'woocommerce_dropdown_variation_attribute_options_args', 'filter_woocommerce_dropdown_variation_attribute_options_args', 10, 1 );
英文:

Managed to modify the above code I was using to find the solution

  1. function filter_woocommerce_dropdown_variation_attribute_options_args( $args ) {
  2. // Check the count of available options in dropdown
  3. if ( count( $args[&#39;options&#39;] ) &gt; 0 ) {
  4. // Initialize
  5. $options = array();
  6. // Get WC_Product_Variable Object
  7. $product = $args[&#39;product&#39;];
  8. $has_default = count($product-&gt;get_default_attributes());
  9. if(!$has_default) {
  10. // Is a WC Product Variable
  11. if ( is_a( $product, &#39;WC_Product_Variable&#39; ) ) {
  12. // Get an array of available variations for the current product
  13. foreach ( $product-&gt;get_available_variations() as $key =&gt; $variation ) {
  14. // Is in stock
  15. $is_in_stock = $variation[&#39;is_in_stock&#39;];
  16. $attributes = $variation[&#39;attributes&#39;];
  17. // True
  18. if ( $is_in_stock ) {
  19. // Set key
  20. foreach ( $attributes as $key =&gt; $attribute ) {
  21. array_push($options, $attribute);
  22. }
  23. // Break
  24. break;
  25. }
  26. }
  27. }
  28. // Finds whether a variable is a number
  29. if ( count( $options ) &gt; 0 ) {
  30. // Selected
  31. $option_key = &quot;&quot;;
  32. foreach ( $options as $key =&gt; $option ) {
  33. $i = 0;
  34. while($i &lt; count($args[&#39;options&#39;])) {
  35. if($option == $args[&#39;options&#39;][$i]) {
  36. $option_key = $i;
  37. // break;
  38. }
  39. $i++;
  40. }
  41. // break;
  42. }
  43. $args[&#39;selected&#39;] = $args[&#39;options&#39;][$option_key];
  44. } else {
  45. $args[&#39;selected&#39;] = &quot;&quot;;
  46. }
  47. }
  48. }
  49. return $args;
  50. }
  51. add_filter( &#39;woocommerce_dropdown_variation_attribute_options_args&#39;, &#39;filter_woocommerce_dropdown_variation_attribute_options_args&#39;, 10, 1 );

huangapple
  • 本文由 发表于 2023年5月25日 04:42:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76327270.html
匿名

发表评论

匿名网友

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

确定