I have a list of emails (dealers) and I need when I receive order in wp-admin i open this order and send this order to a dealer (commercial , user...). Every dealer have an email and mark this order in custom field that he have been send to this dealer.
In my woocommerce orders page I need to open a order and do something like this:
Order 001 --- > send to Email1#exemple.com = Order 001 - Sent to Email1#exemple.com
Order 002 ----> send to Email2#exemple.com = Order 002 - Sent to Email2#exemple.com
Order 003 --- > send to Email1#exemple.com = Order 003 - Sent to Email1#exemple.com
I don't know where to start.
Does anyone have an idea or some code to achieve something like this?
Thanks
Here is a complete answer that will feet your needs. You will have to set in the 2nd function the array of the emails and names from your dealer list.
This code will display in backend Order edit pages a custom metabox with a selector, were you will set a dealer and you will click on "Save Order"…
A New Order notification email will be sent just once to that dealer email address.
Here is the code:
//Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', 'my_custom_order_meta_box' );
if ( ! function_exists( 'my_custom_order_meta_box' ) )
{
function my_custom_order_meta_box()
{
global $woocommerce, $order, $post;
add_meta_box( 'dealer_dispatch', __('Dealer Dispatch','woocommerce'), 'add_order_custom_fields_for_packaging', 'shop_order', 'side', 'core' );
}
}
//adding Meta field in the meta container admin shop_order pages
if ( ! function_exists( 'add_order_custom_fields_for_packaging' ) )
{
function add_order_custom_fields_for_packaging()
{
global $woocommerce, $order, $post;
// Define HERE your array of values <== <== <== <== <== <== <== <==
$option_values = array(
'default' => __('no selection', 'woocommerce'),
'dealer1#email.com' => 'Dealer 1 Name',
'dealer2#email.com' => 'Dealer 2 Name',
'dealer3#email.com' => 'Dealer 3 Name',
);
// Get the values from the custom-fields (if they exist)
$meta_field_data = get_post_meta( $post->ID, '_dealer_dispatch', true );
$dealer_email_sent = get_post_meta( $post->ID, '_dealer_email_sent', true );
echo '<input type="hidden" name="my-custom-order_meta-box-nonce" value="'. wp_create_nonce() .'">
<label for="dealer_dispatch">'.__('Select a dealer', 'woocommerce').'</label><br>
<select name="dealer_dispatch">';
foreach( $option_values as $option_key => $option_value ){
if ( $meta_field_data == $option_key || 'default' == $option_key )
$selected = ' selected';
else
$selected = '';
echo '<option value="'.$option_key.'"'.$selected.'>'. $option_value.'</option>';
}
echo '</select><br>';
// if an email has been sent to the dealer we display a message
if( ! empty($dealer_email_sent) )
echo '<p style="color:green; font-weight:bold;">'.__('Email sent to: ', 'woocommerce').$dealer_email_sent.'</p>';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'add_my_custom_field_for_order_meta_box', 20, 1 );
if ( ! function_exists( 'add_my_custom_field_for_order_meta_box' ) )
{
function add_my_custom_field_for_order_meta_box( $post_id ) {
## Verify and securing data. ##
// Check if our nonce is set.
if ( ! isset( $_POST[ 'my-custom-order_meta-box-nonce' ] ) ) {
return $post_id;
}
$nonce = $_REQUEST[ 'my-custom-order_meta-box-nonce' ];
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) ) {
return $post_id;
}
// Continuing only if form is submited.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check and set 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;
}
}
// -- -- IT IS SECURED NOW -- --
// Sanitize input and update order meta data custom field.
$dealer_dispatch = $_POST[ 'dealer_dispatch' ];
// Saving the selected value
if( 'default' != $dealer_dispatch )
update_post_meta( $post_id, '_dealer_dispatch', sanitize_text_field( $dealer_dispatch ) );
# SEND CUSTOM EMAIL ONLY ONCE #
$dealer_dispatch_val = get_post_meta( $post_id, '_dealer_dispatch', true );
$dealer_email_sent = get_post_meta( $post_id, '_dealer_email_sent', true );
if( empty($dealer_email_sent) && !empty($dealer_dispatch_val) ){
$email_notifications = WC()->mailer()->get_emails();
$email_notifications['WC_Email_New_Order']->recipient = $dealer_dispatch;
$email_notifications['WC_Email_New_Order']->trigger( $post_id );
// Creating a custom meta data for this order to avoid sending this email 2 times
update_post_meta( $post_id, '_dealer_email_sent', $dealer_dispatch_val );
}
}
}
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
Hello community,
I want to show an image uploaded via custom-field on the product table and finally on the PDF invoice …
I use this solution: Add image file upload field to WooCommerce single products (LoicTheAztec's answer)
to make the image-upload work and now I want to extent this solution to show the image (img-name or img-url) also on the product table (emails) and finally on the PDF invoice…
How can I do this? Can someone help me?
Here ist the code from my functions.php:
// -----------------------------------------
// Image-Upload
// -----------------------------------------
// Display additional product fields (+ jQuery code)
add_action( 'woocommerce_before_variations_form', 'display_additional_product_fields', 9 );
function display_additional_product_fields(){
global $product;
$id = $product->get_id();
if(($id == 41745 ) || ($id == 41769 )):
?>
<p class="_customvariations form-row validate-required" id="image" >
<label for="file_field"><span style="color:#000000; font-weight:600;"><?php echo __("Bild hochladen") . ''; ?></span>
<input type='file' name='image' accept='image/*'>
</label>
</p>
<?php
endif;
}
// Add custom fields data as the cart item custom data
add_filter( 'woocommerce_add_cart_item_data', 'add_custom_fields_data_as_custom_cart_item_data', 10, 2 );
function add_custom_fields_data_as_custom_cart_item_data( $cart_item, $product_id ){
if( isset($_FILES['image']) && ! empty($_FILES['image']) ) {
$upload = wp_upload_bits( $_FILES['image']['name'], null, file_get_contents( $_FILES['image']['tmp_name'] ) );
$filetype = wp_check_filetype( basename( $upload['file'] ), null );
$upload_dir = wp_upload_dir();
$upl_base_url = is_ssl() ? str_replace('http://', 'https://', $upload_dir['baseurl']) : $upload_dir['baseurl'];
$base_name = basename( $upload['file'] );
$cart_item['file_upload'] = array(
'guid' => $upl_base_url .'/'. _wp_relative_upload_path( $upload['file'] ), // Url
'file_type' => $filetype['type'], // File type
'file_name' => $base_name, // File name
'title' => ucfirst( preg_replace('/\.[^.]+$/', '', $base_name ) ), // Title
);
$cart_item['unique_key'] = md5( microtime().rand() ); // Avoid merging items
}
return $cart_item;
}
// Display custom cart item data in cart (optional)
add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {
if ( isset( $cart_item['file_upload']['title'] ) ){
$cart_item_data[] = array(
'name' => __( 'Individuelles Bild', 'woocommerce' ),
'value' => str_pad($cart_item['file_upload']['title'], 16, ' ', STR_PAD_LEFT) . '…',
);
}
return $cart_item_data;
}
// Save Image data as order item meta data
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_field_update_order_item_meta', 20, 4 );
function custom_field_update_order_item_meta( $item, $cart_item_key, $values, $order ) {
if ( isset( $values['file_upload'] ) ){
$item->update_meta_data( '_img_file', $values['file_upload'] );
}
}
// Admin orders: Display a linked button + the link of the image file
add_action( 'woocommerce_after_order_itemmeta', 'backend_image_link_after_order_itemmeta', 10, 3 );
function backend_image_link_after_order_itemmeta( $item_id, $item, $product ) {
// Only in backend for order line items (avoiding errors)
if( is_admin() && $item->is_type('line_item') && $file_data = $item->get_meta( '_img_file' ) ){
echo '<p>'.__("Individuelles Bild öffnen") . '</p>'; // Optional
echo '<p><code>'.$file_data['guid'].'</code></p>'; // Optional
}
}
// Admin new order email: Display a linked button + the link of the image file
add_action( 'woocommerce_email_after_order_table', 'wc_email_new_order_custom_meta_data', 10, 4);
function wc_email_new_order_custom_meta_data( $order, $sent_to_admin, $plain_text, $email ){
// On "new order" email notifications
// if ( 'new_order' === $email->id ) {
foreach ($order->get_items() as $item ) {
if ( $file_data = $item->get_meta( '_img_file' ) ) {
echo '<p>
'.__("Individuelles Bild herunterladen") . '<br>
<pre><code style="font-size:12px; background-color:#eee; padding:5px;">'.$file_data['guid'].'</code></pre>
</p><br>';
}
}
//}
}
Thank you very much!
Edit:
Thanks Vincenzo Di Gaetano! Great answer, but I miss one little thing:
I would like to show the image-filename on the email attached PDF-invoice (I use woocommerce germanized plugin for german market, but I think this is not relevant because of order item meta).
I have a working custom textfield (code below) which shows the custom text everywhere, also on the email attached PDF-invoice. But I can't get this work for my image file-upload …
// -----------------------------------------
// Custom textfield
// -----------------------------------------
// 1. Show custom input field above Add to Cart
add_action( 'woocommerce_before_variations_form', 'wasentogo_product_add_on', 9 );
function wasentogo_product_add_on() {
global $product;
$id = $product->get_id();
if(($id == 41745 ) || ($id == 41775 )):
$value = isset( $_POST['custom_text_add_on'] ) ? sanitize_text_field( $_POST['_custom_text_add_on'] ) : '';
echo '<p class="_customvariations"><label><span style="color:#000000; font-weight:600;">Text eingeben</span><input name="custom_text_add_on" value="' . $value . '" maxlength="25"><abbr class="required" title="required"></abbr> max. 25 Zeichen</label></p>';
endif;
}
// -----------------------------------------
// 2. Throw error if custom input field empty
add_filter( 'woocommerce_add_to_cart_validation', 'wasentogo_product_add_on_validation', 10, 3 );
function wasentogo_product_add_on_validation( $passed, $product_id, $qty ){
if( isset( $_POST['custom_text_add_on'] ) && sanitize_text_field( $_POST['custom_text_add_on'] ) == '' ) {
wc_add_notice( 'Sie müssen erst einen individuellen Text eingeben!', 'error' );
$passed = false;
}
return $passed;
}
// -----------------------------------------
// 3. Save custom input field value into cart item data
add_filter( 'woocommerce_add_cart_item_data', 'wasentogo_product_add_on_cart_item_data', 10, 2 );
function wasentogo_product_add_on_cart_item_data( $cart_item, $product_id ){
if( isset( $_POST['custom_text_add_on'] ) ) {
$cart_item['custom_text_add_on'] = sanitize_text_field( $_POST['custom_text_add_on'] );
}
return $cart_item;
}
// -----------------------------------------
// 4. Display custom input field value # Cart
add_filter( 'woocommerce_get_item_data', 'wasentogo_product_add_on_display_cart', 10, 2 );
function wasentogo_product_add_on_display_cart( $data, $cart_item ) {
if ( isset( $cart_item['custom_text_add_on'] ) ){
$data[] = array(
'name' => 'Individueller Text',
'value' => sanitize_text_field( $cart_item['custom_text_add_on'] )
);
}
return $data;
}
// -----------------------------------------
// 5. Save custom input field value into order item meta
add_action( 'woocommerce_add_order_item_meta', 'wasentogo_product_add_on_order_item_meta', 10, 2 );
function wasentogo_product_add_on_order_item_meta( $item_id, $values ) {
if ( ! empty( $values['custom_text_add_on'] ) ) {
wc_add_order_item_meta( $item_id, 'Individueller Text', $values['custom_text_add_on'], true );
}
}
// -----------------------------------------
// 6. Display custom input field value into order table
add_filter( 'woocommerce_order_item_product', 'wasentogo_product_add_on_display_order', 10, 2 );
function wasentogo_product_add_on_display_order( $cart_item, $order_item ){
if( isset( $order_item['custom_text_add_on'] ) ){
$cart_item['custom_text_add_on'] = $order_item['custom_text_add_on'];
}
return $cart_item;
}
// -----------------------------------------
// 7. Display custom input field value into order emails
add_filter( 'woocommerce_email_order_meta_fields', 'wasentogo_product_add_on_display_emails' );
function wasentogo_product_add_on_display_emails( $fields ) {
$fields['custom_text_add_on'] = 'Individueller Text';
return $fields;
}
Can someone help me with this part too?
Maybe this screenshot helps to understand what I need. I want to show the img-filename listed on my invoice-PDF
I think I need these two parts from the custom textfield to build something simular for the img-field …
// 5. Save custom input field value into order item meta
add_action( 'woocommerce_add_order_item_meta', 'wasentogo_product_add_on_order_item_meta', 10, 2 );
function wasentogo_product_add_on_order_item_meta( $item_id, $values ) {
if ( ! empty( $values['custom_text_add_on'] ) ) {
wc_add_order_item_meta( $item_id, 'Individueller Text', $values['custom_text_add_on'], true );
}
}
// -----------------------------------------
// 6. Display custom input field value into order table
add_filter( 'woocommerce_order_item_product', 'wasentogo_product_add_on_display_order', 10, 2 );
function wasentogo_product_add_on_display_order( $cart_item, $order_item ){
if( isset( $order_item['custom_text_add_on'] ) ){
$cart_item['custom_text_add_on'] = $order_item['custom_text_add_on'];
}
return $cart_item;
}
// -----------------------------------------
Thank you very much again!
Instead of the woocommerce_email_after_order_table hook you can use woocommerce_order_item_meta_start. You can find it inside the template: /woocommerce/emails/email-order-items.php
The uploaded image link will be added to all email templates, including the customer-invoice.php template.
If $file_data = $item->get_meta( '_img_file' ) exists you can access the following information:
$file_data['file_name'] (including the image file extension)
$file_data['title'] (without the image file extension)
Since the woocommerce_order_item_meta_start hook doesn't have access to the email object (to check the template id) I thought about adding the image title to all email templates. Through the woocommerce_email_styles hook you can show or hide it based on the email template.
// shows the uploaded image of the order item
add_action( 'woocommerce_order_item_meta_start', 'show_uploaded_image_in_email_templates', 10, 4 );
function show_uploaded_image_in_email_templates( $item_id, $item, $order, $plain_text ) {
if ( $file_data = $item->get_meta( '_img_file' ) ) {
echo '<p>
' . __( "Individuelles Bild herunterladen" ) . '
<pre><code style="font-size:12px; background-color:#eee; padding:5px;">' . $file_data['guid'] . '</code></pre>
<span class="custom_image_filename">' . $file_data['title'] . '</span>
</p>';
}
}
// show or hide the image title based on the email template
add_filter( 'woocommerce_email_styles', 'add_woocommerce_email_styles', 10, 2 );
function add_woocommerce_email_styles( $css, $email ) {
if ( $email->id == 'customer_invoice' ) {
$css .= '.custom_image_filename { display: inline-block; }';
} else {
$css .= '.custom_image_filename { display: none; }';
}
return $css;
}
The invoice email will be:
All other emails:
The code has been tested and works. Add it to your active theme's functions.php.
I found the below code the other day, and now my client wants two more fields. If I add it again to my snippet plugin it says “Cannot redeclare function set_custom_edit_shop_order_columns.”. Could someone kindly help me to change the code with two more custom fields?
I have changed custom_column to courier_reference and want another text field tracking_number and one more is shipping_date with a date picker rather than a text box.
Any help is appreciated.
//from::https://codex.wordpress.org/Plugin_API/Action_Reference/manage_posts_custom_column
// For displaying in columns.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['custom_column'] = __( 'Custom Column', 'your_text_domain' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'custom_column' :
echo esc_html( get_post_meta( $post_id, 'custom_column', true ) );
break;
}
}
// For display and saving in order details page.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_column',
__( 'Custom Column', 'your_text_domain' ),
'shop_order_display_callback',
'shop_order'
);
}
// For displaying.
function shop_order_display_callback( $post ) {
$value = get_post_meta( $post->ID, 'custom_column', true );
echo '<textarea style="width:100%" id="custom_column" name="custom_column">' . esc_attr( $value ) . '</textarea>';
}
// For saving.
function save_shop_order_meta_box_data( $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;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
}
// Make sure that it is set.
if ( ! isset( $_POST['custom_column'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['custom_column'] );
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_column', $my_data );
}
add_action( 'save_post', 'save_shop_order_meta_box_data' );
Updated... You just need to have multiple different slugs in the same functions. For your desired 3 custom fields use the following:
// Add columns to admin orders table.
add_filter( 'manage_edit-shop_order_columns', 'set_custom_edit_shop_order_columns' );
function set_custom_edit_shop_order_columns($columns) {
$columns['courier_reference'] = __( 'Courier reference', 'woocommerce' );
$columns['tracking_number'] = __( 'Tracking number', 'woocommerce' );
$columns['shipping_date'] = __( 'Shipping date', 'woocommerce' );
return $columns;
}
// Add the data to the custom columns for the order post type:
add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_column', 10, 2 );
function custom_shop_order_column( $column, $post_id ) {
switch ( $column ) {
case 'courier_reference' :
echo esc_html( get_post_meta( $post_id, 'courier_reference', true ) );
break;
case 'tracking_number' :
echo esc_html( get_post_meta( $post_id, 'tracking_number', true ) );
break;
case 'shipping_date' :
echo esc_html( get_post_meta( $post_id, 'shipping_date', true ) );
break;
}
}
// Add a metabox.
add_action( 'add_meta_boxes', 'add_shop_order_meta_box' );
function add_shop_order_meta_box() {
add_meta_box(
'custom_meta_box',
__( 'Tracking information', 'woocommerce' ),
'shop_order_content_callback',
'shop_order'
);
}
// For displaying metabox content
function shop_order_content_callback( $post ) {
// Textarea Field
$courier_reference = get_post_meta( $post->ID, 'courier_reference', true );
echo '<p>' . __( 'Courier reference', 'woocommerce' ) . '<br>
<textarea style="width:100%" id="courier_reference" name="courier_reference">' . esc_attr( $courier_reference ) . '</textarea></p>';
// Text field
$tracking_number = get_post_meta( $post->ID, 'tracking_number', true );
echo '<p>' . __( 'Tracking number', 'woocommerce' ) . '<br>
<input type="text" style="width:100%" id="tracking_number" name="tracking_number" value="' . esc_attr( $tracking_number ) . '"></p>';
// Date picker field
$shipping_date = get_post_meta( $post->ID, 'shipping_date', true );
echo '<p>' . __( 'shipping_date', 'woocommerce' ) . '<br>
<input type="date" style="width:100%" id="shipping_date" name="shipping_date" value="' . esc_attr( $shipping_date ) . '"></p>';
}
// For saving the metabox data.
add_action( 'save_post_shop_order', 'save_shop_order_meta_box_data' );
function save_shop_order_meta_box_data( $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;
}
// Check the user's permissions.
if ( ! current_user_can( 'edit_shop_order', $post_id ) ) {
return;
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['courier_reference'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'courier_reference', sanitize_textarea_field( $_POST['courier_reference'] ) );
}
// Make sure that 'tracking_number' it is set.
if ( isset( $_POST['tracking_number'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'tracking_number', sanitize_text_field( $_POST['tracking_number'] ) );
}
// Make sure that 'shipping_date' is set.
if ( isset( $_POST['shipping_date'] ) ) {
// Update the meta field in the database.
update_post_meta( $post_id, 'shipping_date', sanitize_text_field( $_POST['shipping_date'] ) );
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Woocommerce: How to add some html codes below add to cart in single product page? and i want add different codes for each single product!
I used a similar one for metabox, but I can not find a way to adapt it after "add to cart"
---- 1. Backend ----
// Adding a custom Meta container to admin products pages
add_action( 'add_meta_boxes', 'create_custom_meta_box' );
if ( ! function_exists( 'create_custom_meta_box' ) )
{
function create_custom_meta_box()
{
add_meta_box(
'custom_product_meta_box',
__( 'Additional Product text <em>(optional)</em>', 'woocommerce' ),
'add_custom_product_content_meta_box',
'product',
'normal',
'default'
);
}
}
// Custom metabox content in admin product pages
if ( ! function_exists( 'add_custom_product_content_meta_box' ) ){
function add_custom_product_content_meta_box( $post ){
$text_area = get_post_meta($post->ID, '_custom_text', true) ? get_post_meta($post->ID, '_custom_text', true) : '';
$args['textarea_rows'] = 6;
echo '<p>'.__( 'Custom text label', 'woocommerce' ).'</p>';
wp_editor( $text_area, 'custom_text', $args );
echo '<input type="hidden" name="custom_text_field_nonce" value="' . wp_create_nonce() . '">';
}
}
//Save the data of the Meta field
add_action( 'save_post', 'save_custom_product_content_meta_box', 20, 3 );
if ( ! function_exists( 'save_custom_product_content_meta_box' ) ){
function save_custom_product_content_meta_box( $post_id, $post, $update ) {
if ( $post->post_type != 'product') return; // Only products
// Check if our nonce is set.
if ( ! isset( $_POST[ 'custom_text_field_nonce' ] ) )
return $post_id;
//Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST[ 'custom_text_field_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 ( ! current_user_can( 'edit_product', $post_id ) )
return $post_id;
// Sanitize user input and update the meta field in the database.
if ( isset( $_POST[ 'custom_text' ] ) )
update_post_meta( $post_id, $prefix.'_custom_text', wp_kses_post($_POST[ 'custom_text' ]) );
}
}
---- 2. Frontend ----
// Add custom text under single product meta
add_action( 'woocommerce_single_product_summary', 'add_custom_product_text', 70 );
function add_custom_product_text() {
global $product;
$custom_text = get_post_meta( $product->get_id(), '_custom_text', true );
if( empty($custom_text) ) return;
echo '<div class="product-extra-text" style="margin-top:30px;">';
echo '<h3>' . __( 'Product extras', 'woocommerce' ) . '</h3>';
// Updated to apply the_content filter to WYSIWYG content
echo apply_filters( 'the_content', $custom_text );
echo '</div>';
}
I put the code in parts, but it is united in functions of the theme.
Backend IMAGE:
enter image description here
In your theme's function.php add the code,
add_action('woocommerce_after_add_to_cart_button','show_custom_text');
function show_custom_text() {
call the custom text inside this function
}
I am using the visual composer for the WordPress posts and pages actually for over all. But I want to make some custom meta boxes under the screen of the post editor. Actually already I have made the fields. But now I want to make those fields available in the visual composer. Actually I want to add those fields in the visual editor. How can I do that? Please help me with your valuable knowledge.
Here is my code of the meta boxes
<?php
function myplugin_add_meta_box() {
$screens = array( 'post', 'page' );
foreach ( $screens as $screen ) {
add_meta_box(
'myplugin_sectionid',
__( 'My Post Section Title', 'myplugin_textdomain' ),
'myplugin_meta_box_callback',
$screen
);
}
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );
function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce' );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );
echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}
function myplugin_save_meta_box_data( $post_id ) {
if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
/* OK, it's safe for us to save the data now. */
// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
return;
}
// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );
I see that you are echoing your fields as inputs. You need to use the wp_editor() function instead. It will take care of the wysiwyg (visual editor) field creation for you.
I want to change the slug of a post with a custom field.
In example, if the custom field is "keyword" my post link will become: mysite.com/keyword.
I wrote this script in fonction.php:
function change_default_slug($id) {
// get part number
$partno = get_post_meta( $id, 'partno', true );
$post_to_update = get_post( $id );
// prevent empty slug, running at every post_type and infinite loop
if ( $partno == '' )
return;
$updated_post = array();
$updated_post['ID'] = $id;
$updated_post['post_name'] = $partno;
wp_update_post( $updated_post ); // update newly created post
}
add_action('save_post', 'change_default_slug');
add_action( 'add_meta_boxes', 'cd_meta_box_add' );
function cd_meta_box_add()
{
add_meta_box( 'my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'normal', 'high' );
}
function cd_meta_box_cb( $post )
{
$values = get_post_custom( $post->ID );
$text = isset( $values['my_meta_box_text'] ) ? esc_attr( $values['my_meta_box_text'][0] ) : '';
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
?>
<p>
<label for="my_meta_box_text">Text Label</label>
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" />
</p>
<?php
}
add_action( 'save_post', 'cd_meta_box_save' );
function cd_meta_box_save( $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['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
// now we can actually save the data
$allowed = array(
'a' => array( // on allow a tags
'href' => array() // and those anchords can only have href attribute
)
);
// Probably a good idea to make sure your data is set
if( isset( $_POST['my_meta_box_text'] ) )
update_post_meta( $post_id, 'my_meta_box_text', wp_kses( $_POST['my_meta_box_text'], $allowed ) );
}
$partno = get_post_meta($post->ID,'my_meta_box_text',true);
echo $partno;
This script return me "Fatal error: Maximum execution time of 30 seconds exceeded". But it seems it work because my slug change. Any idea about this issue?
The 'save_post' action gets called by wp_update_post(), so your change_default_slug() function causes an infinite loop. You need to perform a check within change_default_slug() and bail out if the function has already been called:
function change_default_slug($id) {
static $beentheredonethat = false;
if ($beentheredonethat) return;
$beentheredonethat = true;
//do your stuff and save the post...
}