Skip to content

Instantly share code, notes, and snippets.

@pingiun
Last active May 29, 2025 15:19
Show Gist options
  • Save pingiun/90501d2e54982dc8daa48cd7d416d8ec to your computer and use it in GitHub Desktop.
Save pingiun/90501d2e54982dc8daa48cd7d416d8ec to your computer and use it in GitHub Desktop.

Hoe gebruik je de extensive description?

Neem dit voorbeeld van een paytium shortcode:

[paytium name="Formulier naam" description="Donatie" button_label="Doneer aan RSP"]
    [paytium_field type="dropdown" label="Kies waarvoor je wil doneren" options="Donatie anti-NAVO campagne/Donatie Palestina-strijdfonds/Donatie RSP landelijk/Donatie afdeling Amsterdam/Donatie afdeling Arnhem/Donatie afdeling Den Haag/Donatie afdeling Eindhoven/Donatie afdeling Nijmegen/Donatie afdeling Noord/Donatie afdeling Overijssel/Donatie afdeling Rotterdam/Donatie afdeling Utrecht/Donatie afdeling Wageningen/Donatie afdeling Zuid-Limburg" options_are_amounts="false" required="true" first_option="Donatie anti-NAVO campagne" /]
    [paytium_field type="open" label="Bedrag donatie:" default="15" quantity_min="3" /]
[/paytium]

Je kan bij elk veld dat je terug wil vinden een payment_label toevoegen in de shortcode, dit wordt dan gebruikt als label in de description.

Voorbeeld:

[paytium name="Formulier naam" description="Donatie" button_label="Doneer aan RSP"]
    [paytium_field payment_label="Campagne" type="dropdown" label="Kies waarvoor je wil doneren" options="Donatie anti-NAVO campagne/Donatie Palestina-strijdfonds/Donatie RSP landelijk/Donatie afdeling Amsterdam/Donatie afdeling Arnhem/Donatie afdeling Den Haag/Donatie afdeling Eindhoven/Donatie afdeling Nijmegen/Donatie afdeling Noord/Donatie afdeling Overijssel/Donatie afdeling Rotterdam/Donatie afdeling Utrecht/Donatie afdeling Wageningen/Donatie afdeling Zuid-Limburg" options_are_amounts="false" required="true" first_option="Donatie anti-NAVO campagne" /]
    [paytium_field type="open" label="Bedrag donatie:" default="15" quantity_min="3" /]
[/paytium]

Hierdoor zal de Mollie description aangepast worden naar:

Donatie 6659
Campagne: Donatie Hoger Onderwijs demonstratie

Het toevoegen van een payment_label heeft geen effect bij een hoeveelheid veld. In het geval hierboven bij "Bedrag donatie:" kan er dus geen payment_label worden toegevoegd, dit is een limitatie vanuit Paytium.

<?php
/*
* Plugin Name: Paytium Extensive Decription
* Description: Adds Paytium field values to the Mollie description field for the information of the accountant.
* Version: 0.1.0
* Author: Jelle Besseling
* Plugin URI: https://gist.github.com/pingiun/90501d2e54982dc8daa48cd7d416d8ec
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Make a class just for scoping things
class ExtensiveDescriptions {
protected const FIELD_PREFIX = 'ext-desc-';
protected static ?ExtensiveDescriptions $instance = null;
private int $next_field_counter = 1;
public function __construct()
{
add_filter('paytium_payment_description', [$this, 'paytium_extensive_description'], 10, 3);
add_filter('shortcode_atts_paytium_field', [$this, 'extend_paytium_field_attrs'], 10, 4);
add_filter('pt_meta_values', [$this, 'payment_label_to_meta'], 10, 2);
add_filter('pt_paytium_field', [$this, 'extend_field_with_payment_label'], 10, 2);
}
/**
* Add any fields in the Paytium shortcode that have a payment_label attribute to the description.
*
* @param $original_desc
* @param $paytium_payment
* @param $meta
* @return mixed|string
*/
public function paytium_extensive_description( $original_desc, $paytium_payment, $meta ) {
$extra_values = [];
foreach ( $meta as $key => $value ) {
if ( str_starts_with($key, 'pt-field-') ) {
// pt-field-dropdown-1
$splits = explode( '-', $key );
// last split will be "1"
if ( ctype_digit($splits[count($splits) - 1]) ) {
$id = $splits[count($splits) - 1];
if (!array_key_exists($id, $extra_values)) {
$extra_values[$id] = [];
}
$extra_values[$id]['value'] = $value;
if (array_key_exists(static::FIELD_PREFIX.$id.'-payment_label', $meta)) {
$extra_values[$id]['label'] = $meta[static::FIELD_PREFIX.$id.'-payment_label'];
}
}
}
}
$add_description = [];
foreach ( $extra_values as $key => $value ) {
if (!array_key_exists('label', $value) || !array_key_exists('value', $value)) {
continue;
}
$add_description[] = sprintf("%s: %s", $value['label'], $value['value']);
}
if (count($add_description) > 0) {
return $original_desc."\n".implode("\n", $add_description);
}
return $original_desc;
}
/**
* Make sure the payment_field attribute in the shortcode is able to be used.
*
* @param $out
* @param $pairs
* @param $atts
* @param $shortcode
* @return mixed
*/
public function extend_paytium_field_attrs($out, $pairs, $atts, $shortcode) {
global $counter;
$this->next_field_counter = $counter;
if (array_key_exists('payment_label', $atts)) {
$out['payment_label'] = $atts['payment_label'];
} else {
$out['payment_label'] = '';
}
return $out;
}
/**
* Store the payment_label attribute from the form in the meta so it can be used by paytium_extensive_description.
*
* @param $meta
* @return mixed
*/
public function payment_label_to_meta($meta) {
if (array_key_exists('pt_items', $_POST)) {
foreach ($_POST['pt_items'] as $itemId => $item) {
if (array_key_exists('payment_label', $item)) {
$meta[static::FIELD_PREFIX.absint($itemId).'-payment_label'] = $item['payment_label'];
}
}
}
return $meta;
}
/**
* Add the payment_label attribute to the form, so it's submitted and can be used by payment_label_to_meta.
*
* @param $html
* @param $args
* @return mixed|string
*/
function extend_field_with_payment_label($html, $args) {
if (array_key_exists('payment_label', $args) && $args['payment_label'] !== '') {
$id = 'pt_items['.$this->next_field_counter.']';
$html .= '<input type="hidden" name="'.$id.'[payment_label]" value="'.$args['payment_label'].'" />';
}
return $html;
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function getInstance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
}
// Instantiate the plugin
ExtensiveDescriptions::getInstance();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment