Add_filter | Edit basket
Overview
The ag_opayo_modify_basket filter allows developers to modify the $basket string in the get_cart_items() function before it’s returned. This filter is helpful for customising the basket structure by adding, modifying, or removing items and data within the $basket string.
Purpose
The ag_opayo_modify_basket filter enables developers to hook into the get_cart_items() function, allowing them to modify the basket details of an order dynamically. This could include adjusting item details, fees, or adding custom entries like discounts or promotional items without directly altering the core codebase.
Syntax
$basket = apply_filters( 'ag_opayo_modify_basket', $basket, $order );ag_opayo_modify_basket: The unique identifier for this filter, which developers will use to hook into and modify the$basketdata.- $basket: A string containing formatted data for all items, fees, shipping, discounts, tax, and the order total.
- $order: The
WC_Orderobject from WooCommerce, providing access to detailed order data for the current transaction.
Example Usage
A developer can then hook into this filter using add_filter() to modify the $basket content:
// Create new basket payload
add_filter( 'ag_opayo_modify_basket', 'modify_basket_payload', 10, 2 );
function modify_basket_payload( $basket, $order ) {
// Custom modifications to the $basket
// $basket will pull old basket data from get_cart_items()
$newBasket = ':Custom Item:1:10.00:0.00:10.00:10.00';
return $newBasket;
}In this callback:
modify_basket_payloadreceives both$basketand$order.- The function appends a custom item string to
$newBasketand returns the modified string, which will then be used in theget_cart_items()output.
Was this helpful?