I have created a custom plugin that updates the product price in woocommerce using API and created a json file to show a progressbar which sends the progress information to the file when the button is clicked. The file cannot be closed until it is complete, causing the old progressbar and the new progressbar file to be called when the user clicks the button multiple times, causing problems with the progress bar.
function import_price_from_api($api_endpoint)
{
global $wpdb;
$base_url = get_option('custom_api_base_url');
$api_token = get_option('custom_api_token');
$args = array(
'method' => "GET",
'timeout' => 60,
'headers' => array(
'Authorization' => 'Bearer ' . $api_token,
),
);
$table_name = $wpdb->prefix . 'orderwise_variants';
$variant_ids = $wpdb->get_col("SELECT variantID FROM $table_name");
$price_variants = count($variant_ids);
$filename = 'price_status1.json';
$fp = fopen($filename, "w");
$arr = array('total' => '0', 'current' => '0');
fwrite($fp, json_encode($arr));
fclose($fp);
foreach ($variant_ids as $index => $variant_id) {
$api_url = $base_url . sprintf($api_endpoint) . '/' . sprintf($variant_id);
$response = wp_remote_get($api_url, $args);
if (is_wp_error($response)) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
continue;
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (is_array($data) && isset($data[0]) && isset($data[0]['variantInfo']) && isset($data[0]['variantInfo']['code'])) {
$variant_sku = $data[0]['variantInfo']['code'];
$product_id = wc_get_product_id_by_sku($variant_sku);
if ($product_id !== 0) {
if (isset($data[0]['variantPurchaseSettings']) && isset($data[0]['variantPurchaseSettings']['estimatedCost'])) {
$price = $data[0]['variantPurchaseSettings']['estimatedCost'];
update_post_meta($product_id, '_regular_price', $price);
update_post_meta($product_id, '_price', $price);
}
}
}
$progress = $index * 100 / $price_variants;
$arr['current'] = $progress;
$arr['total'] = $price_variants;
$fp = fopen($filename, "w");
fwrite($fp, utf8_encode(json_encode($arr)));
fclose($fp);
copy('price_status1.json', 'price_status.json');
ob_flush();
flush();
}
$arr['current'] = 0;
$fp = fopen('price_status.json', "w");
fwrite($fp, utf8_encode(json_encode($arr)));
fclose($fp);
}
<script>
jQuery(document).ready(function($) {
$('#custom_api_integration_price_submit_form').click(function(e) {
e.preventDefault();
$('#custom_api_integration_price_submit').prop('disabled', true);
$('#price-progress-bar').css('width', '0%');
$('#price-progress-bar').text('Progress: 0');
var formData = {
action: 'custom_import_price_ajax',
custom_api_integration_price_submit: true,
custom_api_endpoint: $('#custom_api_endpoint').val()
};
$.ajax({
url: ajax_object.ajax_url,
type: 'POST',
dataType: 'json',
data: formData,
success: function(response) {
},
complete: function() {
},
error: function(xhr, status, error) {
// alert('Error: ' + error);
}
});;
function updateProgressBar() {
$.getJSON('price_status.json', function(data) {
if (data) {
var total = data.total;
var current = data.current;
var width = current.toFixed(2) + '%';
// Update the width of the inner progress bar
jQuery('#price-progress-bar').css('width', width);
// Update text if needed
jQuery('#price-progress-bar').text('Progress: ' + current.toFixed(2) + '%');
// If progress is 100%, stop updating
if (current >= 99) {
clearInterval(progressInterval);
$('#price-progress-bar-container').hide();
$('#progress-message').text('Process completed successfully!');
$('#custom_api_integration_price_submit').prop('disabled', false);
}
}
});
}
updateProgressBar();
var progressInterval = setInterval(updateProgressBar, 2000);
});
})
</script>
Prem Memakiya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.