Bulk Update Scripts

As an Oncord developer, if necessary you can leverage the Oncord PHP API to run a script to bulk adjust data within an account.

Important: Executing the example scripts below could cause irreversible damage to the data within your Oncord account, so you should experiment with this code using a trial / test account, rather than an Oncord account that contains important data.

Create a Page & Form for your Script

To get started, create a new Page on the website. This page is usually offline, and hidden in navigation. As a standard, we usually create a new page called "_updatescript". 

With the page editor open, click SRC in the left sidebar, followed by the PHP tab. Any PHP code you write will execute as soon as the page is viewed publicly.

As a safer alternative, it's better practice to add a web form to the page which executes the PHP function containing the upgrade script, using the attribute onsuccessserver="yourfunction()". This approach prevents you from accidentally executing your PHP prematurely by viewing the page.

Add a form to the Page as HTML:

<forms:form id="update_script" onsubmitserver="myFunction()" onsuccesstoastmessage="Your script has run"> <forms:row> <forms:submitbutton value="Run Script" /> </forms:row> </forms:form>


Open the PHP tab, and enter your code:

<?php function myFunction() { // Enter your PHP here } ?>

Some Example Scripts:

updateContacts():
Update all contacts in a group, setting their home phone number to the value of their mobile number.

updatePages():
Bulk edit all child pages of a given parent page. Update the page content to change all instances of <h1> to <h2>.

deletePosts():
Delete all Posts prior to 01-01-2017.

createRecurringSales():
Bulk create recurring sales for specific contacts.

updateGroups():
Update all groups, changing the values of a "Styles" custom field of data type combo box.


<?php function updateContacts() { // Update all contacts in a group, setting their home phone number to the value of their mobile number foreach (\Components\Customers\Groups::getContactsInGroup('/important-group-1/') as $arrContact) { // Option 1 - use save() to save an array record $arrSave = [ 'contact_id' => $arrContact['contact_id'], 'contact_phone_home' => $arrContact['contact_phone_mobile'] ]; \Components\Customers\Contacts::save($arrSave); // Option 2 - Use saveColumn() to update a record's column to a value \Components\Customers\Contacts::saveColumn($arrContact['contact_id'], 'contact_phone_home', $arrContact['contact_phone_mobile']); } } function updatePages() { // Bulk edit on all child pages of a given parent page. Updating the page content to change <h1> to <h2> foreach (\Components\Website\Pages::getChildren('/about/') as $arrPage) { // PHP to replace <h1> with <h2> $str = $arrPage['page_content']; $str = str_replace('<h1', '<h2', $str); $str = str_replace('</h1', '</h2', $str); // Option 1 - Use save() to save an array record $arrPage['page_content'] = $str; \Components\Website\Pages::save($arrPage); // Option 2 - Use saveColumn() to update a record's column to a value \Components\Website\Pages::saveColumn($arrPage['page_id'], 'page_content', $str); } } function deletePosts() { // Delete all Posts prior to 01-01-2017 $arrQuery = [ 'filter' => [['post_date', '<', dbDate(strtotime('1st January 2017'))]] ]; foreach (\Components\Website\Posts::getAll($arrQuery) as $arrPost) { // Optional - delete the linked page if ($arrPost['post_content_url']) { \Components\Website\Pages::delete($arrPost['post_content_url']); } // Delete the post \Components\Website\Posts::delete($arrPost['post_id']); } } function createRecurringSales() { // Bulk create recurring sales for specific contacts // Set the ID of the product to be created for this recurring sale $iProductId = 2; // Set the quantity of the product order $iProductOrderQty = 1; // Set the ID of the payment method to be assigned for this recurring sale $iPaymentMethodId = 1; // Set the recurring interval // Accepted values: ('weekly','fortnightly','monthly','every two months','quarterly','half yearly','yearly','every two years') $strScheduleInterval = 'yearly'; // Get the specific contacts to be created a recurring sale $arrFilter = ['filter' => ['contact_membership_expiry', '!=', null]]; $arrMemberContacts = \Components\Customers\Contacts::getAll($arrFilter); // Create a cart $iCartId = \Components\Commerce\Carts\Current::create(); \Components\Commerce\Carts\Current::setCartId($iCartId); // Get product details $arrItem = \Components\Commerce\Products::get($iProductId); // Add the product to cart $arrProduct = []; $arrProduct['product_id'] = $arrItem['product_id']; $arrProduct['product_quantity'] = $iProductOrderQty; $arrProduct['product_title'] = $arrItem['product_title']; $arrProduct['product_price'] = $arrItem['product_price_base']; $arrProduct['disable_merge'] = true; \Components\Commerce\Carts\Current::addProduct($arrProduct); // Start creating recurring sales per contact foreach($arrMemberContacts as $arrContact) { // Assign contact to the cart \Components\Commerce\Carts\Current::setContactId($arrContact['contact_id']); // Set the recurring sale values $arrRecurringSale = [ 'recurring_sale_schedule_interval' => $strScheduleInterval, 'recurring_sale_run_next_override' => $arrContact['contact_membership_expiry'], 'payment_method_id' => $iPaymentMethodId, 'contact_id' => $arrContact['contact_id'] ]; // Create the recurring sale $iRecurringId = \Components\Commerce\RecurringSales::save($arrRecurringSale); // Get the cart items and details foreach (\Components\Commerce\Carts\Current::getAllDetailed() as $arrCartItem) { // Create the sale items for the recurring sale $arrSaleItem = []; $arrSaleItem['recurring_sale_id'] = $iRecurringId; $arrSaleItem['product_id'] = $arrCartItem['product_id'] ?? null; $arrSaleItem['sale_item_quantity'] = $arrCartItem['product_quantity'] ?? null; $arrSaleItem['sale_item_title'] = $arrCartItem['product_title'] ?? null; $arrSaleItem['sale_item_description'] = $arrCartItem['product_sales_description'] ?? null; $arrSaleItem['sale_item_order_options'] = $arrCartItem['product_order_options_user'] ?? null; $arrSaleItem['sale_item_total_tax'] = $arrCartItem['product_price_total_tax'] ?? null; $arrSaleItem['sale_item_tax_percent'] = $arrCartItem['product_tax_percent'] ?? null; $arrSaleItem['sale_item_price'] = $arrCartItem['product_price'] ?? null; $arrSaleItem['bought_for_contact_id'] = $arrCartItem['bought_for_contact_id'] ?? null; \Components\Commerce\RecurringSales\Items::save($arrSaleItem); } } } function updateGroups() { // Update all groups, changing the values of a Styles custom field of data type combo box $arrFieldOptionsPair = [ [ 'old' => 'Casual', 'new' => 'Loungewear &amp; Sweats' ], [ 'old' => 'Sexy', 'new' => 'Sexy &amp; Elegant' ], [ 'old' => 'Trending', 'new' => '' ], [ 'old' => 'Creative', 'new' => 'Innovative' ], ]; // start updating sub group values foreach(\Components\Customers\Groups::getAll() as $arrGroup) { $arrGroupStyle = unserialize($arrGroup['group_style']); if(!$arrGroupStyle) continue; foreach($arrFieldOptionsPair as $arrFieldOptionPairValue) { // if old value is found, replace/remove it $arrFoundKey = array_search($arrFieldOptionPairValue['old'], $arrGroupStyle); if($arrFoundKey !== false) { if($arrFieldOptionPairValue['old'] == 'Trending') { // this value should be removed unset($arrGroupStyle[$arrFoundKey]); continue; } $arrGroupStyle[$arrFoundKey] = $arrFieldOptionPairValue['new']; } } // save new group_style values \Components\Customers\Groups::saveColumn($arrGroup['group_id'], 'group_style', serialize($arrGroupStyle)); } } ?>