Skip to content

Instantly share code, notes, and snippets.

@SeanTOSCD
Last active February 26, 2020 14:55
Show Gist options
  • Save SeanTOSCD/31b32f9b582e9c7bd32c113f11f613d8 to your computer and use it in GitHub Desktop.
Save SeanTOSCD/31b32f9b582e9c7bd32c113f11f613d8 to your computer and use it in GitHub Desktop.
EDD - bulk edit variable price options more ALL downloads
<?php // DO NOT COPY THIS LINE
/**
* Bulk edit all downloads with variable price options
*
* This code should only be executed when needed. The purpose is to find
* all downloads with variable pricing, and update their respective variable
* pricing labels (Option Name) based strings given in the custom function.
*
* The easiest way to use the function is with the following WordPress plugins:
*
* Debug Bar: https://wordpress.org/plugins/debug-bar/
* Debug Bar Console: https://wordpress.org/plugins/debug-bar-console/
*
* Once installed and activated, enter the following code into the PHP section
* of the Debug Bar - Console interface (be sure not to delete the opening PHP tag).
* Click "Run" to execute the code. That will immediately modify the data, though
* there's no visual indication. Now in a separate tab, visit one of your downloads
* with variable pricing and it should be updated to reflect the new labels.
*
* ALWAYS test on your staging/dev site first and back-up your data before using.
*/
function sd_bulk_edit_variable_pricing_labels() {
// Get all the published downloads
$args = array(
'post_status' => 'publish',
'post_type' => 'download',
'posts_per_page' => -1,
'fields' => 'ids',
);
$all_downloads = get_posts( $args );
foreach ( $all_downloads as $download_id ) {
// Get the download's data
$the_download = new EDD_Download( $download_id );
$has_variable_prices = $the_download->has_variable_prices();
// Proceed only if the download has variable prices
if ( true === $has_variable_prices ) {
// Get the download's variable price data
$variable_prices = get_post_meta( $download_id, 'edd_variable_prices', true );
// Set new labels for the download's variable prices
$variable_prices[1]['name'] = 'First New Option Name'; /* Price ID 1: (new) Option Name */
$variable_prices[2]['name'] = 'Second New Option Name'; /* Price ID 2: (new) Option Name */
// $variable_prices[3]['name'] = 'Third Option Here'; /* Price ID 3: (new) Option Name */
// $variable_prices[4]['name'] = 'Fourth Option Here'; /* Price ID 4: (new) Option Name */
// etc.
/**
* OPTIONAL: Set new descriptions for variable pricing (Variable Pricing Descriptions extension)
* https://easydigitaldownloads.com/downloads/edd-variable-pricing-descriptions/
*
* If you do not use this extension, comment out the two lines below by adding // at the start of the lines
*/
$variable_prices[1]['description'] = 'First New Option Description'; /* Price ID 1: (new) Option Description */
$variable_prices[2]['description'] = 'Second New Option Description'; /* Price ID 2: (new) Option Description */
// $variable_prices[3]['description'] = 'Third Option Description'; /* Price ID 3: (new) Option Description */
// $variable_prices[4]['description'] = 'Fourth Option Description'; /* Price ID 4: (new) Option Description */
// etc.
// Update the download's variable price data
update_post_meta( $download_id, 'edd_variable_prices', $variable_prices );
}
}
}
// Run it!
sd_bulk_edit_variable_pricing_labels();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment