I use the following code to show date input on the checkout page, after additional information:
add_action( 'woocommerce_after_order_notes', 'custom_checkout_field' );
function custom_checkout_field( $checkout ) {
echo '<div id="custom_checkout_field"><h2>' . __('Date picker:') . '</h2>';
woocommerce_form_field(
'custom_field_name', array(
'type' => 'date',
'class' => array( 'my-field-class form-row-wide' ),
'label' => __( 'Custom Additional Field' ),
'placeholder' => __( 'New Custom Field' ),
),
$checkout->get_value( 'custom_field_name' )
);
echo '</div>';
}
But there is a problem with this code, all days are selectable, I am looking for a way to make previous days, and Sundays unclickable. Please give me some tips to solve this problem.
Related
I'm trying to add this basic code to add a new field to the check out page. However, it seems to disable the place order button. Here is the code:
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
woocommerce_form_field( 'my_field_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Were you assisted with this order?'),
'placeholder' => __('Please enter the name of your rep here'),
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
You just need to remember to add the start echo'<div>' tag for your field options div. Otherwise it disables the woo-commerce functionality on the page.
add_action('woocommerce_after_order_notes','my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
//need to remember echo start tag here!
echo '<div id="my_custom_checkout_field">';
woocommerce_form_field( 'name_of_child', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('Name of Child'),
'placeholder' => __('Enter something'),
), $checkout->get_value( 'name_of_child' ));
echo '</div>';
}
I have been scratching my head all day. I would like to add a custom checkbox within order review before placing order. Here is my code:
add_action( 'woocommerce_checkout_order_review', 'my_checkbox' );
function my_checkbox( $checkout ) {
echo '<div class="my_split_checkbox"><h2>' . __('Split Order', 'woocommerce') . '</h2>';
woocommerce_form_field( 'my_split_checkbox', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Split Order', 'woocommerce'),
'required' => false,
), $checkout->get_value( 'my_split_checkbox' ));
echo '</div>';
}
but the page doesn't fully load. However if I replace the hook by
add_action( 'woocommerce_after_checkout_billing_form', 'my_checkbox' );
then the chekbox shows at the end of billing details with no issues. How can I get the textbox to show within checkout order review? Ideally after table .woocommerce-checkout-review-order-table.
You should use any of those below hooks instead of woocommerce_checkout_order_review to show the checkbox field based on your priority-
woocommerce_review_order_after_cart_contents
woocommerce_review_order_before_shipping
woocommerce_review_order_after_shipping
woocommerce_review_order_before_order_total
woocommerce_review_order_after_order_total
For further information go to woocommerce/templates/checkout/review-order.php. If you already copied the templates folder as woocommerce to your theme directory then may be you'll find the review-order.php there. And also you need to remove $checkout variable as well as , $checkout->get_value( 'my_split_checkbox' ). Cause those hooks do not pass any parameter. Please check the review-order.php, you'll get an overview.
So your whole code will be like below-
add_action( 'woocommerce_checkout_order_review', 'my_checkbox' );
function my_checkbox() {
echo '<div class="my_split_checkbox"><h2>' . __('Split Order', 'woocommerce') . '</h2>';
woocommerce_form_field( 'my_split_checkbox', array(
'type' => 'checkbox',
'class' => array('checkbox_field'),
'label' => __('Split Order', 'woocommerce'),
'required' => false,
));
echo '</div>';
}
Hope that helps.
I'm attempting to register a new widget area in my Genesis child theme for WordPress. The code doesn't seem to be working and crashes my site. Can anyone tell me what I'm doing wrong here? I'm trying to get it above the content on all pages. This is the code I have:
genesis_register_sidebar( array(
'id' => ‘above_content’,
'name' => __( ‘Above Content’, 'domain' ),
'description' => __( ‘Above the content’, 'domain' ),
));
add_action( 'genesis_before_loop’, ‘above_content’ );
function your_widget() {
if ( is_active_sidebar(‘above_content’) ) {
genesis_widget_area( ‘above_content’, array(
'before' => '<div class=“above_content widget-area">',
'after' => '</div>',
));
}
}
Two things:
Make sure you're using standard quotes ', not curly quotes ‘
Assign the proper callback to your action
genesis_register_sidebar( array(
'id' => 'above_content',
'name' => __( 'Above Content', 'domain' ),
'description' => __( 'Above the content', 'domain' ),
) );
add_action( 'genesis_before_loop', 'your_widget' );
function your_widget() {
if ( is_active_sidebar('above_content') ) {
genesis_widget_area( 'above_content', array(
'before' => '<div class="above_content widget-area">',
'after' => '</div>',
) );
}
}
If you want to register another widget, just register a new sidebar (with a unique ID), and add new logic to a new callback function (or add it to your_widget()). Where you add this additional widget depends on your specific use case.
Sorry for the really newbie question.
How do I align my labels with my radio button?
I have just added custom fields to my checkout page.
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('Delivery Timing') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
'type' => 'radio','options' => array( 'a' => __( 'Morning 09:00 - 13:00' ), 'b' => __( 'Afternoon 14:00 - 18:00' ), 'c' => __( 'Evening 18:00 - 22:00' ) ),
'class' => array('my-field-class form-row-wide'),
'label' => __('Select Your Preferred Delivery Timing'),
'required' => true,
), $checkout->get_value( 'my_field_name' ));
echo '</div>';
}
Screenshot:
I am using woocommerce for my site. I wanna to add some extra fields in add product page following to SKU, Regular Price, Sale Price.. Extra fields contains default values like 2% or 5%. when user enters Product price it should be calculated with default field values & result should be displayed in another field..
For Example:
SKU : 001
Regular Price(Rs) : 100
Added Text Field1 : 5% (5% of 100 = 5)
Added Text Field2 : 2% (2% of 100 = 2)
Answer Field : 107 (100 + 5 + 2)
Note: Answer field should be automatically calculated from values present in Regular Price/Sale Price + added text field1 + added text field2.
How to do this???
I have created fields using following function...
// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
// Custom fields will be created here...
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'Our Commision', 'woocommerce' ),
'placeholder' => '5%',
'desc_tip' => 'true',
'description' => __( 'Commision will be added to Product Actual Price', 'woocommerce' )
)
);
// Text Field
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'Payment Gateway Charges', 'woocommerce' ),
'placeholder' => '2%',
'desc_tip' => 'true',
'description' => __( 'Payment Gateway Charges will be added to Product Actual Price', 'woocommerce' )
)
);
echo 'Selling Price = Your Price + Our Commision + Payment Gateway Charges.';
echo '</div>';
}
Add below code to your theme's functions.php :
add_action( 'woocommerce_product_options_general_product_data', 'so28712303_rohil_add_custom_general_fields' );
// Save Fields
add_action( 'woocommerce_process_product_meta', 'so28712303_rohil_add_custom_general_fields_save' );
function so28712303_rohil_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'field_1',
'label' => __( '<strong>Extra Field 1</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Please enter a number', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</div>';
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'field_2',
'label' => __( '<strong>Extra Field 2</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Please enter a number', 'woocommerce' ),
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
)
);
echo '</div>';
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => 'result_field',
'label' => __( '<strong style="color:#239804">Result</strong>', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Percentage of Price', 'woocommerce' ),
'type' => 'number',
'readonly' => 'readonly',
'custom_attributes' => array(
'step' => 'any',
'min' => '0',
'readonly' => 'readonly'
)
)
);
echo '</div>';
}//so28712303_rohil_add_custom_general_fields
function so28712303_rohil_add_custom_general_fields_save( $post_id ){
$woocommerce_field_1 = $_POST['field_1']; //Value of Extra field 1
$woocommerce_field_2 = $_POST['field_2']; //Value of Extra field 2
$woocommerce_result_field = $_POST['result_field']; //No use of this..you can delete
$regular_price = $_POST['_regular_price']; //Value of regular price
if( !empty( $woocommerce_field_1 ) || !empty( $woocommerce_field_2 ) ):
update_post_meta( $post_id, 'field_1', esc_attr( $woocommerce_field_1 ) ); //Save value of Extra Field 1
update_post_meta( $post_id, 'field_2', esc_attr( $woocommerce_field_2 ) ); //Save value of Extra Field 2
endif;
$result_field = ( $woocommerce_field_1 * $regular_price ) / 100 ; //Calculation goes here ...
//if(empty($woocommerce_result_field))
update_post_meta( $post_id, 'result_field', esc_attr( $result_field ) ); //Save result here ...
}
Let me know if you have any doubt.
Screen shot :