首页 > 解决方案 > 为 woocommerce 变体设置默认的第一个属性

问题描述

我发现了这个问题,但没有人给出有效的答案: Woocommerce Set Default Variations

这里提到的不再起作用,我不明白错误是什么: https ://quadlayers.com/default-product-attributes-woocommerce/

也许有人知道如何确保在输入可变产品时,至少选择了某个选项并且“添加到购物车”按钮处于活动状态?

不幸的是不再有效的代码:

add_action('woocommerce_before_single_product_summary', 'quadlayers_product_default_attributes');
function quadlayers_product_default_attributes() {
      global $product;
      if (!count($default_attributes = get_post_meta($product->get_id(), '_default_attributes'))) {
        $new_defaults = array();
        $product_attributes = $product->get_attributes();
        if (count($product_attributes)) {
          foreach ($product_attributes as $key => $attributes) {
            $values = explode(',', $product->get_attribute($key));
            if (isset($values[0]) && !isset($default_attributes[$key])) {
              $new_defaults[$key] = sanitize_key($values[0]);
            }
          }
          update_post_meta($product->get_id(), '_default_attributes', $new_defaults);
        }
      }
    }  

标签: phpwordpresswoocommercevariations

解决方案


您应该可以选择在变体选项卡中选择“默认表单值: ”,它可以让您为变体选择默认选项。

像这样:

在此处输入图像描述


推荐阅读