Skip to content

Commit

Permalink
Merge pull request #158 from contemplate/patch-1
Browse files Browse the repository at this point in the history
FIX:  Gift Recipient Name & Email not saving in newer WooCommerce
  • Loading branch information
andrewlimaza authored Feb 3, 2022
2 parents 26e9c5b + f71ce28 commit 600fe1c
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions includes/pmpro-gift-levels.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ function pmprowoo_gift_levels_recipient_fields_validation($passed, $product_id)
$gift_membership_email_option = get_post_meta($product_id, '_gift_membership_email_option', true);

if(!empty($gift_membership_code) && !empty($gift_membership_email_option)){
if($gift_membership_email_option == '3' && $_REQUEST['gift-send-email'] == ''){
wc_add_notice( __( 'Please select option for Send Email to Recipient', 'pmpro-woocommerce' ), 'error' );
return false;
}
if(($gift_membership_email_option == '1') || ($_REQUEST['gift-send-email'] == '1')){
if ( empty( $_REQUEST['gift-recipient-name'] ) ) {
wc_add_notice( __( 'Please enter a NAME of Recipient', 'pmpro-woocommerce' ), 'error' );
Expand All @@ -121,12 +125,16 @@ function pmprowoo_gift_levels_recipient_fields_validation($passed, $product_id)
function pmprowoo_gift_levels_save_recipient_fields( $cart_item_data, $product_id ) {

if( isset( $_REQUEST['gift-recipient-name'] ) ) {
if( !empty( $_REQUEST['gift-recipient-name'] ) ){
$cart_item_data[ 'gift_recipient_name' ] = $_REQUEST['gift-recipient-name'];
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
}
if( isset( $_REQUEST['gift-recipient-email'] ) ) {
if( !empty( $_REQUEST['gift-recipient-email'] ) ){
$cart_item_data[ 'gift_recipient_email' ] = $_REQUEST['gift-recipient-email'];
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
}
return $cart_item_data;
}
Expand Down Expand Up @@ -154,15 +162,16 @@ function pmprowoo_gift_levels_render_on_cart_and_checkout( $cart_data, $cart_ite
/*
Add Frontend fields for Gift Membership to Order meta.
*/
function gift_membership_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['gift_recipient_name'] ) ) {
wc_add_order_item_meta( $item_id, "Recipient Name", $values['gift_recipient_name'] );
function gift_membership_order_meta_handler( $item_id, $cart_item, $order_id ) {

if( isset( $cart_item->legacy_values['gift_recipient_name'] ) ) {
wc_add_order_item_meta( $item_id, 'Recipient Name', $cart_item->legacy_values['gift_recipient_name'] );
}
if( isset( $values['gift_recipient_email'] ) ) {
wc_add_order_item_meta( $item_id, "Recipient Email", $values['gift_recipient_email'] );
if( isset( $cart_item->legacy_values['gift_recipient_email'] ) ) {
wc_add_order_item_meta( $item_id, 'Recipient Email', $cart_item->legacy_values['gift_recipient_email'] );
}
}
add_action( 'woocommerce_new_order_item', 'gift_membership_order_meta_handler', 1, 3 );
add_action( 'woocommerce_new_order_item', 'gift_membership_order_meta_handler', 10, 3 );

/*
Add gift membership code after order is completed.
Expand Down

0 comments on commit 600fe1c

Please sign in to comment.