WooCommerce PDF Invoice Customization
December 24, 2024 ยท AI Conversations
function woocommerce_pdf_invoice_new_labels( $content , $order_id){
$sign_img ='';
$prescriber_id = get_post_meta($order_id,'_prescriber_id',true);
$user_info = get_userdata($prescriber_id);
$prescriber_name = isset($user_info->display_name) && $user_info->display_name!='' ? $user_info->display_name : $user_info->user_nicename;
$prescriber_sign = get_user_meta($prescriber_id,'prescriber_signature',true);
if($prescriber_sign){
$sign_url = wp_get_attachment_url($prescriber_sign);
$sign_url = str_replace(site_url('/'),'',$sign_url);
$sign_img ='';
}
$content = str_replace( '[[BILLING_FNAME]]', get_post_meta( $order_id,'_billing_first_name',TRUE ),$content);
$content = str_replace( '[[BILLING_LNAME]]', get_post_meta( $order_id,'_billing_last_name',TRUE ),$content);
$content = str_replace( '[[BILLING_DOB]]', get_post_meta( $order_id,'prescription_dob',TRUE ),$content);
// $content = str_replace( '[[ORDER_DATE]]', date('F j, Y',strtotime(get_post_meta( $order_id,'_completed_date',TRUE ))),$content);
$content = str_replace( '[[ORDER_DATE]]', date('F j, Y',strtotime(get_post_meta( $order_id,'_dispensed_date',TRUE ))),$content);
$content = str_replace( '[[MEDICATION_INFO]]',get_medication_details($order_id),$content);
$content = str_replace( '[[PRESCRIBER_NAME]]',$prescriber_name,$content);
$content = str_replace( '[[PRESCRIBER_REG_NUM]]',get_user_meta($prescriber_id,'prescriber_registration_number',true),$content);
$content = str_replace( '[[PRESCRIBER_SIGN]]',$sign_img,$content);
return $content;
}
function get_medication_details( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
// Check WC version – changes for WC 3.0.0
$pre_wc_30 = version_compare( WC_VERSION, '3.0', '
### Optimization Suggestions
1. **Prevent Duplicate Updates**: Ensure `update_post_meta` is only executed if `_prescriber_id` is not already set:
function track_prescriber($order_id, $order){
$user_id = get_current_user_id();
if (!get_post_meta($order_id, '_prescriber_id', true)) {
update_post_meta($order_id, '_prescriber_id', $user_id);
}
}
add_action('woocommerce_order_status_dispensing', 'track_prescriber', 999, 2);
2. **Batch Processing**: If bulk order updates are causing the issue, consider using a scheduled task to process orders in batches rather than executing `update_post_meta` during the transition.
3. **Offload to Background Processing**: Use a background processing library like [Action Scheduler](https://actionscheduler.org/) to handle meta updates asynchronously.
Example:
add_action('woocommerce_order_status_dispensing', 'schedule_prescriber_update', 10, 2);
function schedule_prescriber_update($order_id, $order) {
$user_id = get_current_user_id();
if (!get_post_meta($order_id, '_prescriber_id', true)) {
as_enqueue_async_action('update_prescriber_meta', ['order_id' => $order_id, 'user_id' => $user_id]);
}
}
add_action('update_prescriber_meta', 'update_prescriber_meta', 10, 1);
function update_prescriber_meta($args) {
update_post_meta($args['order_id'], '_prescriber_id', $args['user_id']);
}
4. **Database Optimization**: Index the `postmeta` table to speed up meta key searches. For instance:
ALTER TABLE wp_postmeta ADD INDEX meta_key_index (meta_key(191));
5. **Avoid Redundant Hooks**: Ensure that `track_prescriber` is not being executed multiple times due to repeated status transitions or hook duplications.
6. **Profile the Code**: Use tools like Query Monitor to analyze the exact cause of slowdowns. Look for:
– Redundant queries in the `postmeta` table.
– External API calls or other operations triggered unintentionally during the order status update.
By implementing these optimizations, you can significantly improve the performance when transitioning orders to the dispensing status.
—
### 1. **Remove Unnecessary Hook Registrations**
– Ensure the hook is not registered multiple times unintentionally.
– Example:
add_action('woocommerce_order_status_dispensing', 'track_prescriber', 999, 2);
Ensure this code is included only once in your application.
– If your code runs in multiple files or plugins, wrap it in a condition:
if (!has_action('woocommerce_order_status_dispensing', 'track_prescriber')) {
add_action('woocommerce_order_status_dispensing', 'track_prescriber', 999, 2);
}
—
### 2. **Check Current Meta Values Before Updating**
Avoid redundant updates by checking if the meta value already exists:
function track_prescriber($order_id, $order) {
$user_id = get_current_user_id();
if (!get_post_meta($order_id, '_prescriber_id', true)) {
update_post_meta($order_id, '_prescriber_id', $user_id);
}
}
add_action('woocommerce_order_status_dispensing', 'track_prescriber', 999, 2);
—
### 3. **Prevent Multiple Transitions Triggering the Hook**
WooCommerce can fire multiple status changes for the same order, especially if status transitions occur in bulk or programmatically.
Use a custom order meta key to track whether the action has already been performed:
function track_prescriber($order_id, $order) {
if (!get_post_meta($order_id, '_prescriber_handled', true)) {
$user_id = get_current_user_id();
update_post_meta($order_id, '_prescriber_id', $user_id);
update_post_meta($order_id, '_prescriber_handled', 'yes');
}
}
add_action('woocommerce_order_status_dispensing', 'track_prescriber', 999, 2);
—
### 4. **Unhook Dynamically if Needed**
If the hook may not always be required, you can unhook it after execution:
function track_prescriber_once($order_id, $order) {
$user_id = get_current_user_id();
update_post_meta($order_id, '_prescriber_id', $user_id);
// Unhook to prevent redundant execution.
remove_action('woocommerce_order_status_dispensing', 'track_prescriber_once', 999);
}
add_action('woocommerce_order_status_dispensing', 'track_prescriber_once', 999, 2);
—
### 5. **Use Debugging Tools to Identify Duplicate Hooks**
Use plugins like [Query Monitor](https://wordpress.org/plugins/query-monitor/) or custom debugging to log when and how often your hook is triggered:
function track_prescriber_debug($order_id, $order) {
error_log("track_prescriber called for Order ID: $order_id");
}
add_action('woocommerce_order_status_dispensing', 'track_prescriber_debug', 999, 2);
—
### 6. **Scope Hook Registrations to Admin or Frontend**
If the hook is only required in specific contexts (e.g., admin area), use conditional checks:
if (is_admin()) {
add_action('woocommerce_order_status_dispensing', 'track_prescriber', 999, 2);
}
—
### 7. **Avoid Hook Re-registration in Dynamic Contexts**
If the hook is registered within a function or dynamically executed code (e.g., plugin initialization or theme setup), ensure it isn't called multiple times unintentionally:
function initialize_plugin() {
static $initialized = false;
if (!$initialized) {
add_action('woocommerce_order_status_dispensing', 'track_prescriber', 999, 2);
$initialized = true;
}
}
initialize_plugin();
—
### Conclusion
By following these best practices, you can prevent redundant hook executions and improve your site's performance and reliability. Regularly audit your code and use tools to monitor hook behavior to identify any inefficiencies or duplicate executions.