Serial Number from custom table not appear in woocommerce_email_before_order_table action - hook

I have a custom table with serial numbers in WordPress. I have successfully got the serial number to appear on both Order received page after testing with Stripe:
https://prnt.sc/9tz8i3BW7lJR
and it also appears on WooCommerce Admin Orders Page:
https://prnt.sc/jLyb5CQqSAH5
I am using the woocommerce_email_before_order_table action. (on customer_completed_order)
I have the code below and I have echoed the Order ID and the Custom TableName and they BOTH appear in the Thanks for shopping with us email.
It seems the $license query returns nothing and I just can't see why it won't appear.
If I exchange the $woo_order_id for the previous order no, like EMS-0051 the serial number appears.
Is this query too early and it hasn't been populated in the custom table before the query is run?
I cannot get it to work..can anyone see what I have done wrong, please?
The Thanks email and CODE are below.
https://prnt.sc/38wa50jTyr3U
<?php
add_action( 'woocommerce_email_before_order_table', 'add_serial_to_email', 25, 4 );
function add_serial_to_email( $order, $sent_to_admin, $plain_text, $email ) {
global $wpdb;
$ipn_tables = $wpdb->prefix ."ipn_data_tbl";
///////BELOW is using 'seq Order No' plugin..this checks if WOO O/N or plugins O/N.
if (empty($order->get_id)) {
$woo_order_id = $order->get_order_number();
}
elseif (empty($order->get_order_number)) {
$woo_order_id = $order->get_id();
}
///check order ID and Table name are there:
if (!empty($woo_order_id && $ipn_tables )) {
echo '<b>ORDER ID:</b> '.$woo_order_id.'<br>'; // echos the Order ID - appears on "Thanks for shopping with us" email
echo '<b>TABLE NAME:</b> '.$ipn_tables.'<br>'; // echo my Custom table name - appears on "Thanks for shopping with us" email
////But the below $license variable doesn't. I think it's a timing thing.
//$license = $wpdb->get_var(" SELECT serial_no FROM $ipn_tables WHERE woo_order_id = $woo_order_id " );
$license = $wpdb->get_var( $wpdb->prepare( "SELECT * FROM {$ipn_tables} WHERE woo_order_id = %s", $woo_order_id ) );
}
if ( $email->id == 'customer_completed_order' ){
printf( '<p class="custom-text">' .__( 'Your Software Serial Number: '.'<span style="color:red;font-weight:bold;font-size:15px">'.$license ));
}
}//function-END
?>
Forgot to show the MyPHPAdmin table:
https://prnt.sc/A4DH1v2STWrL
edit:
I should have mentioned that I put that license check for orderID and table just to see if it was being checked..it appears my get_var query isn't working (empty?) but that same query is used in the other PHP pages I edited.

Looks like I found the issue. It was the fact that 'woocommerce_payment_complete' hook
was too early BUT the hook 'woocommerce_pre_payment_complete' is called first after payment is made but before order status change and before the email is sent. :)
So all I changed in the add_action was change:
woocommerce_payment_complete' TO woocommerce_pre_payment_complete that's it.
And it worked.
part of the add_action updated code

Related

PHP Stripe customers->search not finding accurate results

I'm having difficulty with STRIPE customer->search returning incorrect results sometimes.
If I search for a customer, and if it does not exist then add it, then repeat - I can find it adds the customer a second/third/fourth/etc times - for a while. Meanwhile on the stripe dashboard, I can see the customers appearing [multiple times].
If I waited a few minutes between initial refreshes - it seems ok.
Perhaps there's a cache, or a lag or something between adding a customer and the search finding it but this is not very helpful. Am I wrong - or is there something I'm not doing?
The relevant PHP code is here:
$stripe = new \Stripe\StripeClient($stripe_secretkey);
echo "<LI>Searching for $customerNm...</LI>";
$json = $stripe->customers->search(['query' => 'name:\'' . $customerNm . '\'']);
foreach ($json->data as $key) {
echo "<LI>Found id:[" . $key->id . "]";
if (strcmp($key->name, $customerNm) == 0) {
$customerId = $key->id;
$email = $key->email;
break;
}
}
echo "<hr>";
if (strcmp($customerId, "") == 0) {
echo "<LI>Result to search is <PRE style='margin-left:20px'>$json</PRE>";
$email = str_replace(" ", "", str_replace(" ", "", "abc#$customerNm.com"));
$json = $stripe->customers->create(
[
'email' => $email,
'name' => $customerNm,
]);
$customerId = $json->id;
echo "<UL>Created New STRIPE customer id: [$customerId]</UL>\n";
}
}
This code can be found running on a sandbox here** where if you were to run the page multiple times - it will keep creating customers [for a while].
(Note, this page will create a cookie "posterUID" which is used as the customer name; once it's created then it stays until expiry or manual deletion).
** The full source to this can be seen here
This is expected behavior per Stripe's Search documentation:
Don’t use search for read-after-write flows (for example, searching immediately after a charge is made) because the data won’t be immediately available to search. Under normal operating conditions, data is searchable in under 1 minute. Propagation of new or updated data could be more delayed during an outage.

SuiteScript Script sending statements

I couldn't find an answer to this in docs or here. Is it possible to script sending statements to customers? thanks for any help you can give.
Yes, but it's even easier to do it with a workflow. We created a custom field for a 'Statement Contact' and set up a scheduled workflow based on a 'Saved Search Filter' which basically checks that the customer has a balance. There is one state which contains a single 'Send Email' action. The 'Send Email' action has the option to 'Include Statement'. It fires on the 4th day of each month - you can set this or any other parameter to whatever suits you. This works fine for us, so I hope it helps you.
I found this in SuiteAnswers, it is enough for my purposes.
function printStatement()
{
//this is is the customer id
var id = nlapiGetRecordId();
var email = nlapiGetFieldValue('custentity_accounting_email');
//create an array to set the STATEMENT properties(optional)
var sdate = new Array();
sdate.startdate = '11/01/2015'; // replace it as per requirement
sdate.statementdate = '11/30/2015'; // replace it as per requirement
sdate.openonly = 'T'; // replace it as per requirement
sdate.formnuber = 112; // replace it as per requirement
//print the statement to a PDF file object
var file = nlapiPrintRecord('STATEMENT', id, 'PDF', sdate);
//send the PDF as an attachment
nlapiSendEmail('-5', email, 'Regular Statement', 'Please see attached Statment', null, null, null, file); //change the value of author id.
}

NetSuite SuiteScript Client Side drop down validation

I have a custom form where, in a subtab, I have a dropdown that I need to find out the selected value on the client side after the user selects to perform some validation. I created the script and tied it to the on change event of the dropdown. I cannot seem to find the code to get the selected value on the client side. I have found code to read the value on the server side from a submit event. I need this on the client side on change. I am going to use the ID to look up a record and check a value on that record and if applicable popup a warning to the user. Either SS1 or SS2 is good, whatever would be better I have both available. Any help with this would be great. thanks
In a client script, you can use nlapiGetFieldValue() to retrieve the results.
function fieldchanged(type, name, linenum) {
if(name == 'dropdownid') {
var value = nlapiGetFieldValue('dropdownid');
alert(value);
}
}
OK the nlapiGetFieldValue, did not do the trick, what did was the following
function ValidateField( type, field, linenum ) {
if ( field === 'recordid' ) {
var vendorid = nlapiGetCurrentLineItemValue(type,field,linenum);
var vendorRecord = nlapiLoadRecord('vendor',vendorid);
}
return true;
}
thanks for your help

Write the plugin when Selecting the lookup flied and according to filed selection Show/Hide Address filed in form.....?

We Have Contact Entities in contact Entitie one lookup filed company Name in that lookup having two values 1.Account and 2.Contact . When we are selecting contact show the address filed when we select account hide the address filed we needs to write the plugin to Execute that works. Kindly any one help me on the same.
Thanks!!
Rajesh Singh
First, if you need to make change on a form, you can't use plug-ins. Plug-ins are made for bussinees logics, like Update another record when the first one is created, make complex logic, etc...
What you need it is a javascript that executes on the OnLoad of the form and OnChange event in that OptionSet.
The lines you are looking for are:
function ShowField()
{
// The field is present on the form so we can access to its methods
var optionSet = Xrm.Page.getAttribute("custom_attribute");
if (optionSet == undefined)
{
return;
}
// The control is present on the form so we can access to its methods
var controlAddress = Xrm.Page.getControl("custom_address");
if (controlAddress == undefined)
{
return;
}
var valueOptionSet = optionSet.getValue(); // This is the value you set creating the OptionSet
switch (valueOptionSet)
{
case 0: // Your account value, show
controlAddress.setVisible(true);
case 1: // Your contact value, hide
controlAddress.setVisible(false);
default:
return;
}
}
You need to register a web resource and register the event, if you need more information about the code or why this stuff is here and why this not just tell me.

How can I add the tracking number to the message the users receive?

I have tried editing uc_order module
function uc_order_token_list($type = 'all') {
...
$tokens['order']['order-tracking-number'] = t('The tracking number of the order.');
...
}
function uc_order_token_values($type, $object = NULL) {
...
$values['order-tracking-number'] = uc_tracking_get_order_tracking_numbers($order);
}
Email Message:
[order-first-name] [order-last-name], Your order number [order-link] at [store-name] has been updated. Order status: [order-status] Order tracking number: [order-tracking-number] Order comment: [order-last-comment] Browse to the following page to login to your account and view your order details: [site-login] Thanks again, [store-name] [site-slogan]
But in the message the user receives, the tracking number is not listed.
Actually this did work, I just did not have an actual tracking number for my order since it was a test order, it never actually got shipped.

Resources