Documentation

Add_filter | Edit basket

We’ve put this guide together as a little helping hand for developers and designers. You’ll need a basic understanding of PHP to make the most of it.

Just a quick heads-up, we can’t provide support for custom code or offer any bespoke tweaks. Think of the snippets below as examples to guide you in the right direction.

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

php
1
$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 $basket data.
  • $basket: A string containing formatted data for all items, fees, shipping, discounts, tax, and the order total.
  • $order: The WC_Order object 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:

php
123456789
// 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_payload receives both $basket and $order.
  • The function appends a custom item string to $newBasket and returns the modified string, which will then be used in the get_cart_items() output.

Was this helpful?

Opayo

Don't already have the plugin? Get access now.

Still need help?

Our team are on hand to provide fast, helpful and professional support.

Support request
All systems operational

Back End Demo

Start exploring our fully functional demo site today.
Gain exclusive admin access to see what's possible.

Create your demo now

Front End Demo

Test drive our plugin on the demo site.

View Demo