Add “Where Did You Hear About Us” option in customer invoice email in WordPress

Add “Where Did You Hear About Us” option in customer invoice email in WordPress

Add “Where Did You Hear About Us” option in WordPress

#Add the following code on functions.php to add “How did you find us” option in customer invoice email in WordPress

// For display and saving in order details page.
<?php
add_action( ‘add_meta_boxes’, ‘add_shop_order_meta_box’ );
function add_shop_order_meta_box() {

add_meta_box(
‘custom_column’,
__( ‘Customer Source’, ‘your_text_domain’ ),
‘shop_order_display_callback’,
‘shop_order’
);

}

function shop_order_display_callback( $post ) {

$meta = get_post_meta($post->ID,’customersource’, true);

echo ‘
<div class=”customer_sourse_area”><div class=”customer_sourse_label”>Where did you hear about us?</div></div>
‘;

if($meta){
//$value = get_post_meta( $post->ID, ‘Amouse Order Options’, true );

echo ‘
<div class=””>’.$meta.'</div>
‘;
}else{
echo ‘Not answered’;
}
echo “</div>”;
}

?>

#Add the following code in customer-source.php in the woocommerce folder

<?php

// Store a string into the variable which
// need to be Encrypted
$simple_string = $order->get_id();

// Store the cipher method
$ciphering = “AES-128-CTR”;

// Use OpenSSl Encryption method
//$iv_length = openssl_cipher_iv_length($ciphering);
$options = 0;

// Non-NULL Initialization Vector for encryption
$encryption_iv = ‘1234567891011121’;

// Store the encryption key
$encryption_key = “a2iPU6PX7eiK2Kj4”;

// Use openssl_encrypt() function to encrypt the data
$encryption = openssl_encrypt($simple_string, $ciphering,
$encryption_key, $options, $encryption_iv);

// Display the encrypted string
//echo “Encrypted: ” . $encryption . “\n”;

// Non-NULL Initialization Vector for decryption
$decryption_iv = ‘1234567891011121’;

// Store the decryption key
$decryption_key = “a2iPU6PX7eiK2Kj4”;

// Use openssl_decrypt() function to decrypt the data
$decryption = openssl_decrypt ($encryption, $ciphering,
$decryption_key, $options, $decryption_iv);

// Display the decrypted string
//echo “Decrypted: ” . $decryption;

?>

<form action=”https://amousewithahouse.com.au/how-did-you-find-us” method=”GET”>
<input type=”hidden” name=”oid” value=” <?php echo $encryption; ?> “>
<select name=”platform” style=”width: 180px;height: 45px;border: 1px solid #ccc;padding: 0 8px;”>
<option value=”Facebook”>Facebook</option>
<option value=”Instagram”>Instagram</option>
<option value=”Google search”>Google search</option>
<option value=”Google Ads”>Google Ads</option>
<option value=”Friend”>Friend</option>
<option value=”Word of Mouth”>Word of Mouth</option>
<option value=”Other”>Other</option>
</select>
<input type=”submit” style=”width: 98px;
height: 45px;
border: 1px solid #ccc;
padding: 0 8px;”>
</form>

# Next step is to create a page in wordpress with the form action url we put before so that we can catch the get value then create a wordpress template and apply the template on the page. Put the  following code on the template to process and add meta data.

<?php get_header(); ?>

<?php /* Template Name: Customer Source */ ?>

<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
?>

<?php

//require_once(‘../wp-load.php’);

global $wpdb;

//require_once(‘../wp-load.php’);

//global $wpdb;

$result = array();

if(isset($_GET[‘oid’]) && isset($_GET[‘platform’])){
if(!empty($_GET[‘oid’]) && !empty($_GET[‘platform’])){

$platform = $_GET[‘platform’];

$encryption = $_GET[‘oid’];
$ciphering = “AES-128-CTR”;
$decryption_iv = ‘1234567891011121’;
$options = 0;

// Store the decryption key
$decryption_key = “a2iPU6PX7eiK2Kj4”;

// Use openssl_decrypt() function to decrypt the data
$decryption = openssl_decrypt ($encryption, $ciphering, $decryption_key, $options, $decryption_iv);

//echo $decryption;
// Display the decrypted string
//echo “Decrypted: ” . $decryption;

//echo $_POST[‘order_id’];

$meta = get_post_meta($decryption,’customersource’,true);

//echo “Hello”.$meta;

//delete_post_meta( $decryption,’customersource’, $meta_value = ” );
if(empty($meta)){
add_post_meta( $decryption, ‘customersource’, $platform);
//update_post_meta( $decryption, ‘customersource’, ‘updated’ );
//update_post_meta( $decryption, ‘customersource’, “” );
echo “<div class=’customer_source_add_success’>Add Success</div>”;
echo “<script> var timer = setTimeout(function() { window.location=’https://amousewithahouse.com.au/’ }, 2000); </script>”;
}else{
echo “<div class=’customer_source_add_error’>Some Error Occured</div>”;
echo “<script> var timer = setTimeout(function() { window.location=’https://amousewithahouse.com.au/’ }, 2000); </script>”;
//update_post_meta( $decryption, ‘customersource’, ” );
//header(“Location: https://amousewithahouse.com.au/”);
//die();
}

} else{
echo “Failed”;
}
} else {
echo ‘Failed’;
}

 

?>

<?php get_footer(); ?>

 

 

 

 

 

 

 

 

 

Al Mahbub Khan
Written by Al Mahbub Khan Full-Stack Developer & Adobe Certified Magento Developer

Full-stack developer at Scylla Technologies (USA), working remotely from Bangladesh. Adobe Certified Magento Developer.