Docly

cs_add_custom_json

Estimated reading: 1 minute

Description

With this filter you can add your own custom JSON to the API call to Findock or Converse. This JSON will be added to the root of the JSON object.

Use

				
					add_filter('cs_add_custom_json', 'custom_json', 10);				
			

Example

The example below loops through Woocommerce items in a shopping cart and checks whether Destination IDs have been set. If so, these will be passed in the JSON call to the CampaignSuite API.

				
					function custom_json()
{
    global $woocommerce;
    if ($woocommerce) {
        $items = $woocommerce->cart->get_cart();
        if ($items) {
            $jsonItems = [];
            foreach ($items as $values) {
                $price = get_post_meta($values['product_id'], '_price',true);
                $destination_id = get_post_meta($values['product_id'], '_sf_destination_id', true);
                if ($destination_id) {
                    $jsonItems[] = [
                        'DestinationId' => $destination_id,
                        'Amount' => $price
                    ];
                }
            }
            if ($jsonItems) {
                return [
                    'TransactionInfo' => [
                        'Gifts' => $jsonItems
                    ]
                ];
            }
        }
    }
    return [];
}