Last active
July 31, 2024 13:12
-
-
Save SeanTOSCD/31b32f9b582e9c7bd32c113f11f613d8 to your computer and use it in GitHub Desktop.
EDD - bulk edit variable price options more ALL downloads
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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