Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verygoodplugins/d166a97d6014e8db4df0301435ee625c to your computer and use it in GitHub Desktop.
Save verygoodplugins/d166a97d6014e8db4df0301435ee625c to your computer and use it in GitHub Desktop.
Adds a meta field for wc_coupon and syncs the most recently used coupon to the contact record on checkout
<?php
/*
Plugin Name: WP Fusion - WooCommerce Coupon Sync
Description: Syncs a coupon used at checkout to a custom field on the contact record
Plugin URI: https://verygoodplugins.com/
Version: 1.0
Author: Very Good Plugins
Author URI: https://verygoodplugins.com/
*/
function wpf_coupon_meta_fields( $fields ) {
$fields['wc_coupon'] = array( 'label' => 'Coupon', 'type' => 'text', 'group' => 'woocommerce' );
return $fields;
}
add_filter( 'wpf_meta_fields', 'wpf_coupon_meta_fields' );
function wpf_sync_coupon( $order_id, $contact_id ) {
$order = wc_get_order( $order_id );
$coupons = $order->get_used_coupons();
if( ! empty( $coupons ) ) {
$code = $coupons[0];
wp_fusion()->crm->update_contact( $contact_id, array( 'wc_coupon' => $code ) );
}
}
add_action( 'wpf_woocommerce_payment_complete', 'wpf_sync_coupon', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment