I'm building an online store and I have a private page where admins can scan a QR code and it displays the order details of the order assigned to that QR code. Admins can also set the status of the order on that page. I managed to display order notes in that page too. I also would like to add a form (textarea + submit button) to add a order note to the order, because our online store is about services, and I want to be able to put a note when the order is partially redeemed, so we don't have to go to the desktop -> orders -> the order and add the note. Could somebody help me? I would need a code to directly add it where I want it, I mean, not in functions.php, a code for the custom template of the page (page-scanqr.php). Any help would be appreciated!
EDIT: I saw this and I thought that maybe it could help me somehow, but I don't know how to apply it so it runs after pressing the submit button and sends the textarea text as a new order note: https://stackoverflow.com/a/49508808/12459095
As I say, I want to apply somehow this function. But I want to run it after pressing the button "submit" and I want it to pick the text from the text area and send it to wordpress as a new order note.
function add_redeeming_notes( $order_id ) {
$order = new WC_Order( $order_id );
// The text for the note
$note = __("I WANT THIS TO PICK THE TEXT FROM THE TEXTAREA");
// Add the note
$order->add_order_note( $note );
EDIT 2: I also show you the code that allows me to display the orders note, in case it could be helpful.
<?php
$order_notes = get_private_order_notes( $order_id );
foreach($order_notes as $note){
$note_id = $note['note_id'];
$note_date = $note['note_date'];
$note_author = $note['note_author'];
$note_content = $note['note_content'];
// Outputting each note content for the order
echo '<p>'.$note_date.' - '.$note_content.'</p>';
} ?>
Add the follows codes snippet in your custom page template. or you can replace follows code in your template form structure -
<?php
if ( isset( $_POST['order_note_nonce_field'] ) && wp_verify_nonce( $_POST['order_note_nonce_field'], 'order_note_action' ) ) {
$order_id = ( isset( $_POST['order_id'] ) && $_POST['order_id'] ) ? absint( $_POST['order_id'] ) : '';
$order_note = ( isset( $_POST['order_note'] ) && $_POST['order_note'] ) ? sanitize_textarea_field( $_POST['order_note'] ) : '';
$order = wc_get_order( $order_id );
if( $order && $order_note ) {
// Add the note
$order->add_order_note( $order_note );
}
}
?>
<form action="" method="post">
<?php wp_nonce_field( 'order_note_action', 'order_note_nonce_field' ); ?>
<input type="hidden" name="order_id" value="21" /> <!-- dont forgot to replace the value with your current order id -->
<div class="form-field">
<textarea name="order_note"></textarea>
</div>
<div class="form-field">
<input type="submit" value="Add Note" />
</div>
</form>
Here is some psuedo code from my comment above:
if (isset($_POST['your_submit_action)) {
update_post_meta($id_of_product, 'your_text_key', $_POST['your_text_field'];
}
That right there will store it. To get it, do this:
$text = get_post_meta($id_of_product, 'your_text_key', TRUE);
Hope that helps.
EDIT:*
WC stores the order notes in the 'comments' table. The orders are post types themselves of 'shop_order'. Simply insert shop_order notes into the comments table as necessary using the order ID set to the 'comment_post_ID' field in the 'comments' table. Done and done.
Related
I am trying to have a profile page where a user can add certain fields over and over on a custom fields form.
The fields will be represented in the UI like:
Input Field (Name) | Input Field (Number) + (Add new)
So when the user clicks on Add new, the fields above are duplicated like below:
Input Field (Name) | Input Field (Number)
Input Field (Name) | Input Field (Number)
I read that custom Fields is what I need and tried putting the block of code below in my themes functions.php file:
// handle custom meta boxes for the user progile page
add_action('add_meta_boxes', 'register_new_user_field');
function register_new_user_field(){
global $post;
if(!empty($post))
{
$pageTemplate = get_post_meta($post->ID, '_wp_page_template', true);
if($pageTemplate == 'page-templates/template-dashboard.php' ){
add_meta_box(
'custom-user-post-id',
'Label',
'customer_submit_callback',
'page',
'normal',
'high'
);
}
}
}
function customer_submit_callback(){
// display output to user
echo '<label for="custom-user-post-id"><p>Custom Title</p></label>';
echo '<input name="custom-user-post-id" id="custom-user-post-id" cols="62" rows="5" ></input>';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
}
function wpdocs_save_meta_box($post_id){
// Save logic
// // if our nonce isn't there, or we can't verify it, bail
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
if( isset( $_POST['custom-user-post-id'] ) )
update_post_meta( $post_id, 'custom-user-post-id', $_POST['custom-user-post-id'] );
}
add_action( 'save_post', 'wpdocs_save_meta_box' );
However, those fields aren't present when i go to the user profile page. any helps would be great. Thanks.
So what you're looking to do is add sections on a page that the user can edit at their leisure.
Step 1. Download Advanced Custom Fields
Step 2. Create A Field Group
Step 3. Click Add Field
Step 4. Set the field label to: Custom Post ID
Step 5. Set the field name to custom_post_id
Step 6. Select the type Text
Step 7. Scroll down to Options
Step 8. Select "Side" From the positions drop down
Step 9. Click Save/Publish/Update
To restrict where the meta box appears, in the location field select:
Page Template > Is Equal To > Your Template
Now the box will appear with all the other metaboxes on all pages.
In order to make the ID actually useful I am assuming you will be echoing it out somewhere so here is how you would do that:
<?php the_field('custom_post_id');?>
An example of an actual case of using it:
<div class="wrapper wrapper-<?php the_field('custom_post_id');?>">
If the user entered "1" in the field in the admin area it would display this:
<div class="wrapper wrapper-1">
I'm attempting to save a simple text input to the WooCommerce session. The session is created when a user adds something to their cart.
My input field exists in custom page template that will be placed in the user flow after the cart but before the checkout: cart > my template > checkout.
So far
Simple form to capture data (custom template file)
<form name="group" method="post" class="checkout woocommerce-checkout" action="http://localhost:/site.dev/my-template">
<div class="group-order">
<p class="form-row form-row woocommerce-validated" id="create_new_group_field">
<label for="create_new_group" class="">Join an existing group</label>
<input type="text" class="input-text " name="create_new_group" id="create_new_group">
</p>
</div>
</form>
Receiving and setting data (I'm having trouble figuring out when/how to run this. in my custom page)
UPDATE
I've added the code below to the top of my page template so the page processes itself and then re-directs to the checkout.
function set_and_save_input_to_session() {
if( !is_admin( ) ) {
// User input
if( ! empty( $_POST['create_new_group'] ) ) {
$group_input_value = $_POST['create_new_group'];
// Set session and save data
WC()->session->set( 'group_order_data', $group_input_value );
wp_redirect( 'http://localhost:28/site.dev/checkout' );
exit();
}
}
get_header();
add_action('woocommerce_checkout_process', 'set_and_save_input_to_session');
Retrieving and saving data
function retrieve_and_save_group_input_value_to_order_meta() {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
I'm currently working my way through what are to me, more complex solutions and therefore I'd appreciate if anyone could point out any major flaws with my process so far.
UPDATE
I can confirm that the form is receiving data and that the WC()->session->set is setting data. (Thanks to #Firefog for suggesting the use the $_SESSION global)
After further investigation and finding the right place to var_dump the session data I found that the data was being set to the session with my original method.
The data is set, but I can't see why the data won't save to the order.
It's more saying Thank you for solving my problem. But here is an answer, too:
The post meta could not been updated because there is no $order_id parameter in your callback function. This should do the trick:
function retrieve_and_save_group_input_value_to_order_meta( $order_id ) {
$retrived_group_input_value = WC()->session->get( 'group_order_data' );
update_post_meta( $order_id, '_create_new_group', $retrived_group_input_value );
}
add_action('woocommerce_checkout_update_order_meta', 'retrieve_and_save_group_input_value_to_order_meta');
Here is another approach.
1st page:
session_start();//place this at the top of all code
$data = $_POST['create_new_group'];
$_SESSION['custom_create_new_group']=$data;
Now in another page write the following to receive the value:
session_start(); //optional
$retrive_price = $_SESSION['custom_create_new_group'];
I don't know if this is the correct place to ask, but I'll try. I have an ongoing project with Wordpress (Genesis framework - Agentpress PRO theme) working with Ninja Forms, and am having a slight problem. The web page is: http://www.supercastor.net/wordpress/listings/castellon-65-m2-ytong/
I have a listings page (for each house model). Each listing has metadata that is accessible via a simple function from the genesis framework:
genesis_get_custom_field( 'CUSTOM_FIELD_NAME' );
I would like to have, like the link above has, a list of checkboxes that the user can select/deselect, and the TOTAL value is finally calculated. Everything is working, except that I cannot assign the value of this "custom field" that is from the listing.
I am using Ninja Forms, and after some research, I have come up with the solution of registering a custom field in NF:
function precio_basico_calc_display( $field_id, $data ){
// Get the default_value
if( isset( $data['default_value'] ) ){
$def_value_new = genesis_get_custom_field( '_listing_precio_sort' );
$data['default_value'] = $def_value_new;
$default_value = $data['default_value'];
debug_to_console("Default_value is set to:$default_value");
}else{
$default_value = '';
debug_to_console("Default_value NOT SET");
}
$products_select = genesis_get_custom_field( '_listing_precio_sort' );
}
// Now that $products_select is populated with the listing_price, output a checkbox with the value of the price. ?>
<input type="checkbox" name="ninja_forms_field_<?php echo $field_id;?>" value="<?php echo $products_select;?>" checked="checked" class="ninja-forms-field ninja-forms-field-calc-listen" disabled="disabled">
<?php
}
I also have a small "debug_to_console" function, but this is just as a quick "debugging". I can see that the value is there, but in the calculation it is not set. I have set the default to 40.000, but it should be set to the value for each price (in the link example, should be 19.000). As:
$products_select = genesis_get_custom_field( '_listing_precio_sort' );
And I have also tried the following (field_id 33 is the checkbox in the form):
add_filter( 'ninja_forms_field', 'my_filter_function', 10, 2 );
function my_filter_function( $data, $field_id ){
if( $field_id == 33 ){
$listing_price = genesis_get_custom_field( '_listing_precio_sort' );
$data['default_value'] = $listing_price;
}
return $data;
}
But it isn't working either. I mean, it does display the value (in the console), but not making the calculation correctly.
Help! I just want the calculation (TOTAL) to update correctly for each listing. I thought changing the default_value would do, but it doesn't. I'm lost :(
Thanks!
I am creating something for a client and I have a Class that I created with a Custom Post Type called 'PuSH Feeds' and when the user adds a new post and publishes it they can then click on one of two buttons that I have in the Custom Meta Box.
One button is for 'Subscribe' and the other for 'Unsubscribe'. I am using the save_post action hook and testing if the $_POST global has that 'pushfeed-subscribe' or 'pushfeed-unsubscribe' and then do what I need to do. However for some reason I have found that once I click on the subscribe on my local machine stops the script because it says it did 100 consecutive calls etc and I end up with loads of duplicate posts with no title.
What would be the best way to avoid this and is there a better hook I can use for these special custom actions I want to activate of subscribing to a feed (which goes into another class and performs the subscribe method)?
This is the markup I have for those two buttons I mentioned with is inside the Metabox
<input type="submit" class="button-secondary" name="pushfeed-subscribe" id="pushfeed-subscribe" value="Subscribe">
<input type="submit" class="button-secondary" name="pushfeed-unsubscribe" id="pushfeed-unsubscribe" value="Unsubscribe">
Then I have this for the action hook:
add_action( 'save_post', array( $this, 'pushfeed_save_post_meta' ) );
The actual hook is like this:
public function pushfeed_save_post_meta( $post_id ) {
// Bail if we're doing an auto save
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['pushfeed-nonce-field'] ) || !wp_verify_nonce( $_POST['pushfeed-nonce-field'], basename( __FILE__ ) ) ) return;
// If Subsctiption ID is empty, generate a random long number and save it
if ( empty( $_POST['pushfeed-subscription-id'] ) ) {
$random_number = substr(number_format(time() * mt_rand(),0,'',''),0,10);
$pushfeed_subscription_id = $random_number . $post_id;
update_post_meta( $post_id, 'pushfeed-subscription-id', $pushfeed_subscription_id );
}
...
if ( isset( $_POST['pushfeed-subscribe'] ) || isset( $_POST['pushfeed-unsubscribe'] ) ) {
$subscription_domain = get_post_meta($post_id, 'pushfeed-domain', true);
$subscription_id = get_post_meta($post_id, 'pushfeed-subscription-id', true);
$subscription_feed_url = get_post_meta($post_id, 'pushfeed-feed-url', true);
$subscription_callback_url = $subscription_domain . '/pushfeed/' . $subscription_id;
$sub = PuSHSubscriber::instance($subscription_domain, $subscription_id, 'PuSHSubscription', new PuSHEnvironment());
if ( isset( $_POST['pushfeed-subscribe'] ) ) {
$sub->subscribe($subscription_feed_url, $subscription_callback_url);
} elseif ( isset( $_POST['pushfeed-unsubscribe'] ) ) {
$sub->unsubscribe($subscription_feed_url, $subscription_callback_url);
}
}
}
I am trying to find out why is it that the post is saving multiple duplicates with no title.
But above all I would like to know if there is a better action hook I can call for these two custom actions.
Update :
Hi everyone. I ended up using an Ajax request using the wordpress admin-ajax.php when clicking a button and then firing of the subscription method. Once that is done the subscription method will do a get request and if returns a 200 code then the method returns true to the Ajax.
The problem is probably caused by your use of a submit button.
Custom Meta Boxes are not intended to include submit buttons. The idea is that they contain form fields that get submitted when you click on the standard "Update" button. You can then save what is submitted in the save_post action hook.
Using a submit other than "Update" may be confusing WordPress and causing your problem.
I suggest that you change your Custom Meta Box to have a checkbox for Subscribe or a radio button for Subscribe/Unsubscribe that you can look at in the action hook.
I have been working with Wordpress and Woocommerce to add a custom meta field to my admin products page. I have everything working fine except that when I save the product, it does not show the info I entered in my meta box.
I checked the database and the data does indeed save, I saw the data I entered in the value and the meta field name was in the meta key. So that leads me to believe everything is working except showing the value on the admin product page once the product has been saved.
I think that the problem lies within one of these two functions but not certain, it could be in the save function though where it calls the update_post_meta.
here is where I think my problem is:
function variable_fields( $loop, $variation_data ) {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[<?php echo $loop; ?>]" value="<?php echo $variation_data['_my_custom_field'][0]; ?>"/>
</div>
</td>
</tr>
<?php
}
function variable_fields_js() {
?>
<tr>
<td>
<div>
<label><?php _e( 'My Custom Field', 'woocommerce' ); ?></label>
<input type="text" size="5" name="my_custom_field[' + loop + ']" />
</div>
</td>
</tr>
<?php
}
This is the function that saves the data, just incase the problem could be in here?
function variable_fields_process( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
$variable_custom_field = $_POST['my_custom_field'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $variable_custom_field[$i] ) ) {
update_post_meta( $variation_id, '_my_custom_field', stripslashes( $variable_custom_field[$i] ) );
}
endfor;
endif;
}
Looking through the code nothing is sticking out to me. Everything is working except when I save the product, the data entered doesnt show (but it is in the database) so Im thinking it could be a conflict with when it is saved, showing the saved values in the fields.
Thanks for your help
Update:
Specifically I think it is this line that is jamming me up:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
I know this is a little late but i managed to solve this and wanted to share it with you.
Basically WooCommerce have dropped $variation_data. You will now need to add $variation to your function and get the id. With the ID you can them get the post_meta / custom field. This can be done like so:
function variable_fields( $loop, $variation_data, $variation ) {
$get_variation_id = $variation->ID;
$data_to_get = get_post_meta($get_variation_id, '_my_custom_field', true );
This line:
value="<?php echo $variation_data['_my_custom_field'][0]; ?>"
Then becomes:
value="<?php echo $data_to_get; ?>"
That's it! i hope this helps you and anyone else that comes across this page.
I would recommend trying the Advanced Custom Fields (ACF) plugin, assuming this is for your site and not code to go into a published plugin.
I also started trying to code custom fields into a site, then realised after several pages of code that goes way beyond the business needs of "just adding a field", that the ACF plugin does the same job in ten clicks and two minutes. Sometimes you just have to be pragmatic, and accept that occasionally the best way of winning the debugging game is not to play the coding game in the first place.
Apologies if that is not the answer you or SO is looking for.