Include files. If the front-end file becomes the default file and more classes are added, you must move to a lower level hierarchical file (like the back-end file). Now let’s see how to add custom fields to the woocommerce payment page using a custom plug-in. 1. Open the common IDE and create a folder containing three files. QuadLayers_checkout_fields \/__Classes \/__\/__class_qlccf_base. PHP (front-end file) \/ _\/ _class_qlccf_back. PHP (backend file) \/ _quadlayers_checkout_fields. PHP (main file)
2. The default file of the main file is quadlayers_checkout_fields. php . Male error – quadlayers_checkout_fields. The existing run_init class in PHP!
\
}
You can change the plug-in information, function and file name and put it directly into the. However, in order to better understand how it works, it is best to copy and paste the script first rather than edit anything.
3. The front-end class file is class_qlccf_base. php。 This is where the main class is located and can contain unlimited classes and functions. Abstract classes cannot be instantiated, so they must inherit from other classes. The following is the detailed description of the front-end file and code. \
\/\/\
\
\
), $checkout – > get_value (\
}
\/\/Insert text in payment Coupon – hook: after_billing _form
Public function quadlayers_email_checkout ($checkout2){
Woocommerce_form_field (\
\
\/\/\
\
\
), $checkout2->get_value( ‘altmail’ ) );
}
\/\/Insert a radio custom field in the checkout woocomer – hook: before_order_notes
Public function quadlayers_radio_checkout ($checkout3){
Woocomerce_form_field (\
\
\/\/\
\
\
\
\
\
\
“YouTube”=>“YouTube”,
\
)
));
}
\/\/Save all custom field values.
Public function quadlayers_save_function ($order_id){
If (! Bin ($_post [\
Update_post_meta ($order_id, \
}
If (! Empty ($_post [\
update_post_meta( $order_id, ‘altmail’,sanitize_text_field( $_POST[‘altmail’] ) );
}
If (! Empty ($_post [\
Update_post_meta ($order_id, \
}
}
}\/\/Include backend classes
include_once(plugin_dir_path( __FILE__ ).’ class_qlccf_back. php’);
}
Another{
Echo: \
}
After declaring the class, define several constants for later use. It then contains all woocommerce hooks for a single method, called add_base_hooks().
The abstract class cannot be instantiated, so run the method in the back-end file and execute all the hooks declared here. We use each hook for different tasks and assign each hook one of the following methods: For example, the woocomer_after_order_notes of the hook: add_base_hooks() method will run in the quadlayers_subscribe_checkout() add_base_hooks() method defined in the base_class class class. Please note how to use word press and woocommerce functions. Output custom fields in the woocommerce_form_field() checkout form. The default metadata types of this post, post _, and other post are used to update the metadata of the post. In addition, the available input field types are:
Male male text male choice male radio male password male date time male datetime – local male date male date male date male number of plants male telephone number Back end file, class_qlccf_back. In PHP, the qlccf_back_class class is class_qlccf_base. Inherit the base_class previously defined in PHP file.
ID );
$c_meta = $order – > get_meta (\
if($c_meta==1):$img_url=’ https:\/\/www.sebastopolys.com\/wp-content\/uploads\/2020\/07\/true-icon.png ‘;
else:$img_url=’ https:\/\/www.sebastopolys.com\/wp-content\/uploads\/2020\/07\/false-icon.png ‘;
Endif
echo ‘
‘;
}
Else if (\
$order = wc_get_order( $post->ID );
$e_meta = $order->get_meta(‘altmail’);
Eco$e_m
eta;
}
Another {}
}
#Column settings for check boxes
Public function qlccf_checkbox ($columns){
$columns [\
Returns the $column;
}
#Set columns for alternate messages
Public function qlccf_email ($columns 1){
$columns 1 [\
Return $columns 1;
}
#Include ALT mail field in WC email dashboard
Public function qlccf_email_template ($order_obj){
$is_set = get_post_meta( $order_obj->get_order_number());
\/\/If no custom field is set, return
If (empty ($is_set))
return goods
\/\/OK, continue to echo custom fields.
$alt_email = get_post_meta( $order_obj->get_order_number(), ‘altmail’, true );
Echo \
}
}
$run = new qlccf_back_class;
Endif
This file defines the generator that performs the front-end hook described in other files. Then, use the conditional if() and WordPress function is_admin() to add the hooks required to display the custom fields in the back-end order list and the woocommerce email template, which is applicable only when the user is on the management back-end screen. The manage_shop_order_posts_custom_column() hook inserts the column into the order list, so the field can be displayed in the qlccf_column_content() function.
If connected to the woocomerce loop, check whether the field name belongs to one of the custom fields, and if so, print. You can use conditional if () else to check all custom fields in the same function. Then create check boxes and text custom field columns in the back-end order list. After the customer completes the payment process and creates an order, a custom column must be set to display the custom woocommerce field. Finally, in the last method, use the woocommerce_email_order_meta hook to display custom text fields in the administrator email template. WordPress and woocommerce functions used: wc_get_order(): get the order object with all data currently attached. Get_meta(): get the custom field value stored in the database to get the metadata of the order. Get_order_number(): to get the ID number of the current order, it is generally necessary to customize the check-out page in all online stores. This guide shows you how to programmatically add custom fields to the woocommerce checkout page by using a small amount of coding. To optimize payment, you learned how to create custom plug-ins step by step. In addition, if you want to customize the payment page and go to the next step, we recommend that you check the guide for this step. Try this method
Did you see? Can you find a way to improve? Please share your experience in the comments section below! To improve woocommerce’s transition, we recommend that you review the following guidelines. Woocommerce’s best quick purchase button how does woocommerce optimize payment through coding and edit the shopping page in woocommerce?