php - 为 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);
}
}
}
解决方案
推荐阅读
- html - 如何消除引导程序中列之间的间隙?
- python - 需要 Python 正则表达式的建议
- tensorflow - Tensorflow @tf.function - 无法在 Tensorflow 图函数中获取会话
- installation - angular 不是内部或外部命令、可运行程序或批处理文件
- wordpress - Wordpress 允许跨源 wp-content (CORS)
- java - RabbitMQ 队列问题。消息侦听器不使用前两条消息
- python-3.x - 如何使用地图和列表理解以外的功能加速计算 NumPy 数组中的元素?
- docker - 使用用户凭据在 Cloud Run 中对 gcloud SDK 进行身份验证
- javascript - 使用 css 滚动捕捉,如何使图像不在视图中模糊(不透明)?
- azure - Azure API 管理从定义的变量访问特定值