gpt4 book ai didi

woocommerce - 我想在 woocommerce 产品管理部分的下拉列表中以特定顺序显示 "Attributes"

转载 作者:行者123 更新时间:2023-12-02 04:12:48 24 4
gpt4 key购买 nike

我想在 woocommerce 产品管理部分的下拉列表中以特定顺序显示“属性”。属性尚未按正确的顺序输入。现在重做已经太晚了,所以我想在下拉菜单中列出它们,因为它们应该出现。对于输入数据的人来说,上下滚动此列表太耗时。如果它们已经按正确的顺序排列,速度会更快。我已经看到如何强制它们在前端按特定顺序显示,但不在管理部分 - 谢谢

最佳答案

运行此代码一次,然后将其删除。因此,将其加载到插件中,激活插件,然后停用。我考虑过自动停用或创建一次运行一次的 transient ,但我会让你处理它。如果代码第一次不正确,这些实际上可能会很麻烦。

也许您可以调整第二个函数,使其也可以在 save_post 上运行或替换 attributes_cmp()以防商店经理将来以错误的顺序添加属性。实际上一个名为 update_post_metadata 的过滤器这将允许您在每次更新帖子和保存元数据时一致地应用此功能。

警告备份您的数据库!此代码具有破坏性,将永久更改您的数据库,并且除了看到新的排序函数在演示数组上运行之外,还没有经过测试。使用风险自负。

function so_35733629_update_products(){
$args = array(
'posts_per_page' => -1,
'meta_value' => '',
'post_type' => 'product',
'post_status' => 'any',
);
$products_array = get_posts( $args );

foreach( $products_array as $product ){
$attributes = get_post_meta( $product->ID, '_product_attributes', true );
if( ! empty( $atttributes ) ){
$attributes = so_35733629_reorder_attributes( $attributes );
update_post_meta( $product->ID, '_product_attributes', $attributes );
}
}
}
add_action( 'admin_init', 'so_35733629_update_products' );

function so_35733629_reorder_attributes( $attributes ){

// here is the desired order
$order = array(
'pa_maker',
'pa_model-name',
'pa_man-lady',
'pa_model-case-ref',
'pa_case-serial'
);

$new_attributes = array();

// create new array based on order of $order array
foreach( $order as $key ){
if( isset( $attributes[$key] ) ){
// add to new attributes array
$new_attributes[$key] = $attributes[$key];
// remove from the attributes array
unset( $attributes[$key] );
}
}

// merge any leftover $attributes in at the end so we don't accidentally lose anything
$new_attributes = array_merge( $new_attributes, $attributes );

// set the new position keys
$i = 0;
foreach( $new_attributes as $key => $attribute ){
// update the position
$new_attributes[$key]['position'] = $i;
$i++;
}

return $new_attributes;
}

关于woocommerce - 我想在 woocommerce 产品管理部分的下拉列表中以特定顺序显示 "Attributes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35733629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com