Unable to add custom select field in woocommerce - wordpress

I want to add a custom field options to checkout page. I am using the following code :
$fields['billing']['billing_options'] = array(
'label' => __('Options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => array(
'option_a' => __('option a', 'woocommerce' ),
'option_b' => __('option b', 'woocommerce' )
)
);
I want to show the options (option_a,option_b) from database or
I want to use dynamic data and want to use for loop in the options menu
How can I use for loop inside this function ?

Just do it before, like this:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function my_custom_checkout_fields( $fields ) {
$args = array(
'post_type' => array('options'),
'posts_per_page' => -1
);
$posts = new WP_Query($args);
$options = array();
foreach ($posts as $post) {
$options[$post->ID] => attr_esc($post->post_title);
}
$fields['billing']['billing_options'] = array(
'label' => __('Options', 'woocommerce'),
'placeholder' => _x('', 'placeholder', 'woocommerce'),
'required' => false,
'clear' => false,
'type' => 'select',
'options' => $options
);
return $fields;
}

Related

how can i add post-categories list in elementor custom widget

My control code is
i want to create control in elementor which shows all post categories. please help me how can i achieve this...
enter image description here
$this->add_control(
'show_elements',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => [
$category,
],
]
); ```
You can use WP get_categories() to get all categories. check below code.
$options = array();
$args = array(
'hide_empty' => false,
);
$categories = get_categories($args);
foreach ( $categories as $key => $category ) {
$options[$category->term_id] = $category->name;
}
$this->add_control(
'show_elements',
[
'label' => __( 'Post Categoris', 'plugin-domain' ),
'type' => \Elementor\Controls_Manager::SELECT2,
'multiple' => true,
'options' => $options,
]
);

Wordpress / CMB2 Framework / Display data from repeatable fields in frontend

I have a own option-page in the backend-sidebar with this repeatable fields in it. (not a metabox) I cant figure out, how to display the data from the group field in the frontend.
function ww_register_theme_options() {
$ww_prefix = '_ww_';
$ww_contacts = new_cmb2_box( array(
'id' => $ww_prefix . 'ww_option_plugin',
'title' => esc_html__( 'Ansprechpartner', 'ww-contact' ),
'icon_url' => '/wp-content/plugins/ww-contact/assets/images/icons/ww-icon-white.png',
'object_types' => array( 'options-page' ),
'option_key' => 'ww_options',
) );
$ww_group_field = $ww_contacts->add_field(array(
'id' => $ww_prefix . 'contact_repeat_group',
'type' => 'group',
'description' => __('Ansprechparter Liste', 'ww-contact'),
'repeatable' => true,
'options' => array(
'group_title' => 'Ansprechpartner {#}',
'add_button' => __('neuer Ansprechpartner', 'ww-contact'),
'remove_button' => __('Entfernen', 'ww-contact'),
'sortable' => true,
),
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'forname',
'type' => 'text',
));
$ww_contacts->add_group_field($ww_group_field, array(
'name' => 'Vorname',
'id' => $ww_prefix . 'surname',
'type' => 'text',
));
}
Remove the prefix from your add_group_field ids they aren't needed. Then loop through like so...
$entries = get_post_meta( $parent, 'ww_contact_repeat_group', true );
if($entries){
foreach ( (array) $entries as $key => $entry ) {
if ( isset( $entry['forname'] ) ) {
$forname = $entry['forname'];
}
}
}

custom permalink structure for custom post type based on meta fields and custom taxonomy

I have two custom post types. One with the slug artwork and another with the slug research-materials. The permalink structure for an artwork post is http://example.com/artwork/artwork-slug and this is expected, works fine and what I want. On the post.php page for any post that is of the research-materials post type I have a custom field (via ACF) that has the slug of the artwork post that the research materials post is related to and the desired slug for the research materials post. There's also a custom taxonomy set for the research-materials post type which would play a part in the construction of the permalink structure for the research-materials posts. The permalink would look like http://example.com/artwork/related-artwork-slug/research-materials/custom-taxonomy-term/research-materials-slug
Here's the code I have so far but I'm not getting the result I'd expect... instead I'm redirected to the artwork custom post type when I should be redirected tot he research-materials custom post type single page.
Any help appreciated. Thanks in advance.
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_action( 'init', 'RRP::rewrite_stuff', 10, 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($query){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || !$query->is_main_query() ){
return;
}
$research_materials_related_artwork = get_query_var('research_materials_related_artwork_slug');
$research_materials_slug = get_query_var('research_materials_slug');
$research_materials_type = get_query_var('research_materials_type');
if( !empty($research_materials_related_artwork) && !empty($research_material_slug) && !empty($research_materials_type) ){
$meta_query = $query->get('meta_query');
if( empty($meta_query) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
else{
$meta_query[] = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '==',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '==',
)
);
}
$query->set('meta_query', $meta_query);
$tax_query = $query->get('tax_query');
if( empty($tax_query) ){
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
else{
$tax_query[] = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
}
$query->set('tax_query', $tax_query);
}
return $query;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_rule( '^artwork/([^/]*)/research-materials/([^/]*)/([^/]*)/?', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_slug=$matches[2]&research_materials_type=$matches[3]', 'top' );
}
}
RRP::init();
After much tinkering I figured it out. There were a lot of small things wrong with the above code and I also had to make use of the index_template hook. Here's the final code:
class RRP{
public static function init(){
// set up custom post types and custom taxonomies
add_action( 'init', 'RRP::build_custom_post_types' );
add_action( 'init', 'RRP::build_custom_taxonomy' );
// set up custom fields
add_action( 'acf/init', 'RRP::register_custom_fields' );
// validate saved value in custom fields
add_filter( 'acf/validate_value', 'RRP::validate_saved_value_in_custom_fields', 10, 4 );
// update related artwork slug
add_filter( 'acf/update_value', 'RRP::update_related_artwork_slug', 10, 3 );
// add the research material type
add_action( 'save_post_research-materials', 'RRP::set_research_material_type' );
// build rewrite rules
add_filter( 'index_template', 'RRP::point_artwork_and_artist_to_single_templates' );
add_action( 'init', 'RRP::rewrite_stuff', 0 );
add_filter( 'query_vars', 'RRP::build_query_vars', 10 );
add_filter( 'pre_get_posts', 'RRP::pre_get_posts', 10 );
}
public static function build_custom_post_types(){
$labels = array(
'name' => 'Research Materials',
'singular_name' => 'Research Material',
'add_new' => 'Add New Research Material',
'add_new_item' => 'Add New Research Material',
'edit_item' => 'Edit Research Material',
'new_item' => 'New Research Material',
'view_item' => 'View Research Material',
'search_items' => 'Search Research Materials',
'not_found' => 'No Research Materials found',
'not_found_in_trash' => 'No Research Materials found in Trash',
'parent_item_colon' => 'Parent Research Material:',
'menu_name' => 'Research Materials',
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array('research-material-type'),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array(
'slug' => 'research-materials',
'with_front' => false,
),
'capability_type' => 'post',
'supports' => array(
'title', 'editor'
)
);
register_post_type( 'research-materials', $args );
}
public static function build_custom_taxonomy(){
$labels = array(
'name' => 'Research Material Types',
'singular_name' => 'Research Material Type',
'search_items' => 'Search Research Material Types',
'popular_items' => 'Popular Research Material Types',
'all_items' => 'All Research Material Types',
'parent_item' => 'Parent Research Material Type',
'parent_item_colon' => 'Parent Research Material Type',
'edit_item' => 'Edit Research Material Type',
'update_item' => 'Update Research Material Type',
'add_new_item' => 'Add New Research Material Type',
'new_item_name' => 'New Research Material Type Name',
'add_or_remove_items' => 'Add or remove Research Material Types',
'choose_from_most_used' => 'Choose from most used sfmomatheme',
'menu_name' => 'Research Material Type',
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => false,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'research-material-type', array( 'research-materials' ), $args );
$terms = get_terms('research-material-type');
if( empty($terms) ){
wp_insert_term( 'Document', 'research-material-type', array(
'slug' => 'document',
) );
}
}
public static function register_custom_fields(){
acf_add_local_field_group(array(
'key' => 'group_research_materials',
'title' => 'Research Material Custom Fields',
'fields' => array(
array(
'key' => 'field_research_materials_slug',
'type' => 'text',
'name' => 'research_materials_slug',
'label' => 'Research Material Slug',
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork',
'type' => 'relationship',
'name' => 'research_materials_related_artwork',
'label' => 'Related Artwork',
'post_type' => array(
'artwork',
),
'max' => 1,
'required' => 1,
),
array(
'key' => 'field_research_materials_related_artwork_slug',
'type' => 'text',
'name' => 'research_materials_related_artwork_slug',
'label' => 'Related Artwork Slug',
'disabled' => 1,
'instructions' => 'Generated based on Related Artwork',
)
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'research-materials',
),
)
),
));
}
public static function validate_saved_value_in_custom_fields($valid, $value, $field, $input){
if( empty($value) && $field['name'] !== 'research_materials_related_artwork_slug' ){
$valid = 'Field must not be empty!';
}
return $valid;
}
public static function set_research_material_type($post_id){
// build main term by default term or by whatever's the first result set
$terms = wp_get_object_terms( strval($post_id), 'research-material-type' );
$default_term = get_term_by( 'slug', 'document', 'research-material-type' );
if( empty($terms) ){
wp_set_object_terms( strval($post_id), $default_term->term_id, 'research-material-type' );
}
else{
wp_set_object_terms( strval($post_id), $terms[0]->term_id, 'research-material-type' );
}
}
public static function build_query_vars($vars){
$vars[] = 'research_materials_related_artwork_slug';
$vars[] = 'research_materials_slug';
$vars[] = 'research_materials_type';
return $vars;
}
public static function update_related_artwork_slug($value, $post_id, $field){
if( $field['name'] === 'research_materials_related_artwork_slug' ){
$value = get_field('research_materials_related_artwork', $post_id)[0]->post_name;
}
return $value;
}
public static function pre_get_posts($query){
// check if the user is requesting an admin page
// or current query is not the main query
if ( is_admin() || !$query->is_main_query() ){
return;
}
$research_materials_related_artwork = $query->get('research_materials_related_artwork_slug');
error_log(print_r($research_materials_related_artwork, true));
$research_materials_slug = $query->get('research_materials_slug');
error_log(print_r($research_materials_slug, true));
$research_materials_type = $query->get('research_materials_type');
error_log(print_r($research_materials_type, true));
if( !empty($research_materials_related_artwork) && !empty($research_materials_slug) && !empty($research_materials_type) ){
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'research_materials_related_artwork_slug',
'value' => $research_materials_related_artwork,
'compare' => '=',
),
array(
'key' => 'research_materials_slug',
'value' => $research_materials_slug,
'compare' => '=',
)
);
$query->set('meta_query', $meta_query);
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'research-material-type',
'field' => 'slug',
'terms' => array($research_materials_type),
)
);
$query->set('tax_query', $tax_query);
}
return $query;
}
// needed because I also have a CPT called artwork
public static function point_artwork_and_artist_to_single_templates($templates = ''){
global $post;
if( $post->post_type == 'research-materials' ){
$templates = locate_template( 'single-research-materials.php' );
}
return $templates;
}
public static function rewrite_stuff(){
add_rewrite_tag( '%research_materials_related_artwork_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_slug%', '([^&]+)' );
add_rewrite_tag( '%research_materials_type%', '([^&]+)' );
add_rewrite_rule( 'artwork\/(.*)\/research-materials\/(.*)\/(.*)\/?$', 'index.php?post_type=research-materials&research_materials_related_artwork_slug=$matches[1]&research_materials_type=$matches[2]&research_materials_slug=$matches[3]', 'top' );
}
}
RRP::init();

Retrieving data from additional OptionTree pages

Adding OptionTree pages is simple with the code below, but does anyone know how to retrieve the stored data?
/**
* Hook to register admin pages
*/
add_action( 'init', 'register_options_pages' );
/**
* Registers all the required admin pages.
*/
function register_options_pages() {
// Only execute in admin & if OT is installed
if ( is_admin() && function_exists( 'ot_register_settings' ) ) {
// Register the pages
ot_register_settings(
array(
array(
'id' => 'custom_options',
'pages' => array(
array(
'id' => 'test_page',
'parent_slug' => 'options-general.php',
'page_title' => 'Test Page',
'menu_title' => 'Test Page',
'capability' => 'edit_theme_options',
'menu_slug' => 'test-page',
'icon_url' => null,
'position' => null,
'updated_message' => 'Test Page updated.',
'reset_message' => 'Test Page reset.',
'button_text' => 'Save Changes',
'show_buttons' => true,
'screen_icon' => 'options-general',
'contextual_help' => null,
'sections' => array(
array(
'id' => 'test_section',
'title' => __( 'Test Section', 'motif-core' )
)
),
'settings' => array(
array(
'id' => 'test_section_input',
'label' => 'Test Input',
'desc' => 'Pretty freaking awesome!',
'std' => '',
'type' => 'text',
'section' => 'test_section',
'class' => ''
)
)
)
)
)
)
);
I tried this $my_plugin_options = get_option('custom_options'); but it only shows the word 'array' on the front end?
This is how to retrieve the stored data:
$my_plugin_options = get_option('custom_options');
echo $my_plugin_options['test_section_input'];

Get value from option tree wordpress

I want to be use some sliders in my wordpress theme. I want to select them in my theme option. I am using the code below.
<?php
$slider_select = get_option_tree( 'slider_select', '', 'true' );
?>
<?php get_template_part('$slider_select'); ?>
But, it is not working. I want the get_template_part code worked. Any suggestion?
replace
get_template_part('$slider_select');
with
get_template_part($slider_select);
You can use this method for create a slider in option tree.
create settings array like this.
array(
'id' => 'my_slider',
'label' => 'Images',
'desc' => '',
'std' => '',
'type' => 'list-item',
'section' => 'general',
'class' => '',
'choices' => array(),
'settings' => array(
array(
'id' => 'slider_image',
'label' => 'Image',
'desc' => '',
'std' => '',
'type' => 'upload',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_link',
'label' => 'Link to Post',
'desc' => 'Enter the posts url.',
'std' => '',
'type' => 'text',
'class' => '',
'choices' => array()
),
array(
'id' => 'slider_description',
'label' => 'Description',
'desc' => 'This text is used to add fancy captions in the slider.',
'std' => '',
'type' => 'textarea',
'class' => '',
'choices' => array()
)
)
)
Add into page using loop
$my_slider = ot_get_option( 'my_slider' );
foreach($my_slider as $slider){
echo '<img src="'.$slider["slider_image"].'">';
echo $slider["slider_link"];
echo $slider["slider_description"];
}

Resources