Hi i am using a plugin to allow employers to register and post jobs. In the registration I have created custom fields they are also in the admin and in the Profile (VAT IDs they I need for billing). I can automaticaly fill and save them also update in the Profile or admin back-end. But I have to include them in the billing fields (not so important where maybe after company name) where they are generated into the invoice. I am not a programmer I only know html and css little bit. I am trying now for 12 hours and no chance to get it there.
I would also like to make them not-editable from user profile.
This are my fields saved in the child-theme functions.php I have done this with the help from Plugin author but he will not help me more because of no support. Plugin guide
add_action('iwj_employer_form_after_general',function ($job){
$post_id = $job ? $job->get_id() : '';
?>
<?php
iwj_field_text( '_ico_company', 'IČO spoločnosti*', true, $post_id, null, 'true', '', __( '' ) );
?>
<?php
});
add_action('iwj_admin_employer_form_after_general',function ($post_id){
?>
<?php
iwj_field_text( '_ico_company', 'IČO spoločnosti*', true, $post_id, null, 'true', '', __( '' ) );
?>
<?php
});
add_action('save_post', function($post_id){
if($post_id && get_post_type($post_id) == 'iwj_employer'){
$custom_field_value = sanitize_text_field($_POST['_ico_company']);
update_post_meta($post_id, '_ico_company', $custom_field_value);
}
}, 99);
add_action('iwj_register_process', function($uid){
$user = IWJ_User::get_user($uid);
$post_id = 0;
if($user->is_employer()){
$emp = $user->get_employer();
$post_id = $emp->get_id();
}
if($post_id){
//Add you custom field name process here
update_post_meta($post_id, '_ico_company', sanitize_text_field($_POST['_ico_company']));
}
});
I would be mega happy if someone could help me out with this :)
This will help you to have the fields in the checkout. I added a mock function get_vat_field, because I don't know yet how you are going to get the VAT field in the checkout.
Generally, it adds an additional field to the checkout, and when the checkout is completed, it adds it to the order. Which then is displayed in the order meta of the order. You can find that in the order itself in the admin panel (order edit page).
Simply add this to your functions.php of your child theme.
// Our hooked in function – $fields is passed via the filter!
function add_vat_to_checkout_fields( $fields ) {
$fields['billing']['vat'] = array(
'type' => 'text',
'label' => __('VAT', 'woocommerce'),
'placeholder' => _x('VAT', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide hidden'),
'clear' => true,
'value' => get_vat_field('') // this is the function that gets the field from the user account or job post.
);
return $fields;
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('VAT').':</strong> ' . get_post_meta( $order->get_id(), 'vat', true ) . '</p>';
}
Related
I am using WPforms plugin for making forms in Wordpress and I want users fill in each form once so I write this code in functions.php :
<php
$entries = wpforms()->entry->get_entries(
array(
'form_id' => 74, // CHANGE THIS FORM ID
'user_id' => get_current_user_id(),
),
true // count values only, we don't need actual data
);
// We allow to fill the form only once.
if ( $entries >= 1 ) {
add_action( 'wpforms_frontend_output_before', function () {
echo '<p>You have already submitted this form.</p>';
} );
add_action( 'wpforms_frontend_load', '__return_false' );
}} );
the problem is that it applies to all forms that was created and I don't know why :(
This might be a stupid question, but the trivial thing is that the most common function fails to work.
By this code I have set up my custom field:
/**
* Extra custom fields
*/
function ccf_create_custom_field() {
$args = array(
'id' => 'custom_cost_field',
'label' => __( 'Product Cost', 'woocommerce' ),
'class' => 'ccf-cost-field',
'type' => 'number',
);
woocommerce_wp_text_input( $args );
}
/* Display Fields */
add_action( 'woocommerce_product_options_general_product_data', 'ccf_create_custom_field' );
/* Save Fields */
add_action('woocommerce_process_product_meta', 'woocommerce_product_custom_fields_save');
function woocommerce_product_custom_fields_save($post_id) {
/* Custom Product Number Field */
$woocommerce_custom_product_number_field = $_POST['custom_cost_field'];
if (!empty($woocommerce_custom_product_number_field))
update_post_meta($post_id, 'custom_cost_field', esc_attr($woocommerce_custom_product_number_field));
}
require_once "custom.php";
This is my custom.php:
<?php while (have_posts()) : the_post(); ?>
<?php wc_get_template_part('content', 'single-product'); ?>
<?php
// Display the value of custom product number field
echo get_post_meta(get_the_ID(), 'custom_cost_field', true);
?>
<?php endwhile; // end of the loop. ?>
This is the function that obtains values:
$costs = (ceil((get_post_meta($product, 'custom_cost_field', true))/100)*85);
Although the custom field has a value saved of for example of 1090, the get_post_meta does not return anything, as the value in my table is always 0 for costs. I absolutely don´t understand this :S.
Any ideas? Am I missing something out?
Okay, this is the answer:
$custom_cost = ceil(get_post_meta(get_the_id(), 'custom_cost_field', true));
For custom fields, always keep the "get_the_id()" in the function and it will work. Thats the whole trick - never keep a variable (product, post) in there.
If you have obtained the ID via a different function before, it still won´t work - you must keep the "get_the_id()" in the function.
I am collecting an additional User Meta Data for my Woo commerce check out page.
woocommerce_form_field('myName', array(
'type' =>'text',
'class'=>array('my-field-class form-row-wide'),
'label'=>__('First Name'),
'placeholder'=>__('Please enter your name'),
), $checkout->get_value('myName'));
And I am updating to the database with this code:
/*Update the info with the checkout*/
add_action('woocommerce_checkout_field_update_order_meta','my_custom_checkout_field_update_meta');
function my_custom_checkout_field_update_meta($order_id){
if($_POST['MyName'])
update_post_meta($order_id, 'First Name',esc_attr($POST['MyName']));
}
Each time I submit, I get an internal server Error, even though I am working on a local machine. I need to collect that data and persist it into the order Database. Anybody help?
Update: Normally the postmeta meta_key should not use white spaces and capitals…
Also your additional field should need to be inside the checkout form, if not nothing will be submitted and nothing can be saved.
To display an custom text field and save it in the database once submitted, the best way is to use the following:
// Add checkout custom text field
add_action( 'woocommerce_before_order_notes', 'add_checkout_custom_field', 20, 1 );
function add_checkout_custom_field( $checkout) {
// Text field
woocommerce_form_field('my_name', array(
'type' => 'text',
'class' => array('my-field-class form-row-wide'),
'label' => __('First Name'),
'placeholder' =>__('Please enter your name'),
), $checkout->get_value('my_name') );
}
// Save the data to the order
add_action('woocommerce_checkout_create_order','my_custom_checkout_field_update_meta');
function my_custom_checkout_field_update_meta( $order ){
if( isset($_POST['my_name']) && ! empty($_POST['my_name']) )
$order->update_meta_data( '_my_name', sanitize_text_field($POST['my_name']) );
}
Code goes in function.php file of your active child theme (or active theme). Tested and works…
To get this data once saved in the order, use get_post_meta( $order_id, '_my_name', true ); where $order_id is the dynamic order ID…
Your additional text field will be located just before "Order notes" field:
Now your additional field is confusing as Billing and Shipping first name fields already exist in checkout page.
I'm a newbie in coding and currently using woocommerce subscription plugin and wanted to change the default "shop" if I don't have any subscription. I want it to link to a specific product instead of the shop.
I want to change the red part:
Below are the current code for this section:
<?php
// translators: placeholders are opening and closing link tags to take to the shop page
printf( esc_html__( 'Anda masih belum melanggan. Mula melanggan %sdi sini%s.', 'woocommerce-subscriptions' ), '', '' );
?>
</p>
<?php endif; ?>
I want to change the example.com/shop into something like example.com/product-category/myproduct
Any help will be appreciated.
To achieve that, you will need:
A conditional function that detect if the current user has an active subscription
A custom function hooked in woocommerce_subscriptions_message_store_url filter hook, where you will define your new URL linked to your product.
Here is the code:
// Conditional function detecting if the current user has an active subscription
function has_active_subscription( $user_id=null ) {
// if the user_id is not set in function argument we get the current user ID
if( null == $user_id )
$user_id = get_current_user_id();
// Get all active subscrptions for a user ID
$active_subscriptions = get_posts( array(
'numberposts' => -1,
'meta_key' => '_customer_user',
'meta_value' => $user_id,
'post_type' => 'shop_subscription', // Subscription post type
'post_status' => 'wc-active', // Active subscription
) );
// if user has an active subscription
if( ! empty( $active_subscriptions ) ) return true;
else return false;
}
// Change woocommerce subscription “shop” link inside my account page
add_filter( 'woocommerce_subscriptions_message_store_url', 'custom_subscriptions_message_store_url', 10, 1 );
function custom_subscriptions_message_store_url( $url ){
// If current user has NO active subscriptions
if( ! has_active_subscription() ){
// HERE Define below the new URL linked to your product.
$url = home_url( "/product-category/myproduct/" );
}
return $url;
}
This code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works
I understand that you can add custom fields on the checkout page of WooCommerce, but what I want to show before the billing details are the account fields, which are already existing as written in the documentation. These fields are named:
account_username
account_password
account_password-2
But they are not shown by default. I only managed to make them visible by putting them at the top of the list in the function for reordering the billing fields like this in my theme's function.php like this
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
$order = array(
"account_username",
"account_password",
"account_password-2",
"billing_first_name",
"billing_last_name",
// other billing fields go here
);
foreach($order as $field)
{
$ordered_fields[$field] = $fields["billing"][$field];
}
$fields["billing"] = $ordered_fields;
return $fields;
}
This works fine with the functionality of creating an account while checking out, but I am having troubles with modifying its label and placeholder. This is what I tried to do:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
$fields['account']['account_username']['label'] = '* Username: ';
$fields['account']['account_username']['placeholder'] = 'Enter username here...';
}
But it wouldn't let me change the labels and placeholder of the fields so I was thinking that maybe it has something to do with how I am displaying it and/or how I am modifying them.
Ideas, anyone? Thanks in advance.
I've found the answer to this, so if anyone has the same problem, this is the best solution. Instead of trying to make the accounts fields visible, in my case it's more efficient to manually output the fields I need since I won't be needing most of the default fields anyway.
What I did was override the form-billing.php template. I removed the loop for the fields which is this part:
<?php foreach ( $checkout->checkout_fields['billing'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
And replaced it with this to individually add them to the page:
<?php
woocommerce_form_field( 'billing_first_name', $checkout->checkout_fields['billing']['billing_first_name'], $checkout->get_value( 'billing_first_name') );
woocommerce_form_field( 'billing_email', $checkout->checkout_fields['billing']['billing_email'], $checkout->get_value( 'billing_email') );
woocommerce_form_field( 'account_username', $checkout->checkout_fields['account']['account_username'], $checkout->get_value( 'account_username') );
woocommerce_form_field( 'account_password', $checkout->checkout_fields['account']['account_password'], $checkout->get_value( 'account_password') );
woocommerce_form_field( 'account_password-2', $checkout->checkout_fields['account']['account_password-2'], $checkout->get_value( 'account_password-2') );
//...other fields that I need
?>
From there, the modifications for the label, placeholder, etc. works just fine. Hope it works for other people with the same issues too. Cheers! :)