V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lcy630409
V2EX  ›  WordPress

wordpress 的 WooCommerce ,商品属性排序问题,求大佬

  •  
  •   lcy630409 · 133 天前 · 598 次点击
    这是一个创建于 133 天前的主题,其中的信息可能已经有所发展或是发生改变。
    通过 wp-all-import 导入的商品,目前都没啥问题
    就是前台显示页面中属性 size 的选项排序不对,比如 该商品 size 有 15 16 17 ,但是前台显示为 17 15 16 这样。
    通过询问 gpt ,在模板函数中添加了下面的代码,还是不行,排序没有改变,我在 usort($terms, function ($a, $b)前后打印了$terms ,发现确实已经完成了排序了,但是前台还是不变,求大佬指教


    function custom_attribute_term_sort($attributes) {
    // 只在产品属性( pa_size )中应用自定义排序
    if (isset($attributes['pa_size'])) {
    $terms = $attributes['pa_size']['terms'];
    usort($terms, function ($a, $b) {

    // 将选项值解析为数字并比较
    $a_parts = explode(' / ', $a->name);
    $b_parts = explode(' / ', $b->name);
    $a_value = intval($a_parts[0]);
    $b_value = intval($b_parts[0]);

    return $a_value - $b_value;
    });
    $attributes['pa_size']['terms'] = $terms;
    }
    return $attributes;
    }
    add_filter('woocommerce_product_get_attributes', 'custom_attribute_term_sort');
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1103 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:23 · PVG 02:23 · LAX 11:23 · JFK 14:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.