I need to create as many dropdowns as values from a database table query in worpress. I can't get post data to usermeta table from foreach dropdown list . This is the code:
$custom_posts_type = $wpdb->get_results("SELECT label,id FROM $table_name");
foreach ($custom_posts_type as $custom_post_type) {
$custom_post_type_name = $custom_post_type->label;
$custom_post_type_id = $custom_post_type->id;
?>
<p>
<label for="dropdown">Permisos de usuario para <?php echo $custom_post_type_name;?>: </label>
<?php
$user_custom_post = 'user_custom_' . $custom_post_type_name;
$selected = get_the_author_meta( $user_custom_post, $user->ID );
$post_type_object = get_post_type_object($custom_post_type_name);
$label = $post_type_object->label;
$posts = get_posts(array('post_type'=> $custom_post_type_name, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
echo '<select name="' . $user_custom_post . '" id="' . $user_custom_post . '">';
?>
<option value="all" <?php selected( $user_custom_post[0], "all" ); ?>>All <?php echo $label;?></option>
<?php
foreach ($posts as $post) {
echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>';
}?>
<?php
echo '</select>';
?>
</p>
<?php
update_user_meta( $user->ID, $user_custom_post, $_POST[$user_custom_post] );
} //endforeach
I appreciate your help.
Thks
Probably... I have created two functions without foreach. And, if I don't use variables it works:
add_action( 'show_user_profile', 'pglr_user_pages' );
add_action( 'edit_user_profile', 'pglr_user_pages' );
add_action( 'user_register', 'pglr_user_pages' );
function pglr_user_pages( $user ) {
$post_type = "page"; ?>
<p>
<label for="dropdown">Asignar <?php echo $post_type;?>: </label>
<?php
$selected = get_the_author_meta( 'user_custom_page', $user->ID );
$post_type_object = get_post_type_object($post_type);
$label = $post_type_object->label;
$posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
echo '<select name="user_custom_page_value" id="user_custom_page_value">';
?>
<option value="all" <?php selected( '', "all" ); ?>>All <?php echo $label;?></option>
<?php
foreach ($posts as $post) {
echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>';
}?>
<?php
echo '</select>';
?>
</p>
<?php
}
add_action( 'personal_options_update', 'save_pglr_user_pages' );
add_action( 'edit_user_profile_update', 'save_pglr_user_pages' );
function save_pglr_user_pages( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
//save dropdown
update_usermeta( $user_id, 'user_custom_page', $_POST['user_custom_page_value'] );
}
However, this code doesn't work:
add_action( 'show_user_profile', 'pglr_user_pages' );
add_action( 'edit_user_profile', 'pglr_user_pages' );
add_action( 'user_register', 'pglr_user_pages' );
function pglr_user_pages( $user ) {
$post_type = "page"; ?>
<p>
<label for="dropdown">Asignar <?php echo $post_type;?>: </label>
<?php
**global $user_custom_post;**
**$user_custom_post = 'user_custom_' . $post_type;**
$selected = get_the_author_meta( **$user_custom_post**, $user->ID );
$post_type_object = get_post_type_object($post_type);
$label = $post_type_object->label;
$posts = get_posts(array('post_type'=> $post_type, 'post_status'=> 'publish', 'suppress_filters' => false, 'posts_per_page'=>-1));
echo '<select name="user_custom_page_value" id="user_custom_page_value">';
?>
<option value="all" <?php selected( '', "all" ); ?>>All <?php echo $label;?></option>
<?php
foreach ($posts as $post) {
echo '<option value="', $post->ID, '"', $selected == $post->ID ? ' selected="selected"' : '', '>', $post->post_title, '</option>';
}?>
<?php
echo '</select>';
?>
</p>
<?php
}
add_action( 'personal_options_update', 'save_pglr_user_pages' );
add_action( 'edit_user_profile_update', 'save_pglr_user_pages' );
function save_pglr_user_pages( $user_id ) {
if ( !current_user_can( 'edit_user', $user_id ) )
return false;
//save dropdown
update_usermeta( $user_id, **$user_custom_post,** $_POST['user_custom_page_value'] );
}
The problem may be using variables...
Related
I am developing a plugin where I need to display some custom select product. So far I can able to make the option field but how can i save them as option field with comma separated product ids like.
45,78,55,48,
here is an example of searchable multiple select option for WooCommerce product.
Here is my code
function crp_select_products() {
global $post, $woocommerce;
$product_ids = array();
?>
<div class="options_group">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field">
<label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
}
First, there is something missing in your function, to display the saved data in it.
After, this special field need to be displayed inside a form that will have a submit button. So it depends where you are using your function.
Here below is an example displaying that custom field as a custom product setting, save the data and display the saved data in it:
function crp_get_product_related_ids() {
global $post, $woocommerce;
$product_ids = get_post_meta( $post->ID, '_related_ids', true );
if( empty($product_ids) )
$product_ids = array();
?>
<div class="options_group">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field">
<label for="related_ids"><?php _e( 'Search Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="related_ids" name="related_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <?php echo wc_help_tip( __( 'Select products are for sale product.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
}
add_action( 'woocommerce_product_options_general_product_data', 'add_custom_fied_in_product_general_fields', 20 );
function add_custom_fied_in_product_general_fields() {
global $post, $woocommerce;
crp_get_product_related_ids();
}
add_action( 'woocommerce_process_product_meta', 'process_product_meta_custom_fied', 20, 1 );
function process_product_meta_custom_fied( $product_id ){
if( isset( $_POST['crosssell_ids'] ) ){
update_post_meta( $product_id, '_related_ids', array_map( 'intval', (array) wp_unslash( $_POST['related_ids'] ) ) );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
im trying to make this work. its theme option page that list categories and allow you to do MULTIPLE select
but something is wrong and it not saving the values and in the end how can i show these selected categories in index?
here is what i found in web and sof
<?php
add_action( 'admin_init', 'theme_options_init' );
add_action( 'admin_menu', 'theme_options_add_page' );
function theme_options_init(){
register_setting( 'sample_options', 'sample_theme_options', 'theme_options_validate' );
}
function theme_options_add_page() {
add_theme_page( __( 'Theme Options', 'sampletheme' ), __( 'Theme Options', 'sampletheme' ), 'edit_theme_options', 'theme_options', 'theme_options_do_page' );
}
function theme_options_do_page() {
global $select_options, $radio_options;
if ( ! isset( $_REQUEST['settings-updated'] ) )
$_REQUEST['settings-updated'] = false;
?>
<div class="wrap">
<?php screen_icon(); echo "<h2>" . get_current_theme() . __( ' Theme Options', 'sampletheme' ) . "</h2>"; ?>
<?php if ( false !== $_REQUEST['settings-updated'] ) : ?>
<div class="updated fade"><p><strong><?php _e( 'Options saved', 'sampletheme' ); ?></strong></p></div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields( 'sample_options' ); ?>
<?php $options = get_option( 'sample_theme_options' ); ?>
<select multiple="multiple" name="site_options[categorychoice][]">
<?php $option = get_option('site_options'); ?>
<?php
$args = array(
'orderby' => 'name',
'parent' => 0,
'exclude' => 1
);
$categories = get_categories( $args );
foreach ($categories as $category) { ?>
<?php $selected = in_array( $category->cat_ID, $option['categorychoice'] ) ? ' selected="selected" ' : ''; ?>
<option value="<?php echo $category->term_id; ?>" <?php echo $selected; ?> >
<?php echo $category->cat_name . ' (' . $category->category_count .')'; ?>
</option>
<?php } //endforeach ?>
</select>
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e( 'Save Options', 'sampletheme' ); ?>" />
</p>
</form>
</div>
<?php
}
function theme_options_validate( $input ) {
global $select_options, $radio_options, $categories;
if ( ! array_key_exists( $input['categorychoice'], $categories ) )
$input['categorychoice'] = NULL;
return $input;
}
I am trying to display the products/items within a WooCommerce order using the following php, but the items are not being shown.
The code I am using is an adaptation of this: Get cart item name, quantity all details woocommerce
Using the original code from the question above also does not display anything on my page.
Thanks in advance.
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
//product image
$getProductDetail = wc_get_product( $values['product_id'] );
echo '<tr><td>';
echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
echo '</td>';
echo '<td>';
echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';
echo '</td></tr>';
};
?>
The full page code is here:
<?php
/*
Template Name: Store
*/
if (!is_user_logged_in() || !current_user_can('manage_options')) wp_die('This page is private.');
?>
<!DOCTYPE HTML>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>
<?php _e('Store Queue'); ?>
</title>
</head>
<body id="driverqueue">
<header>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; endif; ?>
</header>
<section>
<?php
global $woocommerce;
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-processing',
'meta_key' => '_customer_user',
'posts_per_page' => '-1'
);
$my_query = new WP_Query($args);
$customer_orders = $my_query->posts;
foreach ($customer_orders as $customer_order) {
$order = new WC_Order();
$order->populate($customer_order);
$orderdata = (array) $order;
// $orderdata Array will have Information. for e.g Shippin firstname, Lastname, Address ... and MUCH more.... Just enjoy!
}
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$order_id = $loop->post->ID;
$order = new WC_Order($order_id);
?>
<table class="ordertable" id="<?php echo $order_id; ?>">
<tr>
<td>
<p>Order #
<?php echo $order_id; ?> —
<time datetime="<?php the_time('c'); ?>">
<?php echo the_time('d/m/Y g:i:s A'); ?>
</time>
</p>
</td>
<td>
<?php echo $order->billing_first_name . ' ' . $order->billing_last_name ?>
</td>
<td>
<table>
<tr>
<?php
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
//product image
$getProductDetail = wc_get_product( $values['product_id'] );
echo '<tr><td>';
echo $getProductDetail->get_image(); // accepts 2 arguments ( size, attr )
echo '</td>';
echo '<td>';
echo '<p style="font-size:10pt;">'.$_product->post_title.'</p><td><p style="font-size:10pt;">x'. $values['quantity'] . '</p></td>';
echo '</td></tr>';
};
?>
</tr>
</table>
</td>
<td>
<p>
<?php
do_action( 'woocommerce_admin_order_actions_start', $order );
$actions = array();
if ( $order->has_status( array( 'pending', 'on-hold', 'processing' ) ) ) {
$actions['complete'] = array(
'url' => wp_nonce_url( admin_url( 'admin-ajax.php?action=woocommerce_mark_order_status&status=awaiting-shipment&order_id=' . $post->ID ), 'woocommerce-mark-order-status' ),
'name' => __( 'Complete', 'woocommerce' ),
'action' => "complete"
);
};
$actions = apply_filters( 'woocommerce_admin_order_actions', $actions, $order );
foreach ( $actions as $action ) {
printf( '<a class="button %s" href="%s" data-tip="%s">%s</a>', esc_attr( $action['action'] ), esc_url( $action['url'] ), esc_attr( $action['name'] ), esc_attr( $action['name'] ) );
}
do_action( 'woocommerce_admin_order_actions_end', $order );
?>
</p>
</td>
<td>
<form action="">
<input type="checkbox" class="ordercollected" value="0" />
</form>
</td>
</tr>
<?php endwhile; ?>
</section>
</body>
</html>
Solved this using info from the following link with the adapted code below:
https://wordpress.stackexchange.com/questions/180075/how-to-get-woocommerce-order-product-info
<?php
foreach ($order->get_items() as $key => $lineItem) {
//uncomment the following to see the full data
// echo '<pre>';
// print_r($lineItem);
// echo '</pre>';
$product_id = $lineItem['product_id'];
$product = wc_get_product( $product_id );
echo '<tr><td>' . $product->get_image() . '</td>'; // accepts 2 arguments ( size, attr )
echo '<td>' . 'Product: ' . $lineItem['name'] . '</td>';
echo '<td> x' . $lineItem['qty'] . '</td></tr>';
}
?>
I'm trying to figure out on how to populate my custom ordering inside my custom template page.
Here is my work in progress. So far the output shows a select box with empty values. When I check the view source i saw this error.
Warning: Invalid argument supplied for foreach() in C:\Users\Noel\Desktop\xampp\htdocs\lumiere\wp-content\themes\klasik\lumiere-products-page.php on line 23
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => 'Lumiere', 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php //Woocommerce Custom Ordering ?>
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php foreach ( $catalog_orderby_options as $id => $name ) : ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>><?php echo esc_html( $name ); ?></option>
<?php endforeach; ?>
</select>
<?php
// Keep query string vars intact
foreach ( $_GET as $key => $val ) {
if ( 'orderby' === $key || 'submit' === $key ) {
continue;
}
if ( is_array( $val ) ) {
foreach( $val as $innerVal ) {
echo '<input type="hidden" name="' . esc_attr( $key ) . '[]" value="' . esc_attr( $innerVal ) . '" />';
}
} else {
echo '<input type="hidden" name="' . esc_attr( $key ) . '" value="' . esc_attr( $val ) . '" />';
}
}
?>
</form>
<li class="product product-items ">
<div class="product-item">
<?php woocommerce_show_product_sale_flash( $post, $product ); ?>
<div class="product-thumbnail">
<?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" />'; ?>
</div>
<div class="product-info">
<h3>
<?php the_title(); ?>
</h3>
<h4 class="product-category-title">
<?php
$size = sizeof( get_the_terms( $post->ID, 'product_cat' ) );
echo $product->get_categories( ', ', '<span class="posted_in">' . ' ', '.</span>' );
?>
</h4>
<?php echo $product->get_sku(); ?>
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
<?php if ( $price_html = $product->get_price_html() ) : ?>
<span class="price"><?php echo $price_html; ?></span>
<?php endif; ?>
<div class="product-rating">
<?php if ($average = $product->get_average_rating()) : ?>
<?php echo '<div class="star-rating" title="'.sprintf(__( 'Rated %s out of 5', 'woocommerce' ), $average).'"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>'; ?>
<?php endif; ?>
</div>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</div>
</div>
</li>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</ul><!--/.products-->
I've created a drop down selection on a front end user profile which includes all custom post type posts.
On selecting it doesn't actually save the selection, it just reverts back to the first option.
Where am I going wrong?
This is the code I have in my functions.php file:
add_action( 'personal_options_update', 'save_custom_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' );
function save_custom_profile_fields( $user_id ) {
update_user_meta( $user_id, 'teampage', $_POST['teampage'], get_user_meta( $user_id, 'teampage', true ) );
}
add_action( 'personal_options', 'add_profile_options');
function add_profile_options( $profileuser ) {
$greeting = get_user_meta($profileuser->ID, 'teampage', true);
?><tr>
<th scope="row">Member of which Health Board?</th>
<td>
<select name="teampage" id="teampage" >
<?php $portfolioloop = new WP_Query( array(
'post_type' => 'board',
'post_status' => 'publish'
)); ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<option id="Yes" <?php selected( $profileuser->teampage, 'Yes' ); ?>><?php echo the_title(); ?></option>
<?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>
</select>
</td>
</tr><?php
}
I'm using this tutorial.
I found out what your problem is. It's a couple of little mistakes that you made. Here is the working code:
add_action( 'personal_options_update', 'save_custom_profile_fields' );
add_action( 'edit_user_profile_update', 'save_custom_profile_fields' );
function save_custom_profile_fields( $user_id ) {
update_user_meta( $user_id, 'teampage', $_POST['teampage'], get_user_meta( $user_id, 'teampage', true ) );
}
add_action( 'personal_options', 'add_profile_options');
function add_profile_options( $profileuser ) {
$greeting = get_user_meta($profileuser->ID, 'teampage', true);
?><tr>
<th scope="row">Member of which Health Board?</th>
<td>
<select name="teampage" id="teampage" >
<?php $portfolioloop = new WP_Query( array(
'post_type' => 'board',
'post_status' => 'publish'
));
global $post; ?>
<?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
<option <?php selected( $profileuser->teampage, $post->ID ); ?> value="<?php echo $post->ID; ?>"><?php echo the_title(); ?></option>
<?php endwhile; wp_reset_query(); wp_reset_postdata(); ?>
</select>
</td>
</tr><?php
}
You forgot to add the value attribute to each option.
Also you shouldn't check against the same value for all options(you used selected( $profileuser->teampage, 'Yes' );, which essentially checks whether the value of $profileuser->teampage is Yes). Instead we assign the post ID to each option and we check against that.