Created
July 6, 2018 14:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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