问题描述
我发现了这个问题,但没有人给出有效的答案: 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);
}
}
}