Ecommerce Shopify WordPress Discussion

WordPress Plugin not adding the API response data into the database

I have been trying to make a plugin for my wordpress website and it seems that that it's not functioning at all. I am trying to get a response from an API and trying to save it in the database. However, after saving the product(supposedly the button to trigger the API), the db still doesn't get populated with the data. register_activation_hook(__FILE__, 'product_shipping_calculator_activate'); // Activation function function product_shipping_calculator_activate() { // Add any activation tasks here } // Add settings page to product data meta box add_action('woocommerce_product_options_general_product_data', 'product_shipping_calculator_product_options_general_product_data'); function product_shipping_calculator_product_options_general_product_data() { global $post; echo '<div class="options_group">'; woocommerce_wp_text_input(array( 'id' => 'product_shipping_calculator_api_url', 'label' => __('API URL for Product Information', 'woocommerce'), 'placeholder' => __('Enter API URL', 'woocommerce'), 'desc_tip' => 'true', 'description' => __('Enter the API URL for fetching product information.', 'woocommerce') )); woocommerce_wp_text_input(array( 'id' => 'product_shipping_calculator_api_key', 'label' => __('API Key', 'woocommerce'), 'placeholder' => __('Enter API Key', 'woocommerce'), 'desc_tip' => 'true', 'description' => __('Enter the API Key for authentication.', 'woocommerce') )); woocommerce_wp_textarea_input(array( 'id' => 'product_shipping_calculator_request_body_options', 'label' => __('Request Body Options', 'woocommerce'), 'placeholder' => __('Enter request body options in JSON format', 'woocommerce'), 'desc_tip' => 'true', 'description' => __('Enter the request body options in JSON format for the API request.', 'woocommerce') )); echo '</div>'; } // Save the API URL, API Key, and Request Body Options when the product is saved add_action('woocommerce_process_product_meta', 'product_shipping_calculator_save_product_meta'); function product_shipping_calculator_save_product_meta($post_id) { $api_url = isset($_POST['product_shipping_calculator_api_url']) ? $_POST['product_shipping_calculator_api_url'] : ''; $api_key = isset($_POST['product_shipping_calculator_api_key']) ? $_POST['product_shipping_calculator_api_key'] : ''; $request_body_options = isset($_POST['product_shipping_calculator_request_body_options']) ? $_POST['product_shipping_calculator_request_body_options'] : ''; update_post_meta($post_id, 'product_shipping_calculator_api_url', sanitize_text_field($api_url)); update_post_meta($post_id, 'product_shipping_calculator_api_key', sanitize_text_field($api_key)); update_post_meta($post_id, 'product_shipping_calculator_request_body_options', sanitize_text_field($request_body_options)); } // Fetch and save shipping rates when the product is saved add_action('woocommerce_process_product_meta', 'product_shipping_calculator_fetch_and_save_shipping_rates'); function product_shipping_calculator_fetch_and_save_shipping_rates($post_id) { // Get the API URL, API Key, and Request Body Options from post meta $api_url = get_post_meta($post_id, 'product_shipping_calculator_api_url', true); $api_key = get_post_meta($post_id, 'product_shipping_calculator_api_key', true); $request_body_options = get_post_meta($post_id, 'product_shipping_calculator_request_body_options', true); // Make sure the API URL and API Key are not empty if (!empty($api_url) && !empty($api_key)) { // Set your request headers $headers = array( 'Content-Type' => 'application/json', 'Authorization' => 'jwt ' . $api_key, // Use dynamic API key ); // Build the request body based on user input $body = json_decode($request_body_options, true); // Make an HTTP request to the API $response = wp_remote_request( $api_url, array( 'method' => 'POST', // Change the method to POST if needed 'headers' => $headers, 'body' => json_encode($body), // Encode the body as JSON if needed ) ); // Check if the request was successful if (!is_wp_error($response) && $response['response']['code'] === 200) { // Parse the JSON response $data = json_decode($response['body'], true); // Check if the response contains zone rates if (isset($data[0]['zone_rates'])) { // Extract and save the zone rates as post meta $zone_rates = $data[0]['zone_rates']; // Assuming the response is for a single product update_post_meta($post_id, 'product_shipping_calculator_zone_rates', $zone_rates); } } } } P.S: I created this with the help of you chatgpt because of less knowledge of PHP
TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.