ulc_view_codes_data_row
Filters the data row for coupon codes before it is displayed to the user on the WooCommerce Coupons admin page.
add_filter( 'ulc_view_codes_data_row', $callback, 10, 2 );
Description
Filters the HTML for a single row in the "View Codes" admin table before it's displayed. Allows modification of the row's content, including adding or altering action buttons for each coupon. Runs for each coupon displayed in the table.
Usage
add_filter( 'ulc_view_codes_data_row', 'your_function_name', 10, 2 );
Parameters
-
$r(mixed) - This parameter contains the HTML string being built for a single row of coupon codes in the admin view.
-
$coupon(mixed) - This parameter contains the HTML string that is being built for the current row of coupon codes data.
Return Value
The filtered value.
Examples
/**
* Example function to add a custom column or modify an existing one in the Uncanny LearnDash Codes view.
*
* This example adds a new column displaying the coupon's usage count if available,
* or modifies the existing actions column to include a custom link.
*
* @param string $r The HTML string for the current row's data cell.
* @param object $coupon The coupon object being processed.
* @return string The modified HTML string for the row's data cell.
*/
add_filter( 'ulc_view_codes_data_row', 'my_custom_ulc_view_codes_data_row', 10, 2 );
function my_custom_ulc_view_codes_data_row( $r, $coupon ) {
// Check if we are in a context where we want to add custom data,
// for instance, if the row is not for the 'actions' column specifically.
// The actual column context is not directly provided by this hook,
// so we might infer it or assume we're adding to the default output.
// For demonstration, let's assume we want to append some information
// to the existing row data before the actions are typically added.
// If you were targeting a specific column, you'd need to inspect $r
// or potentially use a different hook if available that provides column context.
// Let's add a simple 'Used By:' text and the number of uses if the coupon object has a 'used_count' property.
// This is purely illustrative as the exact structure of $coupon and $r in the default context
// would determine the best way to modify.
$usage_info = '';
if ( isset( $coupon->used_count ) && is_numeric( $coupon->used_count ) ) {
// Assuming $coupon->used_count holds the number of times the coupon has been used.
$usage_info = sprintf( '<span class="ulc-usage-info">Used by: %d</span>', absint( $coupon->used_count ) );
}
// Append the custom information to the existing row output.
// This assumes $r is a string that we're building upon.
// If $r is meant to be an array of cells, this would need adjustment.
$r .= ' ' . $usage_info;
// Alternatively, if the intent is to ADD a new cell or modify the actions cell directly,
// the logic would differ. The current hook filters the output of a specific cell's data.
// If you wanted to add a completely new action button to the actions column,
// you would likely need to parse $r to find where the actions are, or
// if the hook were called *before* actions were finalized, you could prepend/append.
// Given the source context, it seems $r is the cumulative HTML for the cell.
// Let's say we want to add a "View Stats" link *alongside* the edit link.
// This is more complex as we'd need to parse $r to find the existing actions,
// or assume a specific structure. For simplicity and to adhere to the hook's signature,
// we'll just append to the existing $r.
// Example: adding a custom link if a condition is met
if ( isset( $coupon->some_custom_field ) && $coupon->some_custom_field === 'special' ) {
$custom_link = '<a href="' . esc_url( admin_url( 'admin.php?page=my-custom-coupon-page&coupon_id=' . $coupon->ID ) ) . '" class="uncannyowl-btn-action uncannyowl-btn-action--stats">' . __( 'View Stats', 'uncanny-learndash-codes' ) . '</a>';
$r .= ' ' . $custom_link;
}
return $r;
}
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/classes/admin/class-view-codes.php:240
$actions['edit'] = '<a class="uncannyowl-btn-action uncannyowl-btn-action--edit uo-btn-edit-code" href="#" data-code-id="' .
$coupon->ID . '" data-tooltip="' . esc_attr__( 'Edit expiry date', 'uncanny-learndash-codes' ) .
'" data-tooltip-size="sm"><span class="dashicons dashicons-clock"></span></a>';
$r .= implode( ' ', $actions );
break;
default:
$r = apply_filters( 'ulc_view_codes_data_row', $r, $coupon );
break;
}
$r .= '</td>';
}
$r .= '</tr>';
return $r;