WooCommerce admin order edit save post - php

In WooCommerce, when I submit, how to catch a custom select field added in the order edit admin pages?
I have added this custom select field in the file class-wc-meta-box-order-data.php. I get this:
But I dont know how to catch or to save $_POST['vendor']
I have tried to add $_POST['vendor'] in wp-admin/post.php ,but it doesn't work.
This is the code that I have added:
<select class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-allow_clear="true">
<option value="<?php echo esc_attr( $user_id ); ?>" selected="selected"><?php echo htmlspecialchars( $user_string ); ?></option>
</select>
<!--/email_off-->
</p>
<p> <label for="order_status">供應商: </label>
<select name="vendor">
<?php
global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
for($i=1;$i<=$user_count;$i++){
$user_info = get_userdata($i);
if (implode(', ', $user_info->roles)=='vendor')
echo "<option value=".$user_info->user_login.">$user_info->user_login</option>";
}
?>
</select></p>
How can I get the submitted value and how can I save it?

Overriding core files is something prohibited for developers. So this is not the correct way to do it.
The way to do it is using the available hooks in the source code, instead of overriding this core files, as you will loose everything when the plugin will be updated.
Replace all original core files
Add this code instead (I have make some minor necessary changes).
Here is the replacement code + a hook to save the data to the order meta data:
add_action( 'woocommerce_admin_order_data_after_order_details', 'custom_code_after_order_details', 10, 1 );
function custom_code_after_order_details ( $order ) {
// Get custom field value from '_vendor' meta key
$value = $order->get_meta('_vendor');
?>
<p> <label for="order_status">供應商: </label>
<select name="vendor">
<?php global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
echo '<option value="">Select a vendor</option>';
for ( $i=1; $i<=$user_count; $i++ ) {
$user_info = get_userdata($i);
if ( in_array('vendor', $user_info->roles) ){
$user_login = $user_info->user_login;
$selected = $value == $user_login ? 'selected' : '';
echo '<option '.$selected.' value="'.$user_login.'">'.$user_login.'</option>';
}
}
?>
</select></p>
<input type="hidden" name="custom_select_field_nonce" value="<?php echo wp_create_nonce(); ?>">
<?php
}
add_action( 'save_post', 'save_custom_code_after_order_details', 10, 1 );
function save_custom_code_after_order_details( $post_id ) {
// We need to verify this with the proper authorization (security stuff).
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_select_field_nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'custom_select_field_nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
// Update the meta field in the database.
update_post_meta( $post_id, '_vendor', $_POST[ 'vendor' ] );
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works.

Related

How to store the image ID in wordpress database (localhost)

HI first of thanks and i love this community . I stuck in problem where i want to store the image/item ID in localhost wordpress database instead of image path. I tried everything but failed. Well i am newbie in wordpress so i need a help.
suppose my image/item id is like:---- http://localhost/wpmegameta/wp-admin/upload.php?item=45
databse path:----http://localhost/wpmegameta/wp-content/uploads/2019/05/coding.png
and i want to store only 45 in database
Wordpress database Screenshot
and I want to store only 45 in database like this ---
wp_nonce_field( 'case_study_bg_submit', 'case_study_bg_nonce' );
$lacuna2_stored_meta = get_post_meta( $post->ID ); ?>
<p>
<label for="case-study-bg" class="lacuna2-row-title">Practice Area Icon Image</label>
<img style="max-width:200px;height:auto;" id="meta-image-preview" src="<?php if ( isset ( $lacuna2_stored_meta['meta-image'] ) ){ echo $lacuna2_stored_meta['meta-image'][0]; } ?>" />
<input type="text" name="meta-image" id="meta-image" class="meta_image" value="<?php if ( isset ( $lacuna2_stored_meta['meta-image'] ) ){ echo $lacuna2_stored_meta['meta-image'][0]; } ?>" />
<input type="button" id="meta-image-button" class="button" value="Choose or Upload an Image" />
</p>
<script>
jQuery('#meta-image-button').click(function() {
var send_attachment_bkp = wp.media.editor.send.attachment;
wp.media.editor.send.attachment = function(props, attachment) {
jQuery('#meta-image').val(attachment.url);
jQuery('#meta-image-preview').attr('src',attachment.url);
wp.media.editor.send.attachment = send_attachment_bkp;
}
wp.media.editor.open();
return false;
});
</script>
<?php
}
/**
* Add Case Study background image metabox to the back end of Case Study posts
*/
function lacuna2_add_meta_boxes() {
add_meta_box( 'case-study-bg', 'Game Image', 'lacuna2_case_study_bg', 'post', 'side', 'low' );
}
add_action( 'add_meta_boxes', 'lacuna2_add_meta_boxes' );
/**
* Save background image metabox for Case Study posts
*/
function save_case_study_bg_meta_box($post_id ) {
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ 'case_study_bg_nonce' ] ) && wp_verify_nonce( $_POST[ 'case_study_bg_nonce' ], 'case_study_bg_submit' ) ) ? 'true' : 'false';
// Exits script depending on save status
if ( $is_autosave || $is_revision || !$is_valid_nonce ) {
return;
}
// Checks for input and sanitizes/saves if needed
if( isset( $_POST[ 'meta-image' ] ) ) {
update_post_meta( $post_id, 'meta-image', $_POST[ 'meta-image' ] );
}
}
add_action( 'save_post', 'save_case_study_bg_meta_box' );
In wordpress
Image /Item ID==45
Image Path== http://localhost/wpmegameta/wp-content/uploads/2019/05/coding.png
i want to store the image/item ID in wordpress database like=45
i think it's stander to store the image URL ,
but if you want to store just the id do that is to create a table contain all images URL's
and contains tow columns : ID,URL
hope this useful .

Wordpress Custom Post Type Meta Box not saving to database

I've created a Custom Post Type 'media-page-items' that I'm attempting to add meta boxes to. Currently the meta boxes are showing but not saving to the database. I've tried a few different approaches on it and none of them seem to save to database.
Debug is turned on but no errors are being thrown currently that I can see.
Any help is much appreciated!
//add article link to media page item
add_action( 'admin_menu', 'gruman_article_link_create' );
add_action( 'save_post', 'gruman_article_link_save', 10, 2 );
function gruman_article_link_create() {
add_meta_box( 'gruman-article-link', 'Article Link', 'gruman_article_link', 'media-page-items', 'advanced', 'high' );
}
function gruman_article_link( $post ) {
// retrieve the _gruman_article_title current value
$current_article_link = get_post_meta( $post->ID, '_gruman_article_link', true );
?>
<p>
<label>Article Link</label>
<br />
<input name="gruman-article-link" id="article-link" style="width: 97%;"><?php $current_article_link; ?>/>
<input type="hidden" name="gruman_article_link_nonce" value="<?php echo wp_create_nonce( plugin_basename( __FILE__ ) ); ?>" />
</p>
<?php }
function gruman_article_link_save( $post_id ) {
// verify taxonomies meta box nonce
if ( !isset( $_POST['gruman_article_link_nonce'] ) || !wp_verify_nonce( $_POST['gruman_article_link_nonce'], basename( __FILE__ ) ) ){
return $post_id;
}
// return if autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
return $post_id;
}
// Check the user's permissions.
if ( !current_user_can( 'edit_post', $post_id ) ){
return $post_id;
}
// store article title value
if ( isset( $_REQUEST['gruman-article-link'] ) ) {
update_post_meta( $post_id, '_gruman_article_link', sanitize_text_field( $_POST['gruman-article-link'] ) );
}
}
In wp_create_nonce you are using plugin_basename( __FILE__ ).
And when you verifying nonce you use basename( __FILE__ ) as action name.
Those values are not the same. First one will return something like my-plugin/my-plugin.php and the second will be my-plugin.php
That is why I believe wp_verify_nonce returns False and your data is not saved.

Wordpress post layout meta box not saving

I added the meta box to my post page, so that I can choose what layout I have for each post. I created a meta box, and it's showing, but it won't save.
if ( ! function_exists( 'post_layout_meta_box' ) ){
function post_layout_meta_box( $post ){
$custom = get_post_custom($post->ID);
$page_layout = isset($custom["page_layout_left"][0]) ? $custom["page_layout_left"][0] : ( isset($custom["page_layout_right"][0]) ? $custom["page_layout_right"][0] : $custom["page_layout_none"][0] );
?>
<p>
<select name="page_layout" id="page_layout">
<option value="left_sidebar" <?php selected( $page_layout, 'left_sidebar' ); ?>>Left Sidebar</option>
<option value="right_sidebar" <?php selected( $page_layout, 'right_sidebar' ); ?>>Right Sidebar</option>
<option value="full_width" <?php selected( $page_layout, 'full_width' ); ?>>No Sidebar</option>
</select>
</p>
<?php
}
}
if ( ! function_exists( 'post_save_layout_meta_box' ) ){
function post_save_layout_meta_box($post_id){
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
return $post_id;
} else{
if( isset( $_POST['page_layout'] ) ) {
update_post_meta( $post_id, 'page_layout', wp_kses($_POST['page_layout'], '') );
}
}
}
}
add_action( 'save_post', 'post_save_layout_meta_box' );
So what am I missing? When I choose Right Sidebar for example, it will return the value to the Left Sidebar after updating a page.
EDIT: Figured it out:
The problem was in this:
$page_layout = isset($custom["page_layout_left"][0]) ? $custom["page_layout_left"][0] : ( isset($custom["page_layout_right"][0]) ? $custom["page_layout_right"][0] : $custom["page_layout_none"][0] );
It should be
$page_layout = isset($custom["page_layout"][0]) ? $custom["page_layout"][0] : 'left_sidebar';
I guess this, in these lines of code there is problem. Wordpress confuse with page_layout_left, page_layout_right.
$page_layout = isset($custom["page_layout_left"][0]) ? $custom["page_layout_left"][0] : ( isset($custom["page_layout_right"][0]) ? $custom["page_layout_right"][0] : $custom["page_layout_none"][0] );
update_post_meta( $post_id, 'page_layout', wp_kses($_POST['page_layout'], '') );
I suggest if you want to use page_layout_left in update_post_meta() function then you should first find what option user selected i.e left or right or full width.
After that in update_post_meta function instead of using page_layout custom field name, use either page_layout_left or page_layout_right like this.
update_post_meta( $post_id, 'page_layout_left', wp_kses($_POST['page_layout'], '') );
Hope it may work.
Happy Coding.
Atul Jindal

Make Input Field Output Links

So, I added an input field to one of my post types.
What I'm trying to do is put a link (google.com, facebook.com, any link) into this field, and have it output on the front end into a button.
Below is my button. I would like it to go to the link I placed on the input field when I click the button.
<?php echo __('Purchase Now','Vierra'); ?>
Here's the code for the metabox:
<?php
add_action( 'add_meta_boxes', 'cart_meta_box_add' );
function cart_meta_box_add() {
add_meta_box( 'cart-meta-box-id', 'Put cart Here!', 'cart_meta_box_cb', 'Room', 'side', 'high' );
}
function cart_meta_box_cb() {
wp_nonce_field( basename( __FILE__ ), 'cart_meta_box_nonce' );
$value = get_post_meta(get_the_ID(), 'cart_key', true);
$html = '<label>cart: </label><input type="text" name="cart" value="'.$value.'"/>';
echo $html;
}
add_action( 'save_post', 'cart_meta_box_save' );
function cart_meta_box_save( $post_id ){
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
if ( !isset( $_POST['cart_meta_box_nonce'] ) || !wp_verify_nonce( $_POST['cart_meta_box_nonce'], basename( __FILE__ ) ) ) return;
if( !current_user_can( 'edit_post' ) ) return;
if( isset( $_POST['cart'] ) )
update_post_meta( $post_id, 'cart_key', esc_attr( $_POST['cart'], $allowed ) );
}
?>
Thank you for the help!
It's the same function used in the admin: get_post_meta(). Assuming the button is being printed inside the loop *:
<?php
if( $field = get_post_meta( get_the_ID(), 'cart_key', true ) ) {
?>
<a href="<?php echo $field; ?>" id="btn-book-now" class="btn-custom">
<?php echo __('Purchase Now','Vierra'); ?>
</a>
<?php
}
?>
* Otherwise, you'd use $post->ID instead of get_the_ID().

Custom Metabox Not Saving with looped input box

I am creating a metabox that loops through all the posts of another CPT: 'product'. The metabox lists all the published products in that CPT, puts them in a table row along with an input box. Each input box has a unique ID based off of the CPT product id.
Most of this code I have added based on other posts, but I am having a problem saving the data. I know that it has to do with my value="", my nonce, and $meta_value, but I am not sure where to go next. Below is the code I have so far:
<?php
add_action('admin_init', 'my_theme_on_admin_init');
function my_theme_on_admin_init() {
add_meta_box('my_metabox',
__('Daily Inventory Levels', 'textdomain'),
'my_metabox_render',
'location_inventory', 'normal', 'high'
);
}
function my_metabox_render($post) {
// using an underscore, prevents the meta variable
// from showing up in the custom fields section
global $woocommerce, $post;
$productsList = new WP_Query( 'post_type=product&post_status=publish');
?>
<div class="inside">
<table class="form-table">
<input type="hidden" name="inventory_noncename" id="inventory_noncename" value="<?php wp_create_nonce( 'inventory-nonce' ); ?>" />
<?php
while ( $productsList->have_posts() ) {
$productsList->the_post();
?>
<tr><td><label for="location_inventory_product-<?php the_ID(); ?>"><?php the_title(); ?></label></td><td><input id="location_inventory_product-<?php the_ID(); ?>" name="location_inventory_product-<?php the_ID(); ?>" cols="40" rows="5" value="<?php echo $meta_key; ?>" /></td></tr>
<?php }
wp_reset_postdata();
?>
</table>
</div>
<?php
}
/*update on save*/
add_action('save_post', 'save_postdata_dynamic_inventory_metabox' );
function save_postdata_dynamic_inventory_metabox( $post_id ) {
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
// Check permissions
if ( 'location_inventory' == $_POST['post_type'] ) {
if ( !current_user_can( 'edit_page', $post_id )) {
return $post_id;
}
}
elseif ( !current_user_can( 'edit_post', $post_id )) {
return $post_id;
}
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if (isset($_POST['inventory_noncename'])){
if ( !wp_verify_nonce( $_POST['inventory_noncename'], 'inventory-nonce' ) )
return;
} else {
return;
}
// Get the posted data and sanitize it for use as an HTML class.
$new_meta_value = ( isset( $_POST['location_inventory_product-'.the_ID().''] ) ? sanitize_html_class( $_POST['location_inventory_product-'.the_ID().''] ) : '' );
// Get the meta key.
$meta_key = 'location_inventory_product-'.the_ID().'';
// Get the meta value of the custom field key.
$meta_value = get_post_meta( $post_id, $meta_key, true );
// If a new meta value was added and there was no previous value, add it.
if ( $new_meta_value && '' == $meta_value )
add_post_meta( $post_id, $meta_key, $new_meta_value, true );
// If the new meta value does not match the old value, update it.
elseif ( $new_meta_value && $new_meta_value != $meta_value )
update_post_meta( $post_id, $meta_key, $new_meta_value );
// If there is no new meta value but an old value exists, delete it.
elseif ( '' == $new_meta_value && $meta_value )
delete_post_meta( $post_id, $meta_key, $meta_value );
} // ends function save_postdata_dynamic_reviews_metabox
Some observations:
1) The hook save_post takes 2 arguments:
add_action( 'save_post', 'save_postdata_dynamic_inventory_metabox', 10, 2 );
function save_postdata_dynamic_inventory_metabox( $post_id, $post_object ) { }
2) I don't think checking for permissions is necessary (at least, never seen it). Just in case, this is an example of the post_object use:
if ( 'location_inventory' == $post_object->post_type )
3) Short this check:
if ( !isset($_POST['inventory_noncename']) || !wp_verify_nonce( $_POST['inventory_noncename'], 'inventory-nonce' ) )
return;
4) The use of the_ID() inside the function save_postdata_dynamic_inventory_metabox is wrong. This is only available inside a Loop and you already have $post_id at hand.
5) Finally, and the most important, the way you're handling the $meta_key is wrong. For one, it's not defined inside the function my_metabox_render. Check this Answer for a working example.
Research in this search query to find more examples.

Categories