I want to add a conditional requirement on the Checkout page. Because a lot of people fill the Name of Company without they are a company. And I want to add to the "billing_company" field a conditional where if the customer fill the "billing_company" field need to be fill the "woocommerce_eu_vat_number" field too. The problem is those two fields are optionals. But I want to do them as required if billing_company /billing_vat_number is filled. Is it possible?
Field Codes in HTML:
<p class="form-row form-row-wide" id="billing_company_field" data-priority="30">
<label for="billing_company" class="">Company name <span class="optional">(optional)</span></label>
<span class="woocommerce-input-wrapper czr-focus">
<input type="text" class="input-text czr-focusable" name="billing_company" id="billing_company" placeholder="" value="" autocomplete="organization">
</span>
</p>
<p class="form-row form-row-wide" id="woocommerce_eu_vat_number_field" data-priority="120">
<label for="woocommerce_eu_vat_number" class="">VAT number (companies) <span class="optional">(optional)</span></label>
<span class="woocommerce-input-wrapper czr-focus">
<input type="text" class="input-text czr-focusable" name="billing_vat_number" id="woocommerce_eu_vat_number" placeholder="" value="">
</span>
</p>
I want a code something like this. Sorry because I can understand more or less php code but I don't know how to create it from zero. T_T
<?php
function custom_required_vat_billing_fields() {
if( !empty ('billing_company' ) {
set ($fields['billing']['billing_vat_number'] = class [REQUIRED=true] );)
} if not {
wc_add_notice(__('Please enter a VAT number if you are a company'), 'error');
}
};
return $fields;
}
add_filter('woocommerce_checkout_fields','custom_required_vat_billing_fields');
?>
If checkbox is checked, then field company ID is required. Write code below to your child themes functions.php file and change ID of elements:
wi_as_company is ID of checkbox and
billing_company_wi_id is ID of required field if checkbox is checked
add_action( 'woocommerce_checkout_process', 'afm_validation' );
function afm_validation() {
if ( isset($_POST['wi_as_company']) && isset($_POST['billing_company_wi_id']) && empty($_POST['billing_company_wi_id']) ) {
wc_add_notice( __("Please fill company ID"), "error" );
}
}
Related
So I was able to limit the zip code length limit to 5 on the user's shipping/billing info on their account page as well as the checkout page using this code in my functions.php:
function my_wc_custom_billing_fields( $fields ) {
$fields['billing_postcode']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_billing_fields', 'my_wc_custom_billing_fields' );
function my_wc_custom_shipping_fields( $fields ) {
$fields['shipping_postcode']['maxlength'] = 5;
return $fields;
}
add_filter( 'woocommerce_shipping_fields', 'my_wc_custom_shipping_fields' );
However, I still am able to enter past the limit in the zip code box on the CART page. You are able to change your address on the cart page and if you click "go to checkout" after typing more than 5 digits, it will allow the form to fill in with those additional digits on the checkout page. I want the zip code box on the CART page to work the same as it does on the other pages.
Thank you for taking to the time to read this. I hope to hear from someone soon!
------- >EDIT: FIGURED IT OUT!
Instead of creating a filter inside my functions.php, I directly edited the woocommerce/templates/cart/shipping-calculator.php file.
In this piece of code:
<?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ) : ?>
<p class="form-row form-row-wide" id="calc_shipping_postcode_field">
<input type="text" class="input-text" value="<?php echo esc_attr( WC()->customer->get_shipping_postcode() ); ?>" placeholder="<?php esc_attr_e( 'Postcode / ZIP', 'woocommerce' ); ?>" name="calc_shipping_postcode" id="calc_shipping_postcode" />
</p>
<?php endif; ?>
I simply added "maxlength="5" in the line.
<input type="text" class="input-text" maxlength="5" value="
I hope this helps someone along the way! :)
I have built an e-commerce site using Woocommerce. I would like to add two more check boxes below the terms and conditions. I have searched everywhere for a working solution and the only thing that I found is a commercial plugin.
How to add custom checkout fields (2 checkboxes) below the terms and conditions programmatically?
Location of the terms and condition screenshot:
The 1st hooked function displays the 2 additional checkout fields
The 2nd hooked function will check that both checkboxes are "selected" to allow checkout, displaying a custom error notice if not…
The code:
add_action('woocommerce_checkout_before_terms_and_conditions', 'checkout_additional_checkboxes');
function checkout_additional_checkboxes( ){
$checkbox1_text = __( "My first checkbox text", "woocommerce" );
$checkbox2_text = __( "My Second checkbox text", "woocommerce" );
?>
<p class="form-row custom-checkboxes">
<label class="woocommerce-form__label checkbox custom-one">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_one" > <span><?php echo $checkbox1_text; ?></span> <span class="required">*</span>
</label>
<label class="woocommerce-form__label checkbox custom-two">
<input type="checkbox" class="woocommerce-form__input woocommerce-form__input-checkbox input-checkbox" name="custom_two" > <span><?php echo $checkbox2_text; ?></span> <span class="required">*</span>
</label>
</p>
<?php
}
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['custom_one'] )
wc_add_notice( __( 'You must accept "My first checkbox".' ), 'error' );
if ( ! $_POST['custom_two'] )
wc_add_notice( __( 'You must accept "My second checkbox".' ), 'error' );
}
Code goes in function.php file of your active child theme (active theme).
Tested and works.
I want to do the same as this guy do
Magento: How to display customer's phone number on customer information field
but I don't want only to display the customer phone number, I want to give ability to change the telephone number on this page.
If I follow the guy's above solution I only get the Telephone display in a field but when I submit the form everything else is being saved except this telephone field. What could be the code solution to save the new information after FormSubmission?
Thanks in advance
UPDATE #1
I update /app/design/frontend/base/default/template/customer/form/edit.phtml with this code
<input type="text" placeholder="<?php echo $this->__('Telephone') ?>" name="telephone" value="<?php echo $this->getCustomer()->getPrimaryBillingAddress()->getTelephone() ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" id="telephone" />
but it doesn't save (form omit changes) if I change the input value and submit the form.
I wonder why..
I have added two fields [ Start_date and End_Date] in Project , can able to update and save values in the fields.
I want to allow fields editable when EDIT button clicked otherwise it should be readonly.
Code changes but doesn't works
<label for="txtstartdate"><?php echo __("Start Date")?> <span class="required">*</span></label>
<input id="txtstartdate" name="txtstartdate" type="text" class="formInputText" disabled="disabled" value="<?php echo $project->getstartDate()?>" disabled />
<br class="clear"/>
function disableWidgets(){
$('#addProject_start_date').attr('disabled','disabled');
$('#addProject_end_date').attr('disabled','disabled');
}
function enableWidgets(){
$('#addProject_start_date').removeAttr('disabled');
$('#addProject_end_date').removeAttr('disabled');
}
In which file i have to change . Any one could help me to achieve this?
In the billing_fields.phtml there is
<?php
$billingFields['country_id'] = '
<div class="input-box input-country'.((in_array('country', $formErrors)) ? ' input-error' : '').'">
<label for="billing:country_id">'.$this->__('Country').' <span class="required">*</span></label><br />
'.$this->getCountryHtmlSelect('billing').'
</div>';
?>
I do have only one country and the select-tag $this->getCountryHtmlSelect('billing') with the rendered dropdown is not user friendly as it has no further options. I feel its misleading and obsolete. My question is:
How do I have to change the code above to show my default country in a simple (not editable) input field?
EDIT
I came up with this after CBroes getCountryCollection hint
<?php
$countryCollection = $this->getCountryCollection();
foreach($countryCollection as $country) {
$country_id = $country['country_id'];
$countryName = Mage::getModel('directory/country')->load($country_id)->getName();
$billingFields['country_id'] = '
<div class="input-box input-country'.((in_array('country', $formErrors)) ? ' input-error' : '').'">
<label for="billing:country_id">'.$this->__('Country').' </label><br />
<input type="text" name="billing[country_id]" id="billing:country_id" class="input-text" value="'.$countryName.'" style="width: 83%" readonly="readonly" />
</div>';
}
?>
The frontend looks fine. Even the backend recognizes the given value. Unfortunately the emails don't. The country field remains empty there.
What am I missing or how do I make this input legit so that magento-emails accept its value?