city
columns
id
name
province_id
Relation
Province
one to many
Province
columns
id
province_name
I want to display the string value instead of foreign id.So far
<?php foreach($results as $r): ?>
<tr>
<td><?php echo $r['name'] ?></td>
<td><?php echo $r['province_id']?></td>
</tr>
<?php endforeach ?>
</tbody>
displays
name province
Kansas 1(province_id)
I want instead
name province
KANSAS SOUTH PROVINCE
in tableClass
public function SearchCity($name)
{
return $this->createQuery('c')
->andWhere('c.name like ?', '%'.$name.'%')
->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
}
my problem is I want to echo province_name instead of province_id
Any Idea on how to do this?
Just modify your tableClass query like this
public function SearchCity($name)
{
return $this->createQuery('c')
->select('c.*')
->innerJoin('c.Province p')
->addSelect('p.name as provincename')
->andWhere('c.name like ?', '%'.$name.'%')
->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
}
And now you can easily display string values instead of foreign ids
<td><?php echo $r['name'] ?></td>
<td><?php echo $r['provincename'] ?></td>
Related
I created a custom fee on Woocommerce based on a user plan. I did something like this on my functions.php:
function my_discount_based_on_user_membership( $cart ) {
$discount = 10;
$cart->add_fee( "My Discount", -$discount );
}
add_action( 'woocommerce_cart_calculate_fees', 'my_discount_based_on_user_membership', 20, 1 );
Let's say that this fee has the value of -$10. I want that -$10 have another color, like red for example. Is there a way of set a CSS class for a custom fee in Woocommerce? How can I target this element?
Since I could have another discounts later, target the element position using JS or CSS probably is not a good idea. Any advice?
To give a unique style to a fee in WooCommerce
Use CSS
.fee bdi {
color: red;
}
OR
You could overwrite the cart/cart-totals.php template file
This template can be overridden copying it to yourtheme/woocommerce/cart/cart-totals.php.
Replace (line 61 #version 2.3.6)
<td data-title="<?php echo esc_attr( $fee->name ); ?>"><?php wc_cart_totals_fee_html( $fee ); ?></td>
With
<td style="color:red;" data-title="<?php echo esc_attr( $fee->name ); ?>"><?php wc_cart_totals_fee_html( $fee ); ?></td>
AND
the checkout/review-order.php file
This template can be overridden by copying it to yourtheme/woocommerce/checkout/review-order.php.
Replace (line 80 #version 5.2.0)
<td><?php wc_cart_totals_fee_html( $fee ); ?></td>
With
<td style="color:red;"><?php wc_cart_totals_fee_html( $fee ); ?></td>
Note: instead of using inline CSS,
when overwriting the template files. You can also add a custom class instead <td class="my-class"...
I need to add a users from WordPress backend. So, I want to generate unique User IDs for them. But I can achieve it. Can somebody help me out on this.
Here is what I cam so far. It is better if I could append the WordPress default generated unique user id to end of my field.
// Customize the user fields
function custom_user_profile_fields($user) {
$rand = randomGen(1000,5000,1);
$auth_Token = '2021-ABCD-'.$rand[0];
?>
<h3>Profile Information</h3>
<table class="form-table">
<tr>
<th><label for="srn">Student Registration Number</label></th>
<td>
<input type="text" class="regular-text" name="srn" value="<?php echo (get_option('srn') ? esc_attr( get_the_author_meta( 'srn', $user->ID ) ) : $auth_Token); ?>" id="srn" readonly /><br />
</td>
</tr>
</table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );
function save_custom_user_profile_fields($user_id) {
# again do this only if you can
if(!current_user_can('manage_options'))
return false;
# save my custom field
update_usermeta($user_id, 'srn', $_POST['srn']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');
When a user added I can't get the previously saved Index Number to the field. It's generating another and showing again. As I am not an expert for this, could anybody help me out with this?
Thanks in advance
You were on the right path to solve the problem, but you made some mistakes.
You can use the working code below:
Creation of fields
add_action( 'user_new_form', 'bks_add_student_reg_number_field' );
add_action( 'edit_user_profile', 'bks_add_student_reg_number_field' );
add_action( 'show_user_profile', 'bks_add_student_reg_number_field' );
function bks_add_student_reg_number_field( $user ) {
?>
<h3><?php _e("Extra profile information", "blank"); ?></h3>
<table class="form-table">
<tr>
<th><label for="student_reg_number"><?php _e("Student Registration Number"); ?></label></th>
<td>
<?php if (is_object($user)) { ?>
<input type="text" name="student_reg_number" id="student_reg_number" value="<?php echo esc_attr( get_the_author_meta( 'student_reg_number', $user->ID )); ?>" class="regular-text" readonly disabled="disabled"/><br />
<?php } else { ?>
<input type="text" name="student_reg_number" id="student_reg_number" class="regular-text" /><br />
<span class="description"><?php _e("Please enter Student registration number."); ?></span>
<?php } ?>
</td>
</tr>
</table>
<?php }
This creates a field option at new user generation and edit form.
The field is only enabled when a new user is created. If the registered user has already have a value the value would be shown with no access to change it.
Save field value
add_action( 'user_register', 'bks_student_reg_number_field' );
function bks_student_reg_number_field($user_id) {
// You can maybe add checks here whch would determine if the users role is customer
// or not or maybe validate the number.
if(!current_user_can('manage_options'))
return false;
$reg_value = get_user_meta($user_id, 'student_reg_number', true);
if (isset( $_POST['student_reg_number']) && $_POST['student_reg_number'] !== $reg_value) {
update_user_meta($user_id, 'student_reg_number', $_POST['student_reg_number'].'-'.$user_id);
}
}
Only user_register hook is used to prevent updation on profile edit.
You can see the user_id appended with a - infront of it in the field and the field is readonly and disabled.
If you want user to be able to edit the id after registering as well then. do the following.
Add profile_update hook. add_action( 'profile_update', 'bks_student_reg_number_field' ); as well so that the code runs when you update the user as well.
Remove readonly and disabled attribute from the form.
The code is TESTED and WORKS.
i have built several diffrent templates for my WP site each calling diffrent metas.
ok, im looking for a way to add a meta-box that will only display if the correct page template is selected.
say i have a homepage.php template, this template calls some specific meta info that only the homepage uses, a call to action of example, so i need the meta-box to appear once the proper page template is selected and disappear if a different page template is selected.
any help is appreciated.
get_page_template will return the template you are looking at. You can switch on this to display the content you want.
ok i finally found a way around it guys and here it is:
<?php
function my_admin() {
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
// check for a template type
if ($template_file == 'home.php'){
add_meta_box( 'product_info',
'Informacion de Producto',
'display_product_info_meta_box',
'ex_producto', 'normal', 'high'
);
}
}
function display_product_info_meta_box( $product_info ) {
$product_price = floatval( get_post_meta( $product_info->ID, 'product_price', true ) );
$product_code = esc_html( get_post_meta( $product_info->ID, 'product_code', true ) );
?>
<table>
<tr style="width: 100%">
<td >Precio Producto</td>
<td><input type="text" size="80" name="product_info_product_price" value="<?php echo $product_price; ?>" /></td>
</tr>
<tr style="width: 100%">
<td >Codigo de Producto</td>
<td><input type="text" size="80" name="product_info_product_code" value="<?php echo $product_code; ?>" /></td>
</tr>
</table>
<?php
}
add_action( 'admin_init', 'my_admin' );
?>
so... yeap i hope that dose someone out there some good :)
How to query Post with current date month with custom field:
Here is my code
<?php
global $wp_query;
$event_month = get_post_meta($wp_query->post->ID, 'eventdate', true); //format in db 11/15/2010
$event_month = date("n"); //A numeric representation of a month, without leading zeros (1 to 12)
$today= getdate(); ?>
<?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>
<?php while (have_posts()): the_post(); ?>
<div class="event-list-txt">
<h4><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>" rel="bookmark">
<?php the_title(); ?>
</a></h4>
<?php the_content(); //Display the content?>
</div>
<?php endwhile; ?>
<?php wp_reset_query();?>
but it doesn't display the output.. .what I want to display is the event list for the month.. for example if it's November it will display the event list for November and when the month of December comes it will show the event list of December and so on.. . The event list date comes with custom field format like this 11/15/2010 compare to current date
i'm stack on it.. .thanks guys
here are the additional expected output
current date is November and the event list should be like this:
+-----------------------+-------------+
| Event Name | Date |
+-----------------------+-------------+
| ABC Market opening | 11/18/2010 |
| ABC Market Seminar | 11/25/2010 |
| ABC Market Promo | 11/29/2010 |
+-----------------------+-------------+
I think your problem is here:
<?php query_posts('meta_key='.$event_month .'&meta_compare&meta_value=' .$today["mon"]);?>
...You're trying to find a custom field named "11" (Numerical representation of November) with value 11.
Check http://codex.wordpress.org/Function_Reference/query_posts#Time_Parameters for a code snippet that allows you to set the "WHERE" part of the query:
Return posts for posts for March 1 to March 15, 2009:
<?php
//Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
//posts for March 1 to March 15, 2009
$where .= " AND post_date >= '2009-03-01' AND post_date < '2009-03-16'";
return $where;
}
// Register the filtering function
add_filter('posts_where', 'filter_where');
// Perform the query, the filter will be applied automatically
query_posts($query_string);
?>
Hope this helps at all...
I have created a menu to manage user records for wordpress admin. While listing the record I have used check box to select and unselect the record as in Pages menu.
I have to select whole address or name on checkbox click. But here what happens is that when I click on name it selects whole checkbox including name and address. I do not have an idea how the check box works.
Here is the code
<?php
foreach($record as $record_row)
{
$name = $record_row->name;
$address = $record_row->name;
?>
<tr>
<th class="check-column" scope="row">
<input name="names[]" class="one" type="checkbox" value="<?php echo $record_row->ID; ?>">
</th>
<th class="check-column" scope="row">
<input name="address[]" class="two" type="checkbox" id="address[]" value="<?php echo $record_row->ID; ?>">
</th>
<td class="column-icon media-icon" width="30%"><?php echo $record_row->name; ?></td>
<td class="post-title page-title column-title"><?php echo $record_row->name; ?></td>
</tr>
<?php
}?>
Can anyone help me. Thanks in advance.
Get rid of the id attribute on the checkbox - it's invalid syntax for starters, and every id in your document must be unique!