I am trying to require a handful of fields in the Admin area of WooCommerce inside of the Create New Order form. I have successfully worked out how to do this for all of the fields except for the State drop-down menus. Here is what I have so far (all of this code is functioning properly):
// Require certain fields in NEW ORDER creation via Admin
add_filter('woocommerce_admin_billing_fields', 'woocommerce_require_admin_billing_fields');
function woocommerce_require_admin_billing_fields( $fields ){
$fields['first_name']['custom_attributes'] = array( 'required' => '1' );
$fields['last_name']['custom_attributes'] = array( 'required' => '1' );
$fields['address_1']['custom_attributes'] = array( 'required' => '1' );
$fields['city']['custom_attributes'] = array( 'required' => '1' );
$fields['postcode']['custom_attributes'] = array( 'required' => '1' );
$fields['country']['custom_attributes'] = array( 'required' => '1' );
$fields['email']['custom_attributes'] = array( 'required' => '1' );
$fields['phone']['custom_attributes'] = array( 'required' => '1' );
return $fields;
}
add_filter('woocommerce_admin_shipping_fields', 'woocommerce_require_admin_shipping_fields');
function woocommerce_require_admin_shipping_fields( $fields ){
$fields['address_1']['custom_attributes'] = array( 'required' => '1' );
$fields['city']['custom_attributes'] = array( 'required' => '1' );
$fields['postcode']['custom_attributes'] = array( 'required' => '1' );
$fields['country']['custom_attributes'] = array( 'required' => '1' );
$fields['state']['custom_attributes'] = array( 'required' => '1' );
return $fields;
}
// Require fields that are not included in woocommerce_admin loops
function vic_require_admin_fields() {
echo '<script>document.getElementById("excerpt").setAttribute("required", "1")</script>'; // order notes (excerpt)
echo '<script>document.getElementById("_payment_method").setAttribute("required", "1")</script>'; // payment method
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'vic_require_admin_fields' );
I have tried to require the "State" drop-down field in a similar manner as the rest (through a filter targeting the "state" id) and I have also attempted to target it with a javascript action, but both efforts produced no solution. It looks like the way the form is constructed is through a boostrap drop-down? The actual tag is set to display: none, so I can't really do much with that.
I can see that when I select a selection without a state, the SELECT field turns into an INPUT field, which is I think why I am unable to target it to add the required attribute:
It's alright if I can't do this with a filter. I'm thinking I may need some javascript attached to the button for form validation maybe? I couln't seem to find the actual form id, in order to validate through javascript, though I will probably look more around this evening when I'm home.
Any ideas to help me find a solution will be much appreciated!
Related
Is there something i am missing?
I try to create a new custom attribute for plugin purpose, but somehow it doesnt work.
The simple function is as follows:
$data = array(
'name' => 'programmas',
'slug' => 'elearning_programmas',
'type' => 'select',
'order_by' => 'menu_order',
'has_archives' => 1
);
wc_create_attribute( $data );
However, whatever i do, no new attribute is created in the list with attributes under the product attributes page, neither available on the individual product page.
Any ideas?
I want to add default image options in WordPress admin (we can say set default options in image field ) when I try to create an image field using ACF thank you so much in advance
You can put the code like below in your functions.php
add_action('acf/render_field_settings/type=image', 'add_defult_image_field');
function add_defult_image_field($field) {
acf_render_field_setting( $field, array(
'label' => 'Defult Image',
'instructions' => 'Appears when creating a new post',
'type' => 'image',
'name' => 'defult_value',
));
}
Taken from https://stupop010.medium.com/how-to-set-a-default-image-on-acf-wordpress-977d38869b78, you need to create an action to a default_value field. After that, add a filter that checks when the user did not select any image, and then loads the default value.
add_action('acf/render_field_settings/type=image', function ($field) {
acf_render_field_setting( $field, array(
'label' => __('Default Image','acf'),
'instructions' => __('Appears when creating a new post','acf'),
'type' => 'image',
'name' => 'default_value',
));
});
add_filter('acf/load_value/type=image', function ($value, $post_id, $field) {
if (!$value) {
$value = $field['default_value'];
}
return $value;
}, 10, 3);
I have a custom module on my site. I try to install an update with a new field for my vocabulary, but the field doesn't appear.
hook_update:
function mymodule_update_7118()
{
$field_name = 'field_newfield';
if ( field_info_field( $field_name ) ) {
return;
}
$field = array(
'field_name' => $field_name,
'type' => 'list_integer',
'settings' => array(
'allowed_values' => array(
'Yes' => 1, //heard that adding a NO value may cause problems, although it doesn't work with a no value either.
),
),
);
$field = field_create_field( $field );
$instance = array(
'field_name' => $field['field_name'],
'entity_type' => 'taxonomy',
'bundle' => 'vocab_name',
'label' => 'Label',
'widget' => array(
'active' => 1,
'module' => 'options',
'settings' => array(),
'type' => 'options_select',
'weight' => '3',
),
);
field_create_instance($instance);
}
Logs contain several recordings of Internalization module creating a string to translate this field. Also all needed tables are created in the database, but they are all empty.
For creating a new custom field you must do it like a custom module. The steps can be found out at https://drupal.stackexchange.com/questions/140517/how-to-create-new-field-type
You can find the excellent field_example module from the Examples Module which is always the first place to look. Examples module can be downloaded from https://www.drupal.org/project/examples
I have made a custom module and it works fine till I start working on my custom theme.
Once I move over to my custom theme I get this error
Warning: Missing argument 2 for customvishal_form(), called in
/home/vishal/Dropbox/sites/new/includes/theme.inc on line 1029 and
defined in customvishal_form() (line 441 of
/home/vishal/Dropbox/sites/new/sites/all/modules/customvishal/customvishal.module).
You can see the error at : http://www.iamvishal.com/dev/about-us
I don't think anything is wrong with my code :
/**
* A simple form.
*/
function customvishal_form($form, &$form_submit) {
$form['customvishalactivate'] = array(
'#title' => t('Activate Preference'),
'#type' => 'radios',
'#options' => array('1' => t('Yes'), '0' => t('No')),
'#required' => TRUE,
);
return $form;
}
Its called from
function customvishal_pref($arg1)
{
// Here we willl make the form and save the data so when cron
// runs we will check the users preference
$build = array(
'header_text' => array(
'#type' => 'markup',
'#markup' => '<p>' . t('This page is where you add your preferences. Based on your
entered choices we will send you alerts ') . '</p>',
),
'example_form' => drupal_get_form('customvishal_form'),
);
return $build;
}
What might be causing this problem ?
Cheers,
Vishal
I had the same issue
I called hook_form like this:
/**
* Implements of hook_menu().
*/
function skupina_menu() {
$items = array();
$items['admin/config/people/skupina'] = array(
'title' => 'Skupiny odborníkov',
'description' => 'Prehľady návštev odborníkov',
'page callback' => 'drupal_get_form',
'page arguments' => array('skupina_statistics'),
'access arguments' => array('view statistics'),
'file' => 'admin.inc',
'file path' => drupal_get_path('module', 'skupina'),
'weight' => 1,
);
return $items;
}
and then
/**
* Prehlad navstev odbornikov - page
*/
function skupina_statistics($form, &$form_state) {
$form = array();
$form['skupina_obdobie'] = array(
'#type' => 'select',
'#title' => t('Zmeniť zobrazenie'),
'#options' => skupina_get_zobrazenia(),
'#description' => t('Zmení filtrovanie dát podľa zvolenej možnosti.'),
'#required' => FALSE,
);
return $form;
}
my problem was, that the function didn't have the "_form" in its name so its produce those warnings.
So the function must be called "skupina_statistics_form in my case
when the form function is called the only parameter that is sent to it is the form variable. Since your second function parameter doesn't have a default value it obviously produces a warning.
If you never use it in the function code you might consider removing it or providing a default value.
e.g.:
function customvishal_form($form, &$form_submit = NULL)
or you might consider passing an additional parameter. You can do this like so:
drupal_get_form('customvishal_form', $some_your_parameter);
I think I know the answer. I had the exact same problem.
Is your module named exactly as your custom theme? Mine was and I changed my theme name and the error went away
I have a custom content type that has a custom field called [stock]
I would like to have a single page that shows me ALL nodes [stock] values in a list and allows me to change the value. Is this possible?
Any advice would be much appreciated
Well I was going to write a module to this but as usual with Drupal someone beat me to it!
You should download and install Views, and also the (quite frankly excellent) Slick Grid module. It provides a view type (Slick Grid) that will create a table of content, with inline editing enabled for fields. For more complex field types it provides a modal popup for editing instead.
It took me 10 minutes to create a view with the following output, which lets me edit fields directly in the table by double clicking on the cell:
The content is saved automatically to the database when you click off the edit cell again, and it's all handled via AJAX which makes it very pleasant to use.
You can add as many fields as you like to the grid so I think this is exactly what you're after :)
Yes it is I have done something like this to get it working. In your case you would have to modify the sql query to fetch your custom field. You also would need to modify the $headers and the $rows array(s) to include what you need to display, but I think you get the idea.
// first I create my own module hooking the menu
function custom_menu() {
$items['admin/custom-content'] = array(
'title' => t('Custom Content'),
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('access content'),
);
$items['admin/custom-content/product'] = array(
'title' => t('Products'),
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('access content'),
'page callback' => 'custom_view_products'
);
return $items;
}
// the above will add a new admin menu link called Custom Content
// then I have a callback method for the menu item
function custom_view_products() {
// setup the header for the form
$headers = array(
array('data' => 'Title', 'field' => 'title', 'sort' => 'asc'),
array('data' => 'Published')
);
//
$query = "SELECT n.* FROM {node} n ".
"WHERE n.type = 'product' ORDER BY n.title ASC";
//
$rows = array();
if(($results = db_query($query)->fetchAll())) {
foreach ($results as $node) {
$rows[] = array(
'data' => array(
l($node->title, 'node/'. $node->nid .'/edit'),
$node->status
), 'class' => array('published')
);
}
}
$html = theme('table',
array(
'header' => $headers,
'rows'=>$rows,
'caption' => '',
'sticky' => FALSE,
'empty' => 'No products located...',
)
);
return $html;
}