简体   繁体   中英

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. Is there a simple way of doing it? I want to change all values to yes for all my products.

You can create a plugin and put a function in it which runs through your products and updates every products meta value. 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.

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 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. 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/

You can use the following MYSQL query, wp_postmeta depends on your wordpress database prefix

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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