I have a woocommerce website hosted on domain https://www.micropediaglobal.com.
I have one requirement that my different branch managers located at different locations should see the orders as per the locators and process their orders only.
Can someone please guide here ?
The Website Development Company website does not seems to be on wordpress, however you can bifurcate or process woocommerce orders location wise as below:
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['xerox_shop_name'] ) ) {
update_post_meta( $order_id, 'xeroxshopmeta', sanitize_text_field( $_POST['xerox_shop_name'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Xerox Center Name').':</strong> ' . get_post_meta( $order->id, 'xeroxshopmeta', true ) . '</p>';
}
//add custom field column
add_filter( 'manage_edit-shop_order_columns', 'MY_COLUMNS_FUNCTION' );
function MY_COLUMNS_FUNCTION( $columns ) {
$new_columns = ( is_array( $columns ) ) ? $columns : array();
unset( $new_columns[ 'order_actions' ] );
//edit this for your column(s)
//all of your columns will be added before the actions column
$new_columns['xeroxColumn'] = 'Xerox Center Name';
//stop editing
$new_columns[ 'order_actions' ] = $columns[ 'order_actions' ];
return $new_columns;
}
add_filter( "manage_edit-shop_order_sortable_columns", 'MY_COLUMNS_SORT_FUNCTION' );
function MY_COLUMNS_SORT_FUNCTION( $columns )
{
$custom = array(
'xeroxColumn' => 'xeroxshopmeta',
);
return wp_parse_args( $custom, $columns );
}
add_action( 'manage_shop_order_posts_custom_column', 'MY_COLUMNS_VALUES_FUNCTION', 2 );
function MY_COLUMNS_VALUES_FUNCTION( $column ) {
global $post;
$data = get_post_meta( $post->ID );
//start editing, I was saving my fields for the orders as custom post meta
//if you did the same, follow this code
if ( $column == 'xeroxColumn' ) {
echo get_post_meta( $post->ID, 'xeroxshopmeta', true );
}
}
Related
On my WooCommerce based site, I recently added some code to display the shipping methods and prices for each order on the "Edit Order" page. Now, I would like to try and add those same fields to the "New Order" email template that gets sent to the admin. This is what I've got so far:
// Capture the available shipping methods, and costs:
function action_woocommerce_checkout_update_order_meta( $order_id ) {
// Get shipping packages
$packages = WC()->shipping()->get_packages();
// Set array
$rate_labels = array();
$rate_costs = array();
// Loop through packages
foreach ( $packages as $key => $package ) {
// Loop through package rates
foreach( $package['rates'] as $rate_id => $rate ) {
// Push to array
$rate_labels[] = $rate->get_label();
$rate_costs[] = $rate->get_cost();
}
}
// NOT empty
if ( ! empty ( $rate_labels ) ) {
// Update post meta
update_post_meta( $order_id, '_available_shipping_methods', $rate_labels );
update_post_meta( $order_id, '_available_shipping_method_cost', $rate_costs );
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 );
// Make it display on the edit order page:
function action_woocommerce_admin_order_data_after_shipping_address( $order ) {
// Get meta
$rate_labels = $order->get_meta( '_available_shipping_methods' );
$rate_costs = $order->get_meta( '_available_shipping_method_cost' );
$methods = array ( $rate_labels, $rate_costs );
// True
if ( $rate_labels ) {
// Loop
echo '<p><strong>Shipping Methods: </strong>';
foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
}
}
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'action_woocommerce_admin_order_data_after_shipping_address', 10, 1 );
Adding on to that, this is what I have been trying to get working, with no luck so far:
// Add it to the new order email template
add_filter( 'woocommerce_new_order', 'custom_woocommerce_email_order_meta_fields', 10, 3 );
function custom_woocommerce_email_order_meta_fields( $rate_labels, $sent_to_admin, $order ) {
$rate_labels = $order->get_meta( '_available_shipping_methods' );
$rate_costs = $order->get_meta( '_available_shipping_method_cost' );
$methods = array ( $rate_labels, $rate_costs );
if ( $rate_labels ) {
// Loop
echo '<p><strong>Shipping Methods: </strong>';
foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
}
}
}
Use instead the following for example:
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 ){
$rate_labels = $order->get_meta( '_available_shipping_methods' );
$rate_costs = $order->get_meta( '_available_shipping_method_cost' );
$methods = array ( $rate_labels, $rate_costs );
if ( $rate_labels ) {
// Loop
echo '<p><strong>Shipping Methods: </strong>';
foreach(array_combine($rate_labels, $rate_costs) as $rate_label => $rate_cost) {
echo '<p>' . $rate_label . ' - $' . $rate_cost . '</p>';
}
}
}
}
It should work.
ACF is set up for post type on WooCommerce products. However, I am trying to add a custom column to WooCommerce orders list within Admin dashboard and add the products ACF field.
I have added the column to display after order_status, but I'm having problems getting the ACF field to display.
// ADD NEW COLUMN
add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 20 );
function custom_shop_order_column($columns)
{
$reordered_columns = array();
foreach( $columns as $key => $column){
$reordered_columns[$key] = $column;
if( $key == 'order_status' ){
$reordered_columns['my-column'] = __( 'Location','theme_domain');
}
}
return $reordered_columns;
}
Here, adding ACF to new colum.
// ADD ACF FIELD TO COLUMN
add_action( 'manage_shop_order_posts_custom_column' , 'custom_orders_list_column_content', 20, 2 );
function custom_orders_list_column_content( $column, $post_id )
{
if ( 'Location' == $column_name ){
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
echo get_field( 'location', $product_id );
}
return true;
}
Still learning and not sure how to do this, any advice?
An order generally consists of several products, therefore you cannot use $product_id directly, but you have to loop through the order items.
So you get:
/**
* Add columns
*/
function filter_manage_edit_shop_order_columns( $columns ) {
$reordered_columns = array();
foreach ( $columns as $key => $column ) {
$reordered_columns[$key] = $column;
if ( $key == 'order_status' ) {
$reordered_columns['my-column'] = __( 'Location','theme_domain' );
}
}
return $reordered_columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
/**
* Populate columns
*/
function filter_manage_shop_order_posts_custom_column( $column, $post_id ) {
// Compare
if ( $column == 'my-column' ) {
// Get order
$order = wc_get_order( $post_id );
// Is a WC_Order
if ( is_a( $order, 'WC_Order' ) ) {
// Get items
$items = $order->get_items();
// Loop through
foreach ( $items as $key => $item ) {
// Product ID
$product_id = $item->get_variation_id() > 0 ? $item->get_variation_id() : $item->get_product_id();
// Get field
$address = get_field( 'location', $product_id );
// Output
echo ($address) ? '<div>Address: ' . $address . '</div>' : '<div>Address: No address found!</div>';
}
}
}
}
add_filter( 'manage_shop_order_posts_custom_column', 'filter_manage_shop_order_posts_custom_column', 10, 2 );
We're building a clothing store. The products that will be uploaded could have label sizes from various countries, and as such we have two size taxonomies: product_size and product_size_uk.
I'm trying to set the uk size based upon the international size at the point the product is saved/updated, and running a switch to set a variable to the id of the uk size taxonomy and using
wp_set_object_terms. But this isn't working:
add_action( 'save_post', 'save_uk_size' );
function save_uk_size( $post_id ) {
if ( $post->post_type == 'product' ) {
$post = get_post($post_id);
$terms = wp_get_post_terms( $post->ID, 'product_size', array( 'fields' => 'all' ) );
if ( $terms ) {
$prod_size_int = $terms[0]->slug;
}
switch ($prod_size_int) {
...
case "FR-36":
$prod_size_uk = 805;
break;
case "FR-38":
$prod_size_uk = 806;
break;
...
}
wp_set_object_terms($post_id, $prod_size_uk, 'product_size_uk');
}
}
Can anyone steer me in the right direction? Thanks!
First you need to install Advanced Custom Fields plugin
Then for taxonomy product_size we create Taxonomy field (product_size_uk).
Now in the pages of taxonomy product_size in admin panel there is a field where we can choose which product_size_uk corresponds to this taxonomy.
3.Then we have to choose product_size_uk matching for all sizes.
And then this code
add_action( 'save_post_product', 'product_save_new_term' );
function product_save_new_term($post_id) {
remove_action( 'save_post_product', 'product_save_new_term' );
$terms = wp_get_object_terms( $post_id, 'product_size' );
$term_id = $terms[0]->term_id;
if($term_id != "") {
$key_for_field = 'product_size_' . $term_id;
$product_size_uk_value = get_field( 'product_size_uk', $key_for_field );
wp_set_object_terms($post_id, $product_size_uk_value, 'product_size_uk');
clean_post_cache( $post_id );
} else {
wp_delete_object_term_relationships( $post_id, 'product_size_uk' );
}
add_action( 'save_post_product', 'product_save_new_term' );
}
Or, if you don't want to have controls for size matching in the admin panel and it's easier for you to write everything in the code.
add_action( 'save_post_product', 'product_save_new_term' );
function product_save_new_term($post_id) {
$data_array = array(
"product_size_term_id" => "product_size_uk_term_id",
"product_size_term_id2" => "product_size_uk_term_id2",
"product_size_term_id3" => "product_size_uk_term_id3",
);
remove_action( 'save_post_product', 'product_save_new_term' );
$terms = wp_get_object_terms( $post_id, 'product_size' );
$term_id = $terms[0]->term_id;
if($term_id != "") {
$product_size_uk_value = $data_array[$term_id];
if($product_size_uk_value !="") {
wp_set_object_terms($post_id, $product_size_uk_value, 'product_size_uk');
}
clean_post_cache( $post_id );
} else {
wp_delete_object_term_relationships( $post_id, 'product_size_uk' );
}
add_action( 'save_post_product', 'product_save_new_term' );
}
In WooCommerce, I would like to add a new column to the "My Account" orders table and show the order details.
I have this code, which adds the column, but the values don't show (e.g. get_formatted_meta_data).
Can anyone help rewrite the code to make it work?
function wc_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
if ( 'order-status' === $key ) {
$new_columns['order-details'] = __( 'Order details', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'wc_add_my_account_orders_column' );
function wc_my_orders_order_details_column( $order ) {
$order_details = get_post_meta( $order->get_id(), 'order_details', true );
echo ! empty( $order_details ) ? $order_details : '–';
}
add_action( 'woocommerce_my_account_my_orders_column_order_details', 'wc_my_orders_order_details_column' );
The woocommerce_my_account_my_orders_columns filter has been deprecated since WooCommerce 2.6.0. So although it still functions you should use the woocommerce_account_orders_columns filter to add an additional column.
To populate the column with your data you can use the woocommerce_my_account_my_orders_column_ action which expects you to append the column ID of your custom column. So in your case that would be order-details. In your example you've used order_details (with an underscore instead of a hyphen). That is why your data doesn't show up.
Also it is best practice to retrieve order meta data via the internal WooCommerce getter function get_meta() instead of using get_post_meta().
add_filter( 'woocommerce_account_orders_columns', 'wc_add_my_account_orders_column', 10, 1 );
function wc_add_my_account_orders_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
if ( 'order-status' === $key ) {
$new_columns['order-details'] = __( 'Order details', 'textdomain' );
}
}
return $new_columns;
}
add_action( 'woocommerce_my_account_my_orders_column_order-details', 'wc_my_orders_order_details_column', 10, 1 );
function wc_my_orders_order_details_column( $order ) {
$item_meta = '';
foreach ( $order->get_items() as $item ) {
$item_meta .= wc_display_item_meta( $item, array( 'echo' => false ) );
}
echo !empty( $item_meta ) ? $item_meta : '-';
}
We have two plugins in a WordPress website
1) Plugin A has the code below
add_filter( 'manage_edit-book_columns', 'set_custom_edit_book_columns' );
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function set_custom_edit_book_columns($columns) {
unset( $columns['author'] );
$columns['book_author'] = __( 'Author', 'your_text_domain' );
$columns['publisher'] = __( 'Publisher', 'your_text_domain' );
return $columns;
}
function custom_book_column( $column, $post_id ) {
switch ( $column ) {
case 'book_author' :
$terms = get_the_term_list( $post_id , 'book_author' , '' , ',' , '' );
if ( is_string( $terms ) )
echo $terms;
else
_e( 'Unable to get author(s)', 'your_text_domain' );
break;
case 'publisher' :
echo get_post_meta( $post_id , 'publisher' , true );
break;
}
}
2) Now, I want to add one more column in this grid by adding code in plugin B , I don't want to edit plugin A to make fixes
3) Is it possible that we copy the code in plugin B? When I did so then there has double data in all columns. From plugin A and plugin B.
You simply have to add different data, at least different column IDs.
<?php
/**
* Plugin Name: Plugin B
*/
add_filter( 'manage_edit-book_columns', 'set_custom_edit_book_columns' );
add_action( 'manage_book_posts_custom_column' , 'custom_book_column', 10, 2 );
function set_custom_edit_book_columns($columns) {
$columns['new_column'] = __( 'Brand new column' ); // <-- Column ID
return $columns;
}
function custom_book_column( $column, $post_id ) {
switch ( $column ) {
case 'new_column' : // <-- Column ID
echo "Anything you want";
break;
}
}