Save HPP JSON DB

User
Assistant
To save the `$hppJson` data into a new WordPress database table named `track_user_orders`, follow these steps:

### ✅ **Step 1: Create the Table**
Use a plugin like **WP phpMyAdmin**, or run the following SQL in your DB admin panel:

CREATE TABLE IF NOT EXISTS `track_user_orders` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `order_id` VARCHAR(255),
  `site_name` VARCHAR(100),
  `billing_email` VARCHAR(255),
  `hpp_json` TEXT,
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
);

### ✅ **Step 2: Insert `$hppJson` into the Table**

Add the following block **after** the `echo $hppJson;` line in your code:

global $wpdb;

$table_name = $wpdb->prefix . 'track_user_orders';

$wpdb->insert(
    $table_name,
    [
        'order_id'     => $ord,
        'site_name'    => $siteN,
        'billing_email'=> $billingemail,
        'hpp_json'     => maybe_serialize($hppJson),
    ],
    [ '%s', '%s', '%s', '%s' ]
);

### ✅ **Explanation**
– `maybe_serialize()` ensures the JSON is stored properly.
– `$wpdb->prefix` adds your site's actual DB prefix (like `wp_`).
– The data is saved with columns: `order_id`, `site_name`, `billing_email`, and the actual HPP JSON payload.

### ✅ **Optional Debugging**
To check if it's inserting correctly, add:

if (false === $wpdb->last_error) {
    error_log("✅ Order $ord successfully tracked.");
} else {
    error_log("❌ DB Error: " . $wpdb->last_error);
}

Let me know if you want to auto-create the table during plugin/theme activation or add more columns like `product_id`, `total_price`, etc.

Leave a Comment

Your email address will not be published. Required fields are marked *