woocommerce_before_order_notes /woocommerce_thankyou
-
I am using the above hooks to include information I have stored in a database file linked to a cookie. This works except when the Client chooses Pagseguro. I have set the option to “Lightbox” but it still soen’t seems to work.
The function that displays the Schoolname in the Top_bar shortcode, is the same one that is used to retrieve the School name for the hooks. I attach the functions code and the Data base table with the Cookie info stored.
The context is as follows:
Franchised schools are allowed to buy books from the store. They are given a SchoolName (schoolid) and School Password (schoollogin).
After realising I couldn’t use PHP Sessions, I am using a Cookie (jamer_session_id) details of which I save in the database file If the login process is successful, I update the database record with the schoolid (and other information necessary for pricing etc)
Whenever the SchoolId is required, it is obtained from the database using a function mc_find_schoolid(). This function checks the Cookie is set and then looks up the Schoolid. This works fine and the School Id is displayed on the top_bar and used for calculating prices.
It is important that this information is available for processing the order as delivery and invoicing is to the school (not the person making the order). So I am using hooks woocommerce_before_order_notes / woocommerce_thankyou hook to ensure the back office staff can correctly process the order.
On return from the Pagseguro Lightbox, the top bar continues to correctly display the SchoolId, but often the School Id does not appear in the Order confirmation screen not in the email confirmation.
I would really appreciate any help in trying to find a robust solution to this.
`<?phpadd_action(‘init’, ‘init_jamer_cookies’);
function init_jamer_cookies () {
global $wpdb;
global $wp;
global $woocommerce;
$jamer_session_id=$_COOKIE[‘jamer_session_id’];
if(!isset($jamer_session_id)) {
$time=time()+31556926;
$randomString=generateRandomString(20);
setcookie(‘jamer_session_id’, $randomString, $time, COOKIEPATH, COOKIE_DOMAIN);
}
return;
}function generateRandomString($length ) {
$characters = ‘0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’;
$charactersLength = strlen($characters);
$randomString = ”;
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength – 1)];
}
return $randomString;
}
//add_shortcode(‘mc_check_access1′,’mc_check_access’);
// Checks access
function mc_check_access() {
global $wpdb;
global $wp;
global $woocommerce;
$jamer_session_id=$_COOKIE[‘jamer_session_id’];
$schoolid=””;
$schoolid=mc_find_schoolid();
if(isset($jamer_session_id)) {
$tablename=’jamercookies’;
$tablename_all = $wpdb->prefix.$tablename;
$querycode=”SELECT * from $tablename_all WHERE jamer_session_id = ‘$jamer_session_id'”;
$rs=$wpdb->get_results($querycode);
$rscount=count($rs);
if ($rscount>=1) {
foreach ($r as $rs) {
$id=$r->ID;
$wpdb->delete($tablename_all, array(‘ID’=> $id));
}}
elseif ($rscount==0){
$wpdb->insert($tablename_all, array(‘jamer_session_id’ => $jamer_session_id, ‘schoolid’ => $schoolid ));
}
}$url = wc_get_page_permalink( ‘home’ );
if ($_POST[“submit”] ) {
$schoolname = $_POST[“SchoolName”];
$schoollogin = $_POST[“SchoolLogin”];$schoolset=0;
$schoolset = set_jamer_session ($schoolname, $schoollogin);
if ($schoolset=1 ) {
$_POST=array();
$schoolset=0;
$url = wc_get_page_permalink( ‘shop’ );
wp_redirect( $url );
exit;}
}
return;
}
//
// Sets up Session Variablesfunction set_jamer_session ($postedschoolname, $postedschoollogin) {
global $wpdb;
global $wp;
$schoolset=0;
$tablename=”schools”;
$tablename_all = $wpdb->prefix.$tablename;
$querycode=”select schoolid, password from $tablename_all where schoolid=’$postedschoolname’ and password=’$postedschoollogin'”;
$rs=$wpdb->get_results($querycode);
$rscount=count($rs);
if ($rscount==1) {
$count=1;
foreach($rs as $r) {
$schoolid= trim($r->schoolid);
$schoolidpassword= trim($r->password);
}
$tablename2=”jamercookies”;
$tablename_all2 = $wpdb->prefix.$tablename2;
$jamercookieid=$_COOKIE[‘jamer_session_id’];
if(isset($jamercookieid) ) {
$wpdb->update($tablename_all2, array( ‘schoolid’ => $schoolid), array(‘jamer_session_id’ => $jamercookieid));
}
$schoolset=1;
}
return $schoolset;
}//
function mc_find_schoolid() {
global $wpdb;
global $wp;
global $woocommerce;
$schoolid=””;
$jamercookieid=$_COOKIE[‘jamer_session_id’];
if(isset($jamercookieid)) {
$tablename=”jamercookies”;
$tablename_all = $wpdb->prefix.$tablename;
$querycode=”select schoolid from $tablename_all where jamer_session_id =’$jamercookieid'”;
$rs=$wpdb->get_results($querycode);
$rscount=count($rs);
if ($rscount>=1) {
$count=1;
foreach($rs as $r) {
$schoolid1= trim($r->schoolid);
if ($schoolid1!=””) {
$schoolid=$schoolid1;
}
}}}return $schoolid;
}// Creates Top_bar
add_shortcode( ‘mc_top_bar’, ‘mc_top_bar_function’ );
function mc_top_bar_function () {
global $current_user; wp_get_current_user();
global $wpdb;
global $wp;
global $woocommerce;
$schoolid=mc_find_schoolid();
if(isset($schoolid)){
$text = “”;
if ( is_user_logged_in() ) {
$text = “Olá $current_user->display_name ,”;
}
$text = $text . “Suas compras s?o para $schoolid”;// ;
}
else {
$text = “No School set”;
$url = wc_get_page_permalink( ‘home’ ) ;
wp_redirect( str_replace( ‘&’, ‘&’, $url ) );
}
echo $text;
return;
}//
// Clears Woocommerce Cart
add_shortcode( ‘mc_school_logout’, ‘mc_school_logout_call’) ;function mc_school_logout_call () {
global $woocommerce;
global $wpdb;
global $wp;
$tablename=”jamercookies”;
$tablename_all = $wpdb->prefix.$tablename;
$jamercookieid=$_COOKIE[‘jamer_session_id’];
if(isset($jamercookieid)) {
$wpdb->delete($tablename_all, array(‘jamer_session_id’ => $jamercookieid));
unset( $_COOKIE[‘jamer_session_id’] );
setcookie( ‘jamer_session_id’, ”, time() – ( 15 * 60 ) );
}
WC()->cart->empty_cart();
wp_redirect( str_replace( ‘&’, ‘&’, wc_get_page_permalink( ‘home’ ) ) );
return;
}// Our hooked in function – $fields is passed via the filter!
function mc_override_checkout_fields( $fields ) {
global $woocommerce;
global $wpdb;
$tablename=”schools”;
$schoolid = mc_find_schoolid();
//checks School is set
if(!isset($schoolid)){
$url = wc_get_page_permalink( ‘home’ ) ;
wp_redirect( str_replace( ‘&’, ‘&’, $url ) );
}
//
$tablename_all = $wpdb->prefix.$tablename;
$querycode=”select Razao_Social, ad_line_1, ad_line_2, bairro, cidade, UF, CEP, CNPJ from $tablename_all where schoolid=’$schoolid'”;
$rs=$wpdb->get_results($querycode);
$rscount=count($rs);if ($rscount=1) {
foreach($rs as $r) {
$fields[‘Billing’][‘billing_company’] = $r->Razao_Social;
$fields[‘Billing’][‘billing_address_1’] = $r->ad_line_1;
$fields[‘Billing’][‘billing_address_2’] = $r->ad_line_2;
$fields[‘Billing’][‘billing_neighborhood’] = $r->bairro;
$fields[‘Billing’][‘billing_city’] = $r->cidade;
$fields[‘Billing’][‘billing_postcode’] = $r->CEP;
$fields[‘Billing’][‘billing_state’] = $r->UF;
$fields[‘Billing’][‘billing_cnpj’] = $r->CNPJ;
}
}return $fields;
}
//
// Actions to print out delivery address
//
add_action (‘woocommerce_before_order_notes’, ‘mc_notes’);
add_action( ‘woocommerce_thankyou’, ‘mc_notes’, 10, 1 );//
// Function to print out delivery address
//
function mc_notes () {
global $woocommerce;
global $wpdb;
global $wp;
$schoolid2 = mc_find_schoolid();
//checks School is set
if($schoolid2==””){
$url = wc_get_page_permalink( ‘home’ ) ;
wp_redirect( str_replace( ‘&’, ‘&’, $url ) );
}
//
$tablename=”schools”;
$tablename_all = $wpdb->prefix.$tablename;
$querycode=”select Razao_Social, ad_line_1, ad_line_2, bairro, cidade, UF, CEP, CNPJ from $tablename_all where schoolid=’$schoolid2′”;
$rs=$wpdb->get_results($querycode);
$rscount=count($rs);
echo ‘<h3> Seu Pedido (para ‘.$schoolid2.’) será entregue em: </h3><br>’;
if ($rscount=1) {foreach($rs as $r) {
echo ‘<div style=”margin: 0 0 0;”><p class=”mcnotes”>’.$r->Razao_Social.'</p>’;
echo ‘<p class=”mcnotes”>’.$r->ad_line_1.'</p>’;
echo ‘<p class=”mcnotes”>’.$r->ad_line_2.'</p>’;
echo ‘<p class=”mcnotes”>’.$r->bairro.'</p>’;
echo ‘<p class=”mcnotes”>’.$r->cidade.'</p>’;
echo ‘<p class=”mcnotes”>’.$r->UF.'</p>’;
echo ‘<p class=”mcnotes”>’.$r->CEP.'</p>’;
echo ‘<p class=”mcnotes”>CNPJ: ‘.$r->CNPJ.'</p></div><br><br>’ ;}
}return ;
}add_action(‘woocommerce_email_footer’, ‘mc_notes_email’,10, 1);
// * Function to print out delivery address in emails
//
function mc_notes_email ($email) {
// if ( $email->id == ‘cancelled_order’ ) {mc_notes();}
// if ( $email->id == ‘customer_completed_order’ ) {mc_notes();}
// if ( $email->id == ‘customer_invoice’ ) {mc_notes();}
if ( $email->id == ‘customer_processing_order’ ) {mc_notes();}
// if ( $email->id == ‘customer_new_account’ ) {mc_notes();}
// if ( $email->id == ‘customer_note’ ) {mc_notes();}
if ( $email->id == ‘customer_on_hold_order’ ) {mc_notes();}
// if ( $email->id == ‘customer_refunded_order’ ) {mc_notes();}
// if ( $email->id == ‘customer_reset_password’ ) {mc_notes();}
// if ( $email->id == ‘failed_order’ ) {mc_notes();}
if ( $email->id == ‘admin_new_order’ ) {mc_notes();}
if ( $email->id == ‘new_order’ ) {mc_notes();}}
//
//
//
// // // // Hook in
add_filter( ‘woocommerce_checkout_fields’ , ‘mc_custom_override_checkout_fields’ );// Function to stop clients changing delivery address
function mc_custom_override_checkout_fields( $fields ) {
unset($fields[‘order’][‘order_comments’]);
unset($fields[‘billing’][‘billing_persontype’]);
unset($fields[‘billing’][‘billing_cpf’]);
unset($fields[‘billing’][‘billing_cnpj’]);
unset($fields[‘billing’][‘billing_rg’]);
unset($fields[‘billing’][‘billing_ie’]);
unset($fields[‘billing’][‘billing_company’]);
unset($fields[‘billing’][‘billing_address_1’]);
unset($fields[‘billing’][‘billing_address_2’]);
unset($fields[‘billing’][‘billing_number’]);
unset($fields[‘billing’][‘billing_neighborhood’]);
unset($fields[‘billing’][‘billing_city’]);
unset($fields[‘billing’][‘billing_postcode’]);
unset($fields[‘billing’][‘billing_state’]);
unset($fields[‘billing’][‘billing_country’]);
unset($fields[‘billing’][‘billing_state’]);
return $fields;
}*/
add_action( ‘woocommerce_after_checkout_billing_form’, ‘mc_pupil_checkout_field’ );function mc_pupil_checkout_field($checkout) {
global $woocommerce;
echo ‘<div><h3>Nome do Aluno</h3>’;
echo ‘<div class=”mc_field”>’;
woocommerce_form_field( ‘pupilname’, array(
‘type’ => ‘text’,
‘label’ => ‘Nome do Aluno’,
‘class’ => array( ‘mc-field’, ‘form-row-wide’ ),
‘required’ => true,
), $checkout->get_value( ‘pupilname’)
);
echo ‘</div>’;
echo ‘</div>’;echo ‘<div> <h3>Turma</h3>’;
echo ‘<div class=”mc_field”>’;
woocommerce_form_field( ‘classname’, array(
‘type’ => ‘text’,
‘label’ => ‘Turma’,
‘class’ => array( ‘mc-field’, ‘form-row-wide’ ),
‘required’ => true,
), $checkout->get_value( ‘classname’ )
);
echo ‘</div>’;
echo ‘</div>’;echo ‘<div> <h3>Escola</h3>’;
echo ‘<div class=”mc_field”>’;
woocommerce_form_field( ‘schoolname’, array(
‘type’ => ‘text’,
‘label’ => ‘Escola’,
‘class’ => array( ‘mc-field’, ‘form-row-wide’ ),
‘required’ => true,
), $checkout->get_value( ‘schoolname’ )
);
echo ‘</div>’;
echo ‘</div>’;
}// Validates pupil data
add_action(‘woocommerce_after_checkout_validation’, ‘mc_pupil_checkout_field_process’);
//add_action(‘woocommerce_checkout_process’, ‘mc_pupil_checkout_field_process1’);function mc_pupil_checkout_field_process() {
$url=wc_get_page_permalink( ‘checkout’ );
if ( empty( $_POST[‘pupilname’] ) ) {
wc_add_notice( __( ‘Favor preencher o nome do aluno.’ ), ‘error’ );
wp_redirect($url);
}
if ( empty( $_POST[‘classname’] ) ) {
wc_add_notice(__(‘Favor preencher a turma do aluno.’) , ‘error’ );
wp_redirect($url);
}
if ( empty( $_POST[‘schoolname’] ) ) {
wc_add_notice(__(‘Favor preencher a escola do aluno.’) , ‘error’ );
wp_redirect($url);
}
}//
// Update the order meta with field value
//
add_action( ‘woocommerce_checkout_update_order_meta’, ‘mc_custom_checkout_field_update_order_meta’ );function mc_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST[‘pupilname’] ) ) {
update_post_meta( $order_id, ‘pupilname’, sanitize_text_field( $_POST[‘pupilname’] ) );
}
if ( ! empty( $_POST[‘classname’] ) ) {
update_post_meta( $order_id, ‘classname’, sanitize_text_field( $_POST[‘classname’] ) );
}
if ( ! empty( $_POST[‘schoolname’] ) ) {
update_post_meta( $order_id, ‘schoolname’, sanitize_text_field( $_POST[‘schoolname’] ) );
}
}
//
// Add info to emails
//
add_action( ‘woocommerce_email_after_order_table’, ‘mc_pupilnotes’, 20, 4 );function mc_pupilnotes( $order, $sent_to_admin, $plain_text, $email ){
echo ‘<h3 class=”mc_pupilnotes”>Nome do Aluno: ‘ . get_post_meta( $order->get_id(), ‘pupilname’, true ) . ‘</h3>’;
echo ‘<h3 class=”mc_pupilnotes”>Turma: ‘ . get_post_meta( $order->get_id(), ‘classname’, true ) . ‘</h3>’;
echo ‘<h3 class=”mc_pupilnotes”>Escola: ‘ . get_post_meta( $order->get_id(), ‘schoolname’, true ) . ‘</h3><br>’;
}add_action( ‘woocommerce_thankyou’, ‘mc_pupilnotesthankyou’, 20, 4 );
function mc_pupilnotesthankyou($order_id){
echo ‘<h3 class=”mc_pupilnotes”>Nome do Aluno: ‘ . get_post_meta( $order_id, ‘pupilname’, true ) . ‘</h3>’;
echo ‘<h3 class=”mc_pupilnotes”>Turma: ‘ . get_post_meta( $order_id, ‘classname’, true ) . ‘</h3>’;
echo ‘<h3 class=”mc_pupilnotes”>Escola: ‘ . get_post_meta( $order_id, ‘schoolname’, true ) . ‘</h3><br>’;
}
/**
* Block non-admin from accessing wp-admin
*/
function block_wp_admin_init() {if(is_user_logged_in()) {
if (strpos(strtolower($_SERVER[‘REQUEST_URI’]),’/wp-admin/’) !== false) {if(!current_user_can( ‘manage_options’ ) ) {
wp_redirect( get_option(‘siteurl’), 302 );
exit;
}
}}
}
add_action(‘init’,’block_wp_admin_init’,0);
?>
The page I need help with: [log in to see the link]
- The topic ‘woocommerce_before_order_notes /woocommerce_thankyou’ is closed to new replies.