In Wordpress Woocommerce checkout page I want to make field 'billing_name' not required if checkbox 'buy_on_company' is checked.
I have managed to hide 'billing_name' in checkout page, but how to make it not required also?
add_filter( 'woocommerce_checkout_fields' , 'company_checkbox_and_new_checkout_fields', 9999 );
function company_checkbox_and_new_checkout_fields( $fields ) {
$fields['billing']['buy_on_company'] = array( // CSS ID
'type' => 'checkbox',
'label' => __('Buy on company', 'woocommerce'),
'class' => array('form-row-wide'), // CSS Class
//'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox checkbox'),
'clear' => true,
'priority' => '10'
);
$fields['billing']['billing_name']['placeholder'] = 'First Name Last name';
return $fields;
}
function hook_javascript() {
?>
<script>
document.getElementById('buy_on_company').onclick = function () {
if (this.checked) {
document.getElementById('billing_name').style['display'] = 'none';
} else {
document.getElementById('billing_name').style['display'] = 'block';
}
};
</script>
<?php
}
add_action( 'woocommerce_after_checkout_form', 'hook_javascript' );
I have done this:
add_filter( 'woocommerce_checkout_fields' , 'company_checkbox_and_new_checkout_fields_1', 9999 );
function company_checkbox_and_new_checkout_fields_1( $fields ) {
if (isset($_POST['buy_on_company'])) {
$fields['billing']['billing_name']['required'] = false;
} else {
$fields['billing']['billing_name']['required'] = true;
}
return $fields;
}
Now it doesn't ask for billing name field.
Related
I am really stuck and have tried every email hook under the sun. I used an outdated PHP code developed by someone else and revised it for new woocommerce hooks (as the code is 4years old). Everything works perfectly but i need the customer field "billing_vat" to appear in the new order admin email. I will share my code and any help will be greatly appreciated please!!
Thanks
//create vat number billing field
add_filter('woocommerce_billing_fields' , 'display_billing_vat_fields');
function display_billing_vat_fields($billing_fields){
$billing_fields['billing_vat'] = array(
'type' => 'text',
'label' => __('VAT number', 'woocommerce' ),
'class' => array('form-row-wide'),
'required' => false,
'clear' => true,
'priority' => 30, // To change the field location increase or decrease this value
);
return $billing_fields;
}
// Printing the Billing Address on My Account
add_filter( 'woocommerce_my_account_my_address_formatted_address',
'custom_my_account_my_address_formatted_address', 10, 3 );
function custom_my_account_my_address_formatted_address( $fields, $customer_id, $type ) {
if ( $type == 'billing' ) {
$fields['vat'] = get_user_meta( $customer_id, 'billing_vat', true );
}
return $fields;
}
// Checkout -- Order Received (printed after having completed checkout)
add_filter( 'woocommerce_order_formatted_billing_address',
'custom_add_vat_formatted_billing_address', 10, 2 );
function custom_add_vat_formatted_billing_address( $fields, $order ) {
$fields['vat'] = $order->get_meta('billing_vat');
return $fields;
}
// Creating merger VAT variables for printing formatting
add_filter( 'woocommerce_formatted_address_replacements',
'custom_formatted_address_replacements', 10, 2 );
function custom_formatted_address_replacements( $replacements, $args ) {
$replacements['{vat}'] = ! empty($args['vat']) ? $args['vat'] : '';
$replacements['{vat_upper}'] = ! empty($args['vat']) ? strtoupper($args['vat']) : '';
return $replacements;
}
//Defining the Spanish formatting to print the address, including VAT.
add_filter( 'woocommerce_localisation_address_formats', 'custom_localisation_address_format' );
function custom_localisation_address_format( $formats ) {
foreach($formats as $country => $string_address ) {
$formats[$country] = str_replace('{company}\n', '{company}\n{vat_upper}\n', $string_address);
}
return $formats;
}
add_filter( 'woocommerce_customer_meta_fields', 'custom_customer_meta_fields' );
function custom_customer_meta_fields( $fields ) {
$fields['billing']['fields']['billing_vat'] = array(
'label' => __( 'VAT number', 'woocommerce' )
);
return $fields;
}
add_filter( 'woocommerce_admin_billing_fields', 'custom_admin_billing_fields' );
function custom_admin_billing_fields( $fields ) {
$fields['vat'] = array(
'label' => __( 'VAT number', 'woocommerce' ),
'show' => true
);
return $fields;
}
add_filter( 'woocommerce_found_customer_details', 'custom_found_customer_details' );
function custom_found_customer_details( $customer_data ) {
$customer_data['billing_vat'] = get_user_meta( $_POST['user_id'], 'billing_vat', true );
return $customer_data;
}
add_filter('woocommerce_email_order_meta_fields', 'supine_add_email_order_meta_fields', 10, 3 );
function supine_add_email_order_meta_fields( $fields, $sent_to_admin, $order_obj ) {
$billing = get_post_meta( $order_obj->get_order_number(), 'billing_vat', true );
return $fields;
}
Shortly after posting this, i found a fix for it. I had to define the new key "billing_vat" so that it posted with other user information. All the code above is correct except the last add_filter hook. Replace that with the following code, and you have added an optional vat_number field to your checkout, admin backend as well as order emails. Here's the code snippet to replace last "add_filter" hook:
// VAT Number in emails
add_filter( 'woocommerce_email_order_meta_keys',
'supine_vat_number_display_email' );
function supine_vat_number_display_email( $keys ) {
$keys['VAT Number'] = '_billing_vat';
return $keys;
}
code goes in functions.php of your active child theme. Tested and working.
I have a problem autocomplete the bootstrap modal. when I enter the keyword search for the book, but only black dots appear. any solution to this?
things that happen as you can see in the image, I've tried adding CSS z-index but the results are still the same.
public function CheckingBook()
{
if ($this->input->is_ajax_request()) {
if ( isset($_GET['term']) ) {
$result = $this->book_model->GetBookByTitle($_GET['term']);
if (count($result) > 0) {
foreach ($result as $row) {
$data[] = array(
'judul_buku' => $row['judul_buku'],
'isbn_buku' => $row['isbn_buku'],
'kode_buku' => $row['kode_buku']
);
}
} else {
$data = [
'msg' => 'error',
'gagal' => 'Tidak ada data yang ditemukan'
];
}
} else {
$data = [
'msg' => 'error',
'gagal' => 'Tidak ada data yang di inputkan'
];
}
echo json_encode($data);
} else {
$this->session->set_flashdata('error', 'Tidak punya akses langsung kehalaman tersebut');
redirect(base_url('adminpage/book'),'refresh');
}
}
this is jquery autocomplete
$('#bookTitleAdd').autocomplete({
source: '/perpustakaan/adminpage/book/CheckingBook',
select : function (event,ui) {
$('#bookTitleAdd').val(ui.item.judul_buku)
$('#bookISBNAdd').val(ui.item.isbn_buku)
}
});
<input type="text" name="judulBuku" class="form-control" id="bookTitleAdd" placeholder="Judul Buku" autocomplete="off">
I try to look at the xhr network and then see the data objects in the array
I had the same problem. It's because of z-index. Boostrap modal has z-index=1050. jquery-ui menu has z-index=100.
You can use this:
.ui-menu.ui-autocomplete.ui-front {
z-index: 1100;
}
$data[] = array(
'label' => $row['judul_buku'], //solved this
'judul_buku' => $row['judul_buku'],
'isbn_buku' => $row['isbn_buku'],
'kode_buku' => $row['kode_buku']
);
I am trying to connect to an api using the code below, so when the customer clicks on the "place order" button on the Woocommerce checkout page, I am getting a "please try again" error:
var amount = <?php global $woocommerce; print WC()->cart->total; ?>;
var merchantOrderId = '<?php echo print time(); ?>';
var apiKey = 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t';
renderMMoneyPaymentButton(amount, merchantOrderId, apiKey);
I am trying to pass this information to the api via this function but I am not getting a successful connection.
public function process_payment( $order_id ) {
global $woocommerce;
// we need it to get any order detailes
$order = new WC_Order($order_id);
/*
* Array with parameters for API interaction
*/
$args = array(
'amount' => '<?php global $woocommerce; print WC()->cart->total; ?>',
'merchant_order_id' => '<?php print time(); ?>',
'api_Key' => 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t',
'currency' => 'BBD',
);
/*
* Your API interaction could be built with wp_remote_post()
*/
$response = wp_remote_post( 'https://api.mmoneybb.com/merchant/js/mmoney-payment.js', $args );
if( !is_wp_error( $response ) ) {
$body = json_decode( $response['body'], true );
// it could be different depending on your payment processor
if ( $body ['$response'] == 'APPROVED') {
// we received the payment
$order->payment_complete();
$order->reduce_order_stock();
// some notes to customer (replace true with false to make it private)
$order->add_order_note( 'Thanks for your payment!!!!', true );
// Empty cart
$woocommerce->cart->empty_cart();
// Redirect to the thank you page
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
} else {
wc_add_notice( 'Please try again.', 'error' );
return;
}
} else {
wc_add_notice( 'Connection error.', 'error' );
return;
}
}
let me know what i am doing wrong much appreciated also this is the other script as well
function renderMMoneyPaymentButton(amount, merchantOrderId, apiKey) {
let paymentParams = {
amount: amount,
api_key: apiKey,
currency: 'BBD',
merchant_order_id: merchantOrderId,
onCancel: function () { console.log('Modal closed'); },
onError: function(error) { console.log('Error', error); },
onPaid: function (invoice) { console.log('Payment complete', invoice); }
};
// "mMoney" window global provided by sourcing mmoney-payment.js script.
// Attach the button to the empty element.
mMoney.payment.button.render(paymentParams, '#mmoney-payment-button');
}
1) In your first snippet code you are using javascript and you need to get the order Id and then the order total… You can only get the Order ID after the order is placed…
There is an answer example here.
2) Your 2nd public function involves only PHP… There are some errors and mistakes in this code. Try the following revisited code instead:
public function process_payment( $order_id ) {
// Get The WC_Order Object instance
$order = wc_get_order( $order_id );
/*
* Array with parameters for API interaction
*/
$args = array(
'amount' => $order->get_total(),
'merchant_order_id' => $order_id,
'api_Key' => 'm85BXXLpf_icrSvqbElR11xquEgmKZ8wfeRb2ly3-G7pIwCKDuytgplB7AQGi-5t',
'currency' => $order->get_currency(),
);
/*
* Your API interaction could be built with wp_remote_post()
*/
$response = wp_remote_post( 'https://api.mmoneybb.com/merchant/js/mmoney-payment.js', $args );
if( !is_wp_error( $response ) ) {
$body = json_decode( $response['body'], true );
// it could be different depending on your payment processor
if ( $body ['$response'] == 'APPROVED') {
// we received the payment
$order->payment_complete();
$order->reduce_order_stock();
// some notes to customer (replace true with false to make it private)
$order->add_order_note( 'Thanks for your payment!!!!', true );
// Empty cart
$woocommerce->cart->empty_cart();
// Redirect to the thank you page
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
} else {
wc_add_notice( 'Please try again.', 'error' );
return;
}
} else {
wc_add_notice( 'Connection error.', 'error' );
return;
}
}
It should better work.
I have one selection form in my application..in that i have four fields one is selection,comments,results,and two radio buttons with selected and selected..
So what I want to do is when person clicked on the selected radio button I need to send selected mail..if person selected not selected radio button need to send mail not selected mail..
My Controller code :
public function requirement()
{
$this->load->model('RequirementModel');
$data['user'] = $this->RequirementModel->getusers();
$this->load->view('Requirements/requirements', $data);
$insert = array(
'role_name' => $this->input->post('role_name'),
'vacancies' => $this->input->post('vacancies'),
'experience' => $this->input->post('experience'),
'jd' => $this->input->post('jd'),
'hiring_contact_name' => $this->input->post('hiring_contact_name'),
'hiring_contact_number' => $this->input->post('hiring_contact_number'),
'user_id' => implode(',', $this->input->post('user_id')),
);
$this->RequirementModel->add_requirement($insert);
$all_users = $this->input->post('user_id');
foreach ($all_users as $key) {
$get_email = $this->RequirementModel->get_user_email_by_id($key);
$role_name = $this->input->post('role_name');
$vacancies = $this->input->post('vacancies');
$experience = $this->input->post('experience');
$jd = $this->input->post('jd');
$hiring_contact_name = $this->input->post('hiring_contact_name');
$hiring_contact_number = $this->input->post('hiring_contact_number');
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://md-in-42.webhostbox.net',
'smtp_port' => 465,
'smtp_user' => 'test3#clozloop.com',
'smtp_pass' => 'test3',
);
$this->load->library('email', $config);
$this->email->set_mailtype("html");
$this->email->from('test3#clozloop.com', 'bharathi');
$this->email->to($get_email);
$this->email->subject('this is our requirements pls go through it');
$link = 'Click on this link - Click Here';
$this->email->message($link);
// $this->email->message("Dear,\n\nRole Name:$role_name, Vacancies:$vacancies,experience:$experience,jd:$jd,hiring_contact_number:$hiring_contact_number,hiring_contact_name:$hiring_contact_name.\n\nThanks, \nGlobaalign.");
//$this->email->send();
//print_r($get_email);
if ($this->email->send()) {
echo "email sent";
} else {
echo "email failed";
}
}
please help me how to do this..
Thank you
Here is the solution:
if($data['selection_process']==1)
{
//send selected mail
}
else
{
//send not selected mail
}
I'm newbie with Yii.
I have a CGridview with a cutom dataprovider which takes a parameter $select:
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'beneficiary-grid',
'dataProvider' => $model->searchForVoucherAssignment($select),
'filter' => $model,
'columns' => array(
'id',
'registration_code',
'ar_name',
'en_name',
'family_member',
'main_income_source',
'combine_household',
array( 'class'=>'CCheckBoxColumn', 'value'=>'$data->id', 'selectableRows'=> '2', 'header' => 'check',
),
),
));
That parameter $select takes its values from dropdownlist:
$data = CHtml::listData(Distribution::model()->findAll(array("condition"=>"status_id = 2")), 'id', 'code');
$select = key($data);
echo CHtml::dropDownList(
'distribution_id',
$select, // selected item from the $data
$data,
array(
)
);
So I defined a script to update the CGridview depending on the value of dropdownlist
Yii::app()->clientScript->registerScript('sel_status', "
$('#selStatus').change(function() {
$.fn.yiiGridView.update('beneficiary-grid', {
data: $(this).serialize()
});
return false;
});
");
My model:
public function searchForVoucherAssignment ($distribution_id = 0) {
$criteria = new CDbCriteria;
if ($distribution_id != 0) {
$criteria->condition = "Custom Query...!!";
}
$criteria->compare('id', $this->id);
//Custom Criteria
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => 20,
),
));
}
The problem is that the CGridview isn't changing where a value of the dropdownlist changed...
I think you have selected the wrong Id for the change event. The Id should be
$('#distribution_id').change(function() {