Change postcode field in Cart page ( Shipping Calculator ) to a dropdown - php

I would like to make a dropdown field when somebody tries to type post code in the shipping calculator, at cart page, so he can choose the field, rather than type it.
I have managed to make it at the checkout field, looking at how other people did it at StackOverflow.
This is the code I have put in function.php and it works at checkout:
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(
'70101' => "70101",
'70202' => "70202",
'70220' => "70220",
'70223' => "70223",
'89245' => "89245",
'89247' => "89247"
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
Then I have put shipping-calculator.php in my theme woocommerce cart php file and I can not figure out how to make a dropdown field at Cart.
This is the code for typing in postcode from woocommerce an shipping-calculator.php:
<?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; ?>
How do I make it to be a dropdown, please?

<?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_postcode', true ) ) : ?>
<p class="form-row form-row-wide" id="calc_shipping_postcode_field">
<select name="calc_shipping_postcode" id="calc_shipping_postcode">
<option value=""><?php _e( 'PostCode;', 'woocommerce' ); ?></option>
<?php
$postcode_array = array(
'70101' => "70101",
'70202' => "70202",
'70220' => "70220",
'70223' => "70223",
'89245' => "89245",
'89247' => "89247"
);
foreach ( $postcode_array as $key => $value ) {
?>
<option value="<?php echo $key; ?>" <?php selected( esc_attr( WC()->customer->get_shipping_postcode() ), $key, 1 ) ?>><?php echo $value; ?></option>
<?php
}
?>
</select>
</p>
<?php endif; ?>
Modify this section in template override

Related

Overriding woocommerce shipping calculator in child theme

I have overridden the woocommerce shipping calculator and it works. However, when I change countries, the 'State' input field appears when I have set that as a hidden field. If I then change back to my home country, Australia, the State field remains, but becomes a dropdown and is populated with the States of Australia. This field is part of the original theme's shipping-calculator.php, not my child theme one.
I'm not sure how this is happening since my child theme works fine until I change countries. I am trying to include the use of the AddressFinder widget into my cart shipping calculator when the customer is from Australia, and not use it when the customer is from another country.
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_before_shipping_calculator' ); ?>
<script>
(function(){
var cartwidget, initAF = function(){
cartwidget = new AddressFinder.Widget(
document.getElementById('address_field'),
'<AddressFinder Key Here>',
'AU',
{
"address_params": {}
}
);
cartwidget.on("result:select", function(fullAddress, metaData) {
var combinedAddressLine1And2;
if ( metaData.address_line_2 ) {
combinedAddressLine1And2 = metaData.address_line_1 + ', ' + metaData.address_line_2
} else {
combinedAddressLine1And2 = metaData.address_line_1
}
if ( document.getElementById("calc_shipping_country").value == "Australia" ) {
document.getElementById('calc_shipping_address_1').value = combinedAddressLine1And2;
document.getElementById('calc_shipping_state').value = metaData.state_territory;
document.getElementById('calc_shipping_postcode').value = metaData.postcode;
} else {
document.getElementById('calc_shipping_address_1').value = '';
document.getElementById('calc_shipping_state').value = '';
document.getElementById('calc_shipping_postcode').value = '';
}
//window.alert(document.getElementById('calc_shipping_address_1').value);
document.getElementById('calc_shipping_city').value = metaData.locality_name;
});
};
function downloadAF(f){
var script = document.createElement('script');
script.src = 'https://api.addressfinder.io/assets/v3/widget.js';
script.async = true;
script.onload=f;
document.body.appendChild(script);
};
document.addEventListener("DOMContentLoaded", function () {
downloadAF(initAF);
});
})();
function country_updated() {
if ( document.getElementById("calc_shipping_country").value == "Australia") {
document.getElementById("address_field").type = 'text';
document.getElementById("calc_shipping_city").type = 'hidden';
} else {
document.getElementById("calc_shipping_city").type = 'text';
document.getElementById("address_field").type = 'hidden';
}
}
</script>
<form class="woocommerce-shipping-calculator" action="<?php echo esc_url( wc_get_cart_url() ); ?>" method="post">
<?php printf( '%s', esc_html( ! empty( $button_text ) ? $button_text : __( 'Calculate shipping', 'woocommerce' ) ) ); ?>
<section class="shipping-calculator-form" style="display:none;">
<?php if ( apply_filters( 'woocommerce_shipping_calculator_enable_country', true ) ) : ?>
<p class="form-row form-row-wide" id="calc_shipping_country_field">
<select name="calc_shipping_country" id="calc_shipping_country" class="country_to_state country_select" onchange="country_updated()">
<option value=""><?php esc_html_e( 'Select a country…', 'woocommerce' ); ?></option>
<?php
foreach ( WC()->countries->get_shipping_countries() as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '"' . selected( WC()->customer->get_shipping_country(), esc_attr( $key ), false ) . '>' . esc_html( $value ) . '</option>';
}
?>
</select>
</p>
<?php endif; ?>
<p class="form-row form-row-wide" id="shipping_address_field">
<input type="text" placeholder="<?php esc_attr_e( 'Enter Address Here', 'woocommerce' ); ?>" id="address_field" name="address_field" placeholder="Enter address here" class="address-search">
</p>
<p class="form-row form-row-wide" id="calc_shipping_address_field_1">
<input type="hidden" name="calc_shipping_address_1" id="calc_shipping_address_1" />
</p>
<p class="form-row form-row-wide" id="calc_shipping_city_field">
<input type="hidden" name="calc_shipping_city" id="calc_shipping_city" />
</p>
<p class="form-row form-row-wide" id="calc_shipping_state_field">
<input type="hidden" name="calc_shipping_state" id="calc_shipping_state" />
</p>
<p class="form-row form-row-wide" id="calc_shipping_postcode_field">
<input type="hidden" name="calc_shipping_postcode" id="calc_shipping_postcode" />
</p>
<p><button type="submit" name="calc_shipping" value="1" class="button"><?php esc_html_e( 'Update', 'woocommerce' ); ?></button></p>
<?php wp_nonce_field( 'woocommerce-shipping-calculator', 'woocommerce-shipping-calculator-nonce' ); ?>
</section>
</form>
<?php do_action( 'woocommerce_after_shipping_calculator' ); ?>
When the customer is from Australia - it should display the country select input and an address input box - which has the AddressFinder widget on it. When the customer is from another country, it should display the country select and the city input field. Instead, when another country is selected, the state field shows as well. And when changing back to Australia, the State field remains.
I figured out the issue. In the 'Country' select, I had left it's class as: class="country_to_state country_select" - so this was obviously populating and calling the State input field. I also realised my javascript should have been checking the country select value for "AU" not "Australia".

Add custom dimension fields to each variation settings for variable products

I'm trying to add a "Built Dimensions" fields to each product variation settings.
Here's a mock of what I'm trying to accomplish:
I've followed these following tips but they aren't doing quite what I want:
http://www.remicorson.com/mastering-woocommerce-products-custom-fields/
Add Advanced Custom Fields to WooCommerce Product Variation
Those are adding it to one of the other data tabs. I need it per variation. Each variation has a built dimension and a shipping dimension.
With the 2 hooked functions below you will get exactly what you are expecting like in your mock:
// Add variation custom "dimentions" fields
add_action( 'woocommerce_variation_options_dimensions','add_variation_options_built_dimensions', 10, 3 );
function add_variation_options_built_dimensions( $loop, $variation_data, $variation ){
$variation_built_lenght = get_post_meta($variation->ID,"_built_lenght", true );
if( ! $variation_built_lenght ) $variation_built_lenght = "";
$variation_built_width = get_post_meta($variation->ID,"_built_width", true );
if( ! $variation_built_width ) $variation_built_width = "";
$variation_built_height = get_post_meta($variation->ID,"_built_height", true );
if( ! $variation_built_height ) $variation_built_height = "";
?>
<p class="form-field form-row dimensions_field built_dimensions hide_if_variation_virtual form-row-last">
<label for="product_built_length"><?php
// translators: %s: dimension unit
printf(
__( 'Built dimensions (L×W×H) (%s)', 'woocommerce' ),
get_option( 'woocommerce_dimension_unit' )
);
?></label>
<?php echo wc_help_tip( __( 'Built length x width x height in decimal form', 'woocommerce' ) ); ?>
<span class="wrap">
<input id="product_built_length" placeholder="<?php esc_attr_e( 'Built length', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_lenght_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_lenght ); ?>" />
<input placeholder="<?php esc_attr_e( 'Built width', 'woocommerce' ); ?>" class="input-text wc_input_decimal" size="6" type="text" name="built_width_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_width ); ?>" />
<input placeholder="<?php esc_attr_e( 'Built height', 'woocommerce' ); ?>" class="input-text wc_input_decimal last" size="6" type="text" name="built_height_<?php echo $loop; ?>" value="<?php echo esc_attr( $variation_built_height ); ?>" />
</span>
</p>
<?php
}
//Save variation custom "dimentions" fields
add_action( 'woocommerce_save_product_variation','save_variation_options_built_dimensions', 10 ,2 );
function save_variation_options_built_dimensions( $variation_id, $loop ){
$built_lenght = $_POST["built_lenght_$loop"];
if(!empty($built_lenght))
update_post_meta( $variation_id, '_built_lenght', sanitize_text_field($built_lenght) );
$built_width = $_POST["built_width_$loop"];
if(!empty($built_width))
update_post_meta( $variation_id, '_built_width', sanitize_text_field($built_width) );
$built_height = $_POST["built_height_$loop"];
if(!empty($built_height))
update_post_meta( $variation_id, '_built_height', sanitize_text_field($built_height) );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for WooCommerce 2.6.x and 3+.
You will get this:

How to get Text Area in WordPress posts section?

I need to change this code to implement "More info" field as a text field in my WordPress post section.
The "More info" field looks like this:
I use smartmetabox. It has 2 files:
textarea.php:
<textarea name="<?php echo $id?>" id="<?php echo $id?>" rows="5" cols="100" class="custom"><?php echo $value?></textarea>
text.php:
<input type="text" name="<?php echo $id?>" id="<?php echo $id?>" value="<?php echo $value?>" class="regular-text" />
And my_file.php code is:
<?php
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', __('Information', 'addict'), 'cd_meta_box_cb', 'post', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$creteria_1_text = isset( $values['creteria_1_text'] ) ? esc_attr( $values['creteria_1_text'][0] ) : '';
$check = isset( $values['my_meta_box_check'] ) ? esc_attr( $values['my_meta_box_check'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="creteria_1_text"><b><?php _e("More info, 'addict') ?></b></label>
<input style="width:85%" type="text" name="creteria_1_text" id="creteria_1_text" value="<?php echo $creteria_1_text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $post_id )
{
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['creteria_1_text'] ) )
update_post_meta( $post_id, 'creteria_1_text', wp_kses( $_POST['creteria_1_text'], $allowed ) );
}
// function for show rating content
//$key_1_value = get_post_meta($post->ID, 'my_meta_box_text', true);
?>
After some digging i found that i should replace this code:
<input style="width:85%" type="text" name="creteria_1_text" id="creteria_1_text" value="<?php echo $creteria_1_text; ?>
To:
<textarea name="creteria_1_text" id="creteria_1_text" rows="5" cols="100" class="custom"><?php echo $creteria_1_text; ?></textarea>

Get the value from a custom field in category

I made a custom field in admin category interface named custom order. It is showing in the admin. But now I am having difficulties in getting the information from the field and echo it in index.php.
If I run
$thisCat = get_category( 29);
print_r($thisCat);
I don't get any information from the custom field.
echo get_post_custom_values('category_custom_order', 29); doesn't echo anything.
How should I get the value from category_custom_order?
Here is my code for custom field in functions.php:
<?php
/** Add Custom Field To Category Form */
add_action( 'category_add_form_fields', 'category_form_custom_field_add', 10 );
add_action( 'category_edit_form_fields', 'category_form_custom_field_edit', 10, 2 );
function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
<label for="category_custom_order">Custom Order</label>
<input name="category_custom_order" id="category_custom_order" type="text" value="" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</div>
<?php
}
function category_form_custom_field_edit( $tag, $taxonomy ) {
$option_name = 'category_custom_order_' . $tag->term_id;
$category_custom_order = get_option( $option_name );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
<td>
<input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : ''; ?>" size="40" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
add_action( 'created_category', 'category_form_custom_field_save', 10, 2 );
add_action( 'edited_category', 'category_form_custom_field_save', 10, 2 );
function category_form_custom_field_save( $term_id, $tt_id ) {
if ( isset( $_POST['category_custom_order'] ) ) {
$option_name = 'category_custom_order_' . $term_id;
update_option( $option_name, $_POST['category_custom_order'] );
}
}
You should be able to get it this way, and by the way it's not a custom field but an option.
$category_id = 29;
$category_custom_order = get_option( 'category_custom_order_' . $category_id );
echo $category_custom_order;

Trying to send a hidden input value to next page

I am trying to tweak Wordpress Jigoshop according to my customer's needs and I got a bit stuck. What I need is: when a product variation is selected, some additional options appear in the form of radio buttons which customers must chose. I've managed to get everything working, but what I need now is to send the selected radio button to the cart, checkout and so on when the submit button is clicked.
I am trying to use their product customization function to do this and their function is:
if (!function_exists('jigoshop_product_customize_panel')) {
function jigoshop_product_customize_panel() {
global $_product;
if ( isset( $_POST['Submit'] ) && $_POST['Submit'] == 'Save Personalization' ) {
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom_products[$_POST['customized_id']] = trim( wptexturize( $_POST['jigoshop_customized_product'] ));
jigoshop_session::instance()->customized_products = $custom_products;
}
if ( get_post_meta( $_product->ID , 'customizable', true ) == 'yes' ) :
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom = isset( $custom_products[$_product->ID] ) ? $custom_products[$_product->ID] : '';
$custom_length = get_post_meta( $_product->ID , 'customized_length', true );
$length_str = $custom_length == '' ? '' : sprintf( __( 'You may enter a maximum of %s characters.', 'jigoshop' ), $custom_length );
echo '<div class="panel" id="tab-customize">';
echo '<p>' . apply_filters('jigoshop_product_customize_heading', __('Enter your personal information as you want it to appear on the product.<br />'.$length_str, 'jigoshop')) . '</p>';
?>
<form action="" method="post">
<input type="hidden" name="customized_id" value="<?php echo esc_attr( $_product->ID ); ?>" />
<?php
if ( $custom_length == '' ) :
?>
<textarea
id="jigoshop_customized_product"
name="jigoshop_customized_product"
cols="60"
rows="4"><?php echo esc_textarea( $custom ); ?>
</textarea>
<?php else : ?>
<input
type="text"
id="jigoshop_customized_product"
name="jigoshop_customized_product"
size="<?php echo $custom_length; ?>"
maxlength="<?php echo $custom_length; ?>"
value="<?php echo esc_attr( $custom ); ?>" />
<?php endif; ?>
<p class="submit"><input name="Submit" type="submit" class="button-alt add_personalization" value="<?php _e( "Save Personalization", 'jigoshop' ); ?>" /></p>
</form>
<?php
echo '</div>';
endif;
}
}
I tried modifying their function to suit my needs and this is what I've come up with (where get_cod is the id and name of the hidden input and "Adauga in cos" is the value of my submit button):
if (!function_exists('salveaza_cod_material')) {
function salveaza_cod_material() {
global $_product;
if ( isset( $_POST['submit']) && $_POST('submit') == 'Adauga in cos') {
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom_products[$_POST['customized_id']] = trim( wptexturize( $_POST['get_cod'] ));
jigoshop_session::instance()->customized_products = $custom_products;
}
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom = isset( $custom_products[$_product->ID] ) ? $custom_products[$_product->ID] : '';
}}
However the value isn't sent to the next page. Can anybody please help? Cheers!
Here's an update containing more of the code:
<form action="<?php echo esc_url( $_product->add_to_cart_url() ); ?>" class="variations_form cart" method="post">
<fieldset class="variations">
<?php foreach ( $attributes as $name => $options ): ?>
<?php $sanitized_name = sanitize_title( $name ); ?>
<div>
<span class="select_label"><?php echo jigoshop_product::attribute_label('pa_'.$name); ?></span>
<select id="<?php echo esc_attr( $sanitized_name ); ?>" name="tax_<?php echo $sanitized_name; ?>">
<option value=""><?php echo __('Choose an option ', 'jigoshop') ?>…</option>
<?php foreach ( $options as $value ) : ?>
<?php if ( taxonomy_exists( 'pa_'.$sanitized_name )) : ?>
<?php $term = get_term_by( 'slug', $value, 'pa_'.$sanitized_name ); ?>
<option value="<?php echo esc_attr( $term->slug ); ?>"><?php echo $term->name; ?> </option>
<?php else : ?>
<option value="<?php echo esc_attr( sanitize_title( $value ) ); ?>"><?php echo $value; ?></option>
<?php endif;?>
<?php endforeach; ?>
</select>
</div>
<?php endforeach;?>
</fieldset>
<div id="piele-neagra" class="colors" style="display:none">
<ul class="materiale">
<li><input type="radio" name="piele-neagra" value="L73">
<p><img class="alignnone size-full wp-image-155" title="L73" src="http://www.scaune-directoriale.ro/wp-content/uploads/materiale/piele-neagra/L73.gif" alt="L73" width="72" height="72" /></p>
</li>
</ul>
</div>
<div id="stofa-mf" class="colors" style="display:none">
<ul class="materiale">
<li><input type="radio" name="tapiterie" value="MF01" />
...
<div id="stofa-rg" class="colors" style="display:none"> Stofa RG</div>
<div class="clear"></div>
<span id="cod_material"><?php echo esc_attr( $custom ); ?></span>
<span><?php echo trim( wptexturize( $_POST['get_cod'] )); ?></span>
<div class="single_variation"></div>
<?php do_action('jigoshop_before_add_to_cart_form_button'); ?>
<div class="variations_button" style="display:none;">
<input type="hidden" name="variation_id" value="" />
<input type="hidden" name="customized_id" value="<?php echo esc_attr( $_product->ID ); ?>" />
<input type="hidden" id="get_cod" name="get_cod" value="" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $post->ID ); ?>" />
<div class="quantity"><input name="quantity" value="1" size="4" title="Qty" class="input-text qty text" maxlength="12" /></div>
<input type="submit" id="submit_cart" class="button-alt" value="<?php esc_html_e('Add to cart', 'jigoshop'); ?>" />
</div>
<?php do_action('salveaza_cod_material'); ?>
<?php do_action('jigoshop_add_to_cart_form'); ?>
</form>
And this is the section of the cart that receives the value:
if ( !empty( $values['variation_id'] )) {
$product_id = $values['variation_id'];
} else {
$product_id = $values['product_id'];
}
$custom_products = (array) jigoshop_session::instance()->customized_products;
$custom = isset( $custom_products[$product_id] ) ? $custom_products[$product_id] : '';
if ( ! empty( $custom_products[$product_id] ) ) :
?>
<dl class="customization">
<dt class="customized_product_label"><?php echo apply_filters('jigoshop_customized_product_label', __('Personal: ','jigoshop') ); ?></dt>
<dd class="customized_product"><?php echo esc_textarea( $custom ); ?></dd>
</dl>
<? php
endif;
?>
It's just a typo: in if ( isset( $_POST['submit']) && $_POST('submit') == 'Adauga in cos'), replace $_POST('submit')by $_POST['submit']

Categories