• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

QUESTION snippet for checkout semplification

manuele

New member
Messages
1
Likes
0
Points
1
#1
Hi!
I'd like to simplify the checkout form in case only free products are in the chart.
For this simple purpose I thought to integrate a little snippet using the "Woody snippets" plugin with this code:

PHP:
<?php
/**
 * @snippet     Simplify free WooCommerce checkout if cart does not need payment
 * @resources
 *                 tutorial https://www.skyverge.com/blog/how-to-simplify-free-woocommerce-checkout/
 *                gist     https://gist.github.com/bekarice/474ab82ab37b8de8617d
 */

if ( !is_null(WC()->cart) && (WC()->cart->total)==0 ) {
    // remove coupon forms since why would you want a coupon for a free cart??
    remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
    // Remove the "Additional Info" order notes
    add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
    // Unset the fields we don't want in a free checkout
    function unset_unwanted_checkout_fields( $fields ) {
        // add or remove billing fields you do not want
        // fields: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/#section-2
        $billing_keys = array(
            //'billing_last_name',
            'billing_company',
            'billing_phone',
            'billing_address_1',
            'billing_address_2',
            'billing_city',
            'billing_postcode',
            'billing_country',
            'billing_state',
        );
        // unset each of those unwanted fields
        foreach( $billing_keys as $key ) {
            unset( $fields['billing'][ $key ] );
            // unset( $fields['billing'][ $key ]['required'] );
        }
        return $fields;
    }
    add_filter( 'woocommerce_checkout_fields', 'unset_unwanted_checkout_fields' );
}
?>
Applying the script to the checkout page I can actually see a lighter form without the unwanted fields but it seams the code is not considered when the form is submitted via AJAX to the service address https://<my site host>/?wc-ajax=checkout so despite if I cannot see the unwanted fields they are however checked, the form is not validated and the service response replays with this response:

JSON:
{"result":"failure","messages":"<div class=\"woocommerce-error row-container row-message\">\n\t<div class=\"row-parent no-h-padding no-top-padding double-bottom-padding\">\n\t\t<ul class=\"woocommerce-error-list woocommerce-error\" role=\"alert\">\n\t\t\t\t\t\t\t<li><strong>Cognome di fatturazione<\/strong> \u00e8 un campo obbligatorio.<\/li>\n\t\t\t\t\t\t\t<li><strong>Provincia di fatturazione<\/strong> \u00e8 un campo obbligatorio.<\/li>\n\t\t\t\t\t\t\t<li><strong>Telefono di fatturazione<\/strong> \u00e8 un campo obbligatorio.<\/li>\n\t\t\t\t\t<\/ul>\n\t<\/div>\n<\/div>\n","refresh":false,"reload":false}
How can I fix it?
Thank you very much in advance.

Best regards

Manuele
 

Temyk

Developer & Support
Messages
1,129
Likes
42
Points
48
#2
Hello.

You remove from the form fields that are required. Contact Woocommerce support with the question how to make the fields optional or read your own link in the code.