I am developing a plugin for WooCommerce. I want to override the order details template of admin. i have read about on https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/ , but still I don't understand how to override the order detail template of admin. following is my code:
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
if ( ! class_exists( 'Test' ) ) {
load_plugin_textdomain( 'test', false, dirname( plugin_basename( __FILE__ ) ) . '/' );
}
}
class Test {
public function __construct() {
add_action( 'init', array( $this, 'include_template_functions' ), 20 );
add_action( 'woocommerce_init', array( $this, 'woocommerce_loaded' ) );
add_action( 'plugins_loaded', array( $this, 'plugins_loaded' ) );
do_action( 'woocommerce_admin_order_data_after_order_details', 'hello' );
}
public function hello() {
echo "order detail template has loaded";
}
public function include_template_functions() {
include( 'woocommerce-template.php' );
echo "template has loaded";
}
public function woocommerce_loaded() {
}
public function plugins_loaded() {
}
}
$GLOBALS['wc_acme'] = new Test();
It's not calling the hook associated with woocommerce_admin_order_data_after_order_details.
Can anyone please suggest or share some example of editing the order details template editing via plugin. Please note that I am referring to the order detail template inside the administrator, where administrator can view the detail of any order from the list.
From my tutorial on customizing WooCommerce checkout fields this is how you'd display some extra order meta data in the Order Details metabox:
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>';
echo '<p><strong>' . __( 'Another field' ) . ':</strong>' . get_post_meta( $order->id, '_another_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );
This assumes that you have collected the data on checkout and saved the data as post meta for the $order in question.
You could use woocommerce_admin_order_data_after_billing_address action:
function order_phone_backend($order){
echo "<p><strong>Billing phone 2:</strong> " . get_post_meta( $order->id, '_billing_phone_new', true ) . "</p><br>";
}
add_action( 'woocommerce_admin_order_data_after_billing_address','order_phone_backend', 10, 1 );
Related
I have added some custom fields on the WooCommerce checkout page. I have saved the data of those fields and displayed it on the admin order page. Like picture no.1. Everything is OK.
Now I want to edit the data/text/value of this field. Like picture no.2. Like billing and shipping on the admin order page....
How can I solve it?
function display_admin_order_meta ( $order ) {
$strt= $order->get_meta('_checkout_custom_name', true );
if( ! empty( $strt) ){
$label = __( 'Phone' );
if( is_admin() ){
echo '<p><strong>' . $label . ' : </strong> ' . $strt. '</p>';
}
else {
echo '<table class="woocommerce-table"><tbody><tr>
<th>' . $label . ' : </th><td>' . $strt. '</td>
</tr></tbody></table>';
}
}
$strt= $order->get_meta('_checkout_others_address', true );
if( ! empty( $strt) ){
$label = __( 'Address' );
if( is_admin() ){ // Admin
echo '<p><strong>' . $label . ' : </strong> ' . $strt. '</p>';
}
else {
echo '<table class="woocommerce-table"><tbody><tr>
<th>' . $label . ' : </th><td>' . $strt. '</td>
</tr></tbody></table>';
}
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'display_admin_order_meta', 20, 1);
This is the checkout field data I created. I want to edit these like the picture above.
This is a way(customize it if needed)
Add custom billing form data input with woocommerce_admin_billing_fields filter
add_filter('woocommerce_admin_billing_fields', 'add_woocommerce_admin_billing_fields');
function add_woocommerce_admin_billing_fields($billing_fields) {
$billing_fields['_checkout_custom_name'] = array( 'label' => __('Name', 'woocommerce') );
$billing_fields['_checkout_others_address'] = array( 'label' => __('Address', 'woocommerce') );
return $billing_fields;
}
Then save custom fields like this
function cloudways_save_extra_details( $post_id, $post ){
update_post_meta( $post_id, '_checkout_custom_name', $_POST[ '_checkout_custom_name' ] ) );
update_post_meta( $post_id, '_checkout_custom_address', $_POST[ '_checkout_custom_address' ] ) );
}
add_action( 'woocommerce_process_shop_order_meta', 'cloudways_save_extra_details', 45, 2 );
I've created 2 custom type post in Wordpress. Now i what to create custom metabox in one of them to select posts from second custom type post and display it in front. I cannot find how to figure out this problem, i've tried query post but nothing was displayed.
Please check the code to create Metabox named Company Address for example seller custom post type.
Meta box company address for seller custom post type added:
/**
* Meta box company address for seller custom post type added
*/
function wdbs_add_seller_metaboxes() {
add_meta_box(
'wdbs_seller_company_address',
'Company Address',
'wdbs_seller_company_address',
'seller',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'wdbs_add_seller_metaboxes' );
Meta box field company address html:
/**
* Meta box field company address html
*/
function wdbs_seller_company_address() {
global $post;
wp_nonce_field( basename( __FILE__ ), 'seller_fields' );
$company_address = get_post_meta( $post->ID, 'company_address', true );
echo '<textarea type="text" name="company_address" class="widefat" rows="6">' . esc_textarea( $company_address ) . '</textarea>';
}
Save Seller metafields:
/**
* SAVE SELLER METAFIELDS
* Saves values for company address meta field
*/
function wdbs_save_seller_meta( $post_id, $post ) {
// Return if the user doesn't have edit permissions.
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
if ( ! isset( $_POST['company_address'] ) || ! wp_verify_nonce( $_POST['seller_fields'], basename(__FILE__) ) ) {
return $post_id;
}
$seller_meta['company_address'] = esc_textarea( $_POST['company_address'] );
foreach ( $seller_meta as $key => $value ) :
if ( 'revision' === $post->post_type ) {
return;
}
if ( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value);
}
if ( ! $value ) {
delete_post_meta( $post_id, $key );
}
endforeach;
}
add_action( 'save_post', 'wdbs_save_seller_meta', 1, 2 );
I have using this function code to make new Custom Text Area, but have issue to show it in product page.
// Custom Field Product
add_action( 'woocommerce_product_options_general_product_data',
'woo_add_custom_general_fields' );
add_action( 'woocommerce_process_product_meta',
'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'Custom Text:', 'woocommerce' ),
'placeholder' => '',
'description' => __( 'Enter the custom value here.', 'woocommerce' )
)
);
echo '</div>';
}
// Save Changes to DB
function woo_add_custom_general_fields_save( $post_id ){
// Textarea
$woocommerce_textarea = $_POST['_textarea'];
if( !empty( $woocommerce_textarea ) )
update_post_meta( $post_id, '_textarea', esc_html( $woocommerce_textarea
) );
}
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
Looks like when press save, its storing data in DB, but its not showing in single product page. Can someone to tell me where is issue in this function?
The problem is that in the following function, $post is not defined. (The code was indented for clarity.)
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
// custom content.
echo get_post_meta( $post->ID, '_textarea', true );
}
So, a simple fix would be to add global $post; to the function:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $post;
// custom content.
if ( $post ) {
echo get_post_meta( $post->ID, '_textarea', true );
}
}
Alternatively, you can use the global $product object:
// Show data to product
add_action( 'woocommerce_after_add_to_cart_button',
'custom_content_after_addtocart_button', 100 );
function custom_content_after_addtocart_button() {
global $product;
// custom content.
if ( $product ) {
echo get_post_meta( $product->get_id(), '_textarea', true );
}
}
I'm sooooo close - but I can't get this last little bit.
I am adding the authors name to books we have for sale in woo. The style needs to be different from the book title so I'm using a custom meta field to put it under the title and style it the way I want. I have everything working and where I want it, now I just need for it only show on a particular category instead of on all products.
There seems to me to be two ways to do this.
1 - Only display it for products in a particular category
2 - Only display it if there is content.
I'm currently trying to only display if it's a particular category, but while writing this, its seems a more elegant solution to only display if content exists.
Here's what I have
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
if ( is_product() && has_term( 'books-media', 'product_cat' ) ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
This doesn't display anywhere, but when I take out the if statement it shows on all products.
Any idea where I went awry?
Here is the answer in context Helga - this snippet will add a custom meta field to the woocommerce product editing dashboard, save the content and display it under the product title on the single product view.
/* add product subhead custom field to woocommerce product dashboard */
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
function woo_add_custom_general_fields() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_text_input(
array(
'id' => '_text_field',
'label' => __( 'MY-LABEL', 'woocommerce' ),
'placeholder' => 'Author',
'desc_tip' => 'true',
'description' => __( 'This text will show below the product title.', 'woocommerce' )
)
);
echo '</div>';
}
/* Save woo custom field */
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
function woo_add_custom_general_fields_save( $post_id ){
$woocommerce_text_field = $_POST['_text_field'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}
/* add author below title on single product */
function cn_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_subhead_to_title', 6 );
What if you simply test for the presence of the meta? If you don't save the meta in the other categories it will never display:
function cn_add_add_subhead_to_title() {
global $post;
if ( $meta = get_post_meta( $post->ID, '_text_field', true ) ) {
?>
<div class="cn-subhead">
<?php printf( __( 'by %s', 'textdomain' ), $meta ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
Helga - Thanks for your interest and help. I had a little time so I decided to try the other route of only displaying the meta if there was content instead of based on category. This may be a more flexible solution anyway, leaving the field open to any sort of subhead they want instead of limiting it only to Authors.
This the code that worked:
/* add author below title on single product */
function cn_add_add_subhead_to_title() {
global $woocommerce, $post;
$meta = get_post_meta( $post->ID, '_text_field', true );
if( $meta != '' ) {
?>
<div class="cn-subhead">
by <?php echo get_post_meta( $post->ID, '_text_field', true ); ?>
</div>
<?php
}
}
add_action( 'woocommerce_single_product_summary', 'cn_add_add_subhead_to_title', 6 );
Is there any way to add a new step to the Checkout process in WooCommerce? I need something between the cart and the Billing Details page to collect some additional information. I've researched quite a bit and turned up nothing. I can't be the only person who wants to do this.
From my tutorial on WooCommerce Custom Checkout Fields this is how to add a custom checkout field which you could use to collect additional information.
// Add a new checkout field
function kia_filter_checkout_fields($fields){
$fields['extra_fields'] = array(
'some_field' => array(
'type' => 'text',
'required' => true,
'label' => __( 'Some field' )
)
);
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'kia_filter_checkout_fields' );
// display the extra field on the checkout form
function kia_extra_checkout_fields(){
$checkout = WC()->checkout(); ?>
<div class="extra-fields">
<h3><?php _e( 'Additional Fields' ); ?></h3>
<?php foreach ( $checkout->checkout_fields['extra_fields'] as $key => $field ) : ?>
<?php woocommerce_form_field( $key, $field, $checkout->get_value( $key ) ); ?>
<?php endforeach; ?>
</div>
<?php }
add_action( 'woocommerce_checkout_after_customer_details' ,'kia_extra_checkout_fields' );
// save the extra field when checkout is processed
function kia_save_extra_checkout_fields( $order_id, $posted ){
if( isset( $posted['some_field'] ) ) {
update_post_meta( $order_id, '_some_field', sanitize_text_field( $posted['some_field'] ) );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'kia_save_extra_checkout_fields', 10, 2 );
// display the extra data in the order admin panel
function kia_display_order_data_in_admin( $order ){ ?>
<div class="order_data_column">
<h4><?php _e( 'Extra Details', 'woocommerce' ); ?></h4>
<?php
echo '<p><strong>' . __( 'Some field' ) . ':</strong>' . get_post_meta( $order->id, '_some_field', true ) . '</p>'; ?>
</div>
<?php }
add_action( 'woocommerce_admin_order_data_after_order_details', 'kia_display_order_data_in_admin' );