1. Go to this page and download the library: Download hizzle/store library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Using Main class (recommended)
$db = Main::instance('my_store');
// Get the collection first
$collection = Store::instance('my_store')->get('payments');
// Create a new payment
$payment = $collection->create(array(
'customer_id' => 123,
'amount' => 99.99,
'status' => 'completed',
));
// Get the payment ID
$payment_id = $payment->get_id();
// Using Main class (recommended)
$db = Main::instance('my_store');
// Get a single record by ID
$payment = $db->get('payments', $payment_id);
if ($payment && !is_wp_error($payment)) {
echo $payment->get('amount'); // 99.99
echo $payment->get('status'); // completed
}
// Get ID by a specific property
$payment_id = $db->get_id_by_prop('transaction_id', 'txn_abc123', 'payments');
// Using Collection directly
// Throws \Hizzle\Store\Store_Exception on failure
$collection = Store::instance('my_store')->get('payments');
$payment = $collection->get($payment_id);
// Check if a record exists
if ($collection->exists($payment_id)) {
// Record exists
}
// Get the record using Main class
$db = Main::instance('my_store');
$payment = $db->get('payments', $payment_id);
if ($payment && !is_wp_error($payment)) {
$payment->set('status', 'refunded');
$payment->set('refund_date', current_time('mysql'));
$payment->save();
}
// Or update via collection
// Throws \Hizzle\Store\Store_Exception on failure
$collection = Store::instance('my_store')->get('payments');
$collection->update($payment_id, array(
'status' => 'refunded',
'refund_date' => current_time('mysql'),
));
// Using Main class (recommended)
$db = Main::instance('my_store');
// Delete records matching criteria
$deleted = $db->delete_where(
array(
'status' => 'pending',
'customer_id' => 123,
),
'payments'
);
// Delete all records (use with caution!)
$db->delete_all('payments');
// Or delete via record object
$payment = $db->get('payments', $payment_id);
if ($payment && !is_wp_error($payment)) {
$payment->delete();
}
// Using Main class (recommended)
$db = Main::instance('my_store');
// Add meta data
$db->add_record_meta($payment_id, 'gateway', 'stripe', false, 'payments');
// Get meta data
$gateway = $db->get_record_meta($payment_id, 'gateway', true, 'payments');
// Update meta data
$db->update_record_meta($payment_id, 'gateway', 'paypal', '', 'payments');
// Delete meta data
$db->delete_record_meta($payment_id, 'gateway', '', 'payments');
// Delete all metadata for a record
$db->delete_all_record_meta($payment_id, 'payments');
// Delete all metadata by key across all records
$db->delete_all_meta_by_key('old_field', 'payments');
// Check if meta exists
if ($db->record_meta_exists($payment_id, 'gateway', 'payments')) {
// Meta exists
}
// Using Collection directly
$collection = Store::instance('my_store')->get('payments');
$collection->add_record_meta($payment_id, 'gateway', 'stripe');
$gateway = $collection->get_record_meta($payment_id, 'gateway', true);
$collection->update_record_meta($payment_id, 'gateway', 'paypal');
$collection->delete_record_meta($payment_id, 'gateway');
use Hizzle\Store\Main;
// Main class automatically converts exceptions to WP_Error
$db = Main::instance('my_store');
$payment = $db->get('payments', $payment_id);
if (is_wp_error($payment)) {
error_log($payment->get_error_message());
} else {
// Work with the payment
echo $payment->get('amount');
}
// When using Store/Collection directly, use try/catch
try {
$collection = Store::instance('my_store')->get('payments');
$payment = $collection->get($payment_id);
// Do something with the payment
} catch (\Hizzle\Store\Store_Exception $e) {
error_log($e->getMessage());
// Or convert to WP_Error
$error = new WP_Error(
$e->getErrorCode(),
$e->getMessage(),
$e->getErrorData()
);
}