WooCommerce批量修改属性价格

WooCommerce的属性价格存储在post meta表中,key为属性的slug,value是属性值。注意key是有前缀的,为attribute_pa__slug。比如我们要批量更改所有产品中body-type为wm-164cm-j-cup-as-image的价格,我们可以这样:

include("wp-load.php");
$meta_value = "wm-164cm-j-cup-as-image";
$sql = "SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'attribute_pa_body-type' and `meta_value` LIKE '$meta_value'";
global $wpdb;
$datas = $wpdb->get_results($sql);//需要修改的数据
$new_price = "230000";
file_put_contents("选项价格变更.txt",PHP_EOL.PHP_EOL.$meta_value.PHP_EOL,FILE_APPEND);
$i = 0;
foreach ($datas as $data){
$i++;
$post_id = $data->post_id;
$old_price = get_post_meta($post_id, '_price',true);
$old_regular_price = get_post_meta($post_id, '_regular_price',true);
update_post_meta( $post_id, '_price', $new_price );
update_post_meta( $post_id, '_regular_price', $new_price );
$product_id = wp_get_post_parent_id($post_id);
file_put_contents("选项价格变更.txt",$product_id.":".$old_price."->".$new_price.",".$old_regular_price."->".$new_price.PHP_EOL,FILE_APPEND);
}
echo $i."DONE!";
include("wp-load.php");
$meta_value = "wm-164cm-j-cup-as-image";
$sql = "SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'attribute_pa_body-type' and `meta_value` LIKE '$meta_value'";
global $wpdb;
$datas = $wpdb->get_results($sql);//需要修改的数据
$new_price = "230000";
file_put_contents("选项价格变更.txt",PHP_EOL.PHP_EOL.$meta_value.PHP_EOL,FILE_APPEND);
$i = 0;
foreach ($datas as $data){
    $i++;
    $post_id = $data->post_id;
    $old_price = get_post_meta($post_id, '_price',true);
    $old_regular_price = get_post_meta($post_id, '_regular_price',true);
    update_post_meta( $post_id, '_price', $new_price );
    update_post_meta( $post_id, '_regular_price', $new_price );
    $product_id = wp_get_post_parent_id($post_id);
    file_put_contents("选项价格变更.txt",$product_id.":".$old_price."->".$new_price.",".$old_regular_price."->".$new_price.PHP_EOL,FILE_APPEND);
}
echo $i."DONE!";
include("wp-load.php"); $meta_value = "wm-164cm-j-cup-as-image"; $sql = "SELECT * FROM `wp_postmeta` WHERE `meta_key` LIKE 'attribute_pa_body-type' and `meta_value` LIKE '$meta_value'"; global $wpdb; $datas = $wpdb->get_results($sql);//需要修改的数据 $new_price = "230000"; file_put_contents("选项价格变更.txt",PHP_EOL.PHP_EOL.$meta_value.PHP_EOL,FILE_APPEND); $i = 0; foreach ($datas as $data){ $i++; $post_id = $data->post_id; $old_price = get_post_meta($post_id, '_price',true); $old_regular_price = get_post_meta($post_id, '_regular_price',true); update_post_meta( $post_id, '_price', $new_price ); update_post_meta( $post_id, '_regular_price', $new_price ); $product_id = wp_get_post_parent_id($post_id); file_put_contents("选项价格变更.txt",$product_id.":".$old_price."->".$new_price.",".$old_regular_price."->".$new_price.PHP_EOL,FILE_APPEND); } echo $i."DONE!";

文件保存在根目录,然后访问一次就修改好了,上方还一起修改了销售价格和优惠价格,你也可以根据实际情况来修改,让商品看起来更具性价比。

© 版权声明
THE END
喜欢就支持一下吧
点赞4 分享
Fight for the things you love no matter what you may face, it will be worth it.
不管你面对的是什么,为你所爱的而奋斗都会是值得的
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容