简体   繁体   English

Woocommerce:如何以编程方式更新所有产品的元属性值

[英]Woocommerce: How to update meta attribute value of all products programmatically

I have a meta attribute on my products that can be accessed like this:我的产品有一个元属性,可以这样访问:

get_post_meta( $product_id, '_disable_shipping', true );

I need to update _disable_shipping attribute of all products on MySQL or programmatically.我需要在 MySQL 上或以编程方式更新所有产品的_disable_shipping属性。 Is there a simple way of doing it?有一种简单的方法吗? I want to change all values to yes for all my products.我想将我所有产品的所有值都更改为yes

You can create a plugin and put a function in it which runs through your products and updates every products meta value.您可以创建一个插件并在其中放置一个 function,它会贯穿您的产品并更新每个产品的元值。 If you don't know how you create a plugin, here is a tutorial for this: https://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/ It is done in minutes.如果您不知道如何创建插件,这里有一个教程: https://www.smashingmagazine.com/2011/09/how-to-create-a-wordpress-plugin/它在几分钟内完成.

Please make sure you have a backup of your database before doing bulk actions to update your products.在执行批量操作以更新您的产品之前,请确保您有数据库的备份。

The function could look something like this: function 看起来像这样:

function disable_shipping_once(){

$products = get_posts(array('post_type' => 'product', 'numberposts' => -1) );

foreach($products as $product) :  


    $product_ID = $product->ID;
    $meta_value = get_post_meta($product_ID, '_disable_shipping',true);

    if($meta_value == "no") :

        update_post_meta($product_ID, '_disable_shipping-include', 'yes' );

    endif;

endforeach; 
}

If you activate your plugin, this function will run.如果你激活你的插件,这个 function 将会运行。 After that, you can deactivate it again.之后,您可以再次停用它。

This code should be pretty save, because it only updates post meta, if the value is "no", so there should not be any unintended updates of the posts.这段代码应该非常省事,因为它只更新帖子元数据,如果值为“否”,则不应有任何意外的帖子更新。


Maybe there also is a plugin for bulk editing your products.也许还有一个用于批量编辑您的产品的插件。 This is the first I found which seems to be doing the job, haven't tested it yet: https://wordpress.org/plugins/custom-field-bulk-editor/这是我发现的第一个似乎在做这项工作,还没有测试过: https://wordpress.org/plugins/custom-field-bulk-editor/

You can use the following MYSQL query, wp_postmeta depends on your wordpress database prefix您可以使用以下 MYSQL 查询, wp_postmeta取决于您的 wordpress 数据库前缀

UPDATE `wp_postmeta` SET `meta_value` = 'yes' WHERE `meta_key` = '_disable_shipping'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM