Add html code below product summary in Woocommerce [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
Woocommerce: How to add some html codes below product summary in single product page? and i want add different codes for each single product!
like this:

Here it is a complete example that will add a custom metabox (with a wysiwyg text editor field) in product edit pages. Then it will be displayed under product meta:
## ---- 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>';
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Backend:
Frontend:

Related

How to show WooCommerce custom product meta in new order emails?

I'm currently successfully saving custom post meta for a single product as follows:
function save_payment_terms( $product_id ) {
if ( isset( $_POST['payment_terms'] ) ) {
update_post_meta( $product_id, 'payment_terms', is_numeric( $_POST['payment_terms'] ) ? absint( wp_unslash( $_POST['payment_terms'] ) ) : '1' );
}
}
How would I go about adding that custom post meta to a new order confirmation email? I've tried the following hooks without success: woocommerce_email_order_meta and woocommerce_order_item_meta_start. The latest iteration looking as follows:
add_action('woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 4);
function email_confirmation_display_order_items($item_id, $item, $order, $plain_text) {
echo '<div>Terms: '. wc_get_order_item_meta( $item_id, 'payment_terms') .'</div>';
}
Resulting in:
Doing a var_dump of wc_get_order_item_meta, I get: ../snippet-ops.php(446) : eval()'d code:7:boolean false
Anyone that could shed some light on this?
Try the following instead:
add_action( 'woocommerce_order_item_meta_start', 'email_confirmation_display_order_items', 10, 3 );
function email_confirmation_display_order_items( $item_id, $item, $order ) {
// On email notifications for line items
if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {
$payment_terms = get_post_meta( $item->get_product_id(), 'payment_terms', true );
if ( ! empty($payment_terms) ) {
printf( '<div>' . __("Terms: %s", "woocommerce") . '</div>', $payment_terms );
}
}
}
Code goes in functions.php file of your active child theme (or active theme). It should works.
Related: WooCommerce Display avanced custom fields (ACF) inside order notification

Add personalized content after the "Add to Cart" button? Woocommerce

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
}

WooCommerce Orders metabox: Run php code on custom submit action

In Woocommerce, I have been able to add a custom submit button on order edit page in a custom metabox.
Here is my code (added in function.php Wordpress theme):
add_action( 'add_meta_boxes', 'MY_order_meta_boxes' );
function MY_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'order_meta_box_content',
'shop_order',
'side',
'default'
);
}
function order_meta_box_content( $order_id ) {
global $woocommerce, $table_prefix, $wpdb;
$order = new WC_Order( $order_id );
$customeremail = $order->get_billing_email();
?>
<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($submit)) {$order->add_order_note(sprintf("test2"));}
?>
<?php
return $order_id;
}
But I don't know why, the code doesn't run when button is clicked (submitted).
How can I run some custom code, when submit button is clicked on this custom metabox?
To make this work as you expect you need some more things. I have also removed unnecessary code and some errors. Also <imput> "submit" ID is too generic and can make unexpected errors.
You will be able to do any action (or saves) in a custom function hooked in save_post action hook:
// Add a custom metabox
add_action( 'add_meta_boxes', 'trusted_list_order_meta_boxes' );
function trusted_list_order_meta_boxes() {
add_meta_box(
'woocommerce-order-verifyemail',
__( 'Trusted List' ),
'trusted_list_order_meta_box_content',
'shop_order',
'side',
'default'
);
}
// Custom metabox content
function trusted_list_order_meta_box_content( $post ){
$customeremail = get_post_meta( $post->ID, '_billing_email', true);
$button_text = __( 'Add Note action', 'woocommerce' );
echo '<form method="post" action="CURRENT_FILE_URL">
<input type="submit" name="submit_trusted_list" value="' . $button_text . '"/>
<input type="hidden" name="trusted_list_nonce" value="' . wp_create_nonce() . '">
</form>';
}
// Saving or doing an action when submitting
add_action( 'save_post', 'trusted_list_save_meta_box_data' );
function trusted_list_save_meta_box_data( $post_id ){
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
// Check if our nonce is set (and our cutom field)
if ( ! isset( $_POST[ 'trusted_list_nonce' ] ) && isset( $_POST['submit_trusted_list'] ) )
return $post_id;
$nonce = $_POST[ 'trusted_list_nonce' ];
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $nonce ) )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
// Check the user’s permissions (for 'shop_manager' and 'administrator' user roles)
if ( ! current_user_can( 'edit_shop_order', $post_id ) && ! current_user_can( 'edit_shop_orders', $post_id ) )
return $post_id;
// Action to make or (saving data)
if( isset( $_POST['submit_trusted_list'] ) ) {
$order = wc_get_order( $post_id );
// $customeremail = $order->get_billing_email();
$order->add_order_note(sprintf("test2"));
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Code is tested on Woocommerce 3+ and works. You will get:
In your custom Metabox content function you will not be able to get any data using $_POST submitted data… So for example $_POST['submit'] will be always empty.
...I think you should use $_POST['submit'] other than $submit

Get the '_order_total_base_currency' value from the Aelia Currency Switcher and display at custom field meta-box

I like to get the value from the Aelia Currency Switcher plugin which is the '_order_total_base_currency' and make a simple USD conversion then display it on my custom field metabox. How do I fetch that value so I can use it for calculation, and then display?
Here is my code:
// Adding the metabox (on the right side)
add_action( 'add_meta_boxes', 'cdmb_add_meta_box');
function cdmb_add_meta_box() {
add_meta_box(
'woocommerce-order-my-custom',
__('USD Currency display'),
'cdmb_display_meta_box',
'shop_order',
'side',
'core'
);
}
// The metabox content
function cdmb_display_meta_box() {
// Get
$total_usd = (get_post_meta( $post->ID, '_order_total_base_currency', true )) / 0.75;
$total_usd .= get_post_meta( $post->ID, '_order_total_base_currency', true );
echo '<p>' . $total_usd . '</p>';
}
// Save/Update the meta data
add_action( 'save_post', 'cdmb_save_meta_box_data' );
function cdmb_save_meta_box_data( $post_id ) {
// Only for shop order
if ( 'shop_order' != $_POST[ 'post_type' ] )
return $post_id;
// Checking that is not an autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return $post_id;
## SETTING AND UPDATING DATA ##
update_post_meta( $post_id, 'total-usd', sanitize_text_field( $_POST[ 'total-usd' ] ) );
}
?>
After spending time with it, I found the answer. I am now able to fetch the value for '_order_total_base_currency' from the Aelia Currency Switcher plugin.
It needs the global $post; before the variable $total_usd.
The code should be this:
function cdmb_display_meta_box() {
// Get
global $post;
$total_usd = get_post_meta( $post->ID, '_order_total_base_currency', true );
echo '<p>' . $total_usd . '</p>';

Dispatch received orders woocommerce to dealers sending email notifications

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.

Categories