I am building a WooCommerce based store. I have a list of postcodes, each one has a different shipping cost attached through Shipping Zones (some provide free shipping, some have a flat rate).
When the customer goes to the checkout page, he needs to type his postcode number in the input field. Depending on postcode, an order preview will show different shipping total (free or flat rate).
Here's how the input field looks like in class-wc-countries.php:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
),
);
However, what I want to do is to turn this field into a drop-down menu, so the customer could just select his postcode option rather than type it.
I managed to make it drop-down, but whenever I choose any option it doesn't seem to change shipping total as it would with input field.
Here's what I did:
public function get_default_address_fields() {
$fields = array(
'postcode' => array(
'label' => __( 'Postcode/ZIP', 'woocommerce' ),
'required' => true,
'class' => array( 'form-row-first', 'address-field' ),
'clear' => true,
'validate' => array( 'postcode' ),
'autocomplete' => 'postal-code',
'type' => 'select',
'options' => array(
'opt1' => "001122", "112200", "334400")
),
);
But this don't work.
Am I missing something?
How do I make these drop-down options change shipping total?
Thanks
This will answer very partially to your question, and just show you the way to customize checkout fields.
Overriding core files is not really something to do, as you will loose everithing each time Woocommerce is going to be updated and is not recommended.
To override checkout fields in a clean way, first you need to use a custom function hooked in one of that 2 filter hooks:
woocommerce_default_address_fields (when customizing billing and shipping address default fields)
woocommerce_checkout_fields (when customizing billing or shipping address fields and also others fields).
Related official documentation: Customizing checkout fields using actions and filters
So here I have chose the first hook, and I have corrected your post codes array. You will get that:
Here is that functional and tested code:
add_filter( 'woocommerce_default_address_fields' , 'custom_override_default_postcode_field' );
function custom_override_default_postcode_field( $address_fields ) {
// Your postcodes array
$postcode_array = array(
'opt1' => "001122",
'opt2' => "112200",
'opt3' => "334400"
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
As selecting a post-code is a live front-end event, you will need to use a complex javascript/ajax script with some remote php function to achieve what you want to do, and this is a real development... It also depends on your settings and is complex to handle as there is already some woocommerce ajax scripts operating in that checkout page.
Related
WooCommerce on CheckOut-Page , the default of ['billing_state']shows in drop-down list which I want to change it to input[text]. The customer have to key-in the information by themselves.
please help, thanks a lot
this code doesn't work for me. (after the customer key-in the state, it shows the state-code. It should show the state name that the customer-key-in)
add_filter( 'woocommerce_checkout_fields' ,
'y_change_address_input_type', 10, 1 );
function y_change_address_input_type( $fields ) {
$fields['billing']['billing_state']['type'] = 'text';
return $fields;
}
thank you for your comment, currently, I found the solution. I can switch from selection[option] to input[text] on hook woocommerce_default_address_fields, and the code work as I expected. (I don't know why the code will not work on woocommerce_checkout_fields. The code should have work on both woocommerce_default_address_fields and woocommerce_checkout_fields)
This is the work:
add_filter( 'woocommerce_default_address_fields', 'y_edit_state', 40, 1 );
function y_edit_state( $state_fields ) {
$state_fields ['state'] = array(
'label' => 'stae', // Add custom field label
'placeholder' => 'stae', // Add custom field placeholder
'required' => true, // if field is required or not
'class' => array( 'form-row-wide', 'address-field' ), // add class name
'clear' => false, // add clear or not
'type' => 'text', // add field type
'priority' => 80, // Priority sorting option
);
return $state_fields;
}
In WooCommerce I am adding a custom billing field at the end of the checkout billing fields section, with the code below:
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields($fields)
{
$fields['billing']['billing_options'] = array(
'label' => __('תאריך לידה', 'woocommerce'), // Add custom field label
'placeholder' => _x('תאריך לידה'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'date', // add field type
'class' => array('my-css') // add class name
);
return $fields;
}
How can I add this field after the first name last name field or after company field?
You need to use the "priority" argument, which will allow you to set your field in the correct location (after the first and last name fields).
Normally "billing first name" has a 10 as priority and "billing last name 20 as priority. Then comes the "billing company" that has 30 as priority… So for your custom billing field use a priority of 25 (in between).
There is a little mistake in your code for the placeholder where you should replace _x() function by __().
Your code is going to be:
add_filter('woocommerce_checkout_fields', 'custom_woocommerce_billing_fields');
function custom_woocommerce_billing_fields( $fields )
{
$fields['billing']['billing_options'] = array(
'label' => __('תאריך לידה', 'woocommerce'), // Add custom field label
'placeholder' => __('תאריך לידה', 'woocommerce'), // Add custom field placeholder
'required' => true, // if field is required or not
'clear' => false, // add clear or not
'type' => 'date', // add field type
'class' => array('my-css'), // add class name
'priority' => 25, // Priority sorting option
);
return $fields;
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
If you want this field after the billing company, you will use a priority of 35 instead.
Related: Reordering checkout fields in WooCommerce 3
I have some variable products (different 3-4 variations) with different prices. I want to add some simple products as child products (show as optional checkbox in front). I don't know how to manage that for adding to the cart.
For example let's assume I have a cellphone as main product and when user added it to it's cart it should see 3 sub product (child product) :
Handsfree (in three type of color)
Glass guard (in two type of color )
cell phone shield case ( just black)
as you can understand from above example I wanna to:
add all of this items as one product (includes handsfree, glass guard , cellphone case and cellphone itself ) and calculate it's price .I've read Add a checkbox on single product pages that adds an additional cost in Woocommerce and after that I have codes below to adding options in product edit page.
//custom product tab to woocommerce edit
function sm_wccpd_custom_product_tabs($tabs)
{
$tabs['sm_wcpa_extra_products'] = array(
'label' => __('Samanik Extra Products', 'sm-wcpa'),
'target' => 'sm_wc_extra_products_options',
'class' => array('show_if_variable'),
);
return $tabs;
}
add_filter('woocommerce_product_data_tabs', 'sm_wccpd_custom_product_tabs');
and here is tab content
function extra_products_options_product_tab_content()
{
global $post;
// Note the 'id' attribute needs to match the 'target' parameter set above
?>
<div id='sm_wc_extra_products_options' class='panel woocommerce_options_panel'><?php
?>
<div class='options_group'><?php
woocommerce_wp_checkbox(
[
'id' => '_has_extra_products',
'label' => __('has extra products', 'sm-wcpa'),
]);
woocommerce_wp_select(
[
'class' => 'multiselect attribute_values wc-enhanced-select',
'custom_attributes' => ['multiple' => 'multiple', 'style' => 'width:100% !importan;'],
'id' => '_sm_wcpa_product[]',
'label' => __('Extra Products to be add here', 'sm-wcpa'),
'value' => array_values(json_decode(get_post_meta($post->ID, '_sm_wcpa_product'), true)),
'options' => sm_wcpa_get_products_as_options(),//this is function that I pass Products as array like [product_id => product name]
]);
?></div>
</div><?php
}
// add_filter('woocommerce_product_data_tabs', 'extra_products_options_product_tab_content'); // WC 2.5 and below
add_filter('woocommerce_product_data_panels', 'extra_products_options_product_tab_content'); // WC 2.6 and up
also I can save the data and I think they are not necessary here.
now what I need to do is how to manage this in front. I mean show child products as checkbox, add price to parent price if checked, what about changing variables(those have different prices).
thanks for your patient to read my question.
I am building a new site in woo/ WordPress and a little stuck, basically the client wants to be able to add custom fields to the checkout (done) but they need to be in a repeatable field so the customer can add the field as many times as they need (done) but I am stuck on the actual integration into the database.
My code is at http://pastebin.com/vR4GWV7w
Basically I need to add a custom ID to each repeatable field input e.g.
child_name1
child_name2
If I was doing this in a template file I would use PHP count but this needs to work in the code below which creates the field.
function my_custom_checkout_field( $checkout ) {
echo '<div id="entry1" class="clonedInput"><h2>' . __('Child Details') . '</h2>';
woocommerce_form_field( 'child_name', array(
'type' => 'text',
'class' => array('child_field form-row-wide'),
'label' => __('Child Name'),
'placeholder' => __('Enter the child\'s name'),
), $checkout->get_value( 'child_name' ));
}
Any help / guidance would be much appreciated, I posted this on the WordPress exchange and got put on hold so trying here as it's a PHP question
I am setting up magento store which buy stuff from customers. Instead of decreasing Price of the product, I want to increase the price of the product using coupon code. in which file i need to make changes for this.
Update 1:
Thanks Amit.
I have another question. I like to change "Discount" to "Promotion" in cart and onepage checkout. However, I can't find any file location. I have turn on the Template Path Hints from Configuration. Can anyone help me out ?
If you need only fixed amount discount, then you can remove the validate for Discount Amount field so that you can add negative value in this field, so when you try to apply this coupon it will automatically add that amount instead to decrease. So you need to override below two classes.
For more details on Magento override see this Link.
Mage_Adminhtml_Block_Promo_Quote_Edit_Tab_Actions
find this code
$fieldset->addField('discount_amount', 'text', array(
'name' => 'discount_amount',
'required' => true,
'class' => 'validate-not-negative-number',
'label' => Mage::helper('salesrule')->__('Discount Amount'),
));
and change it to
$fieldset->addField('discount_amount', 'text', array(
'name' => 'discount_amount',
'required' => true,
'label' => Mage::helper('salesrule')->__('Discount Amount'),
));
and remove the below code
if ($this->hasDiscountAmount()) {
if ((int)$this->getDiscountAmount() < 0) {
Mage::throwException(Mage::helper('rule')->__('Invalid discount amount.'));
}
}
from
Mage_Rule_Model_Abstract::_beforeSave()
Have a look at the CartController.php in Mage/Checkout/Controllers and at the Mage_Sales_Model_Quote model with its methode -collectTotals().
You need to create a new module where you override the model or create an observer.