WooCommerce not letting me add custom column to shop_order post type - wordpress

I just started writing a plugin and ran into a problem right away. I want to add a column to the order overview page in the WooCommerce admin. The straight forward filter below doesn't do anything. Replacing shop_order with post or product, however, does show the extra column on the respective overview page.
add_filter('manage_edit-shop_order_columns', 'add_sales_column');
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Trying this on:
WC Version: 2.1.5
WP Version: 3.8.1
How to solve this?

I encountered a similar issue when adding a custom column to the WooCommerce Orders overview page from my theme's functions.php file. I was able to get a custom column to show up by increasing the filter priority above the default value of 10. Try replacing your code with the following:
add_filter('manage_edit-shop_order_columns', 'add_sales_column', 11);
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Tested on WP 3.9.1 and WC 2.1.12.
Check out the WordPress Codex entry on add_filter for more details on the behavior of filters using the $priority parameter.

The problem is that we have to wait for WooCommerce to finish its setup. This goes two ways, first running all our hooks inside a plugins_loaded main hook, and then setting the priority of our hooks for something greater than WC's.
add_action( 'plugins_loaded', 'setup_so_22237380' );
function setup_so_22237380()
{
// Just to make clear how the filters work
$posttype = "shop_order";
// Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
// Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column", 'column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns", 'column_sort_so_22237380' );
}
function column_set_so_22237380( $columns )
{
$columns['order_sales'] = "Sales";
return $columns;
}
function column_display_so_22237380( $column_name, $post_id )
{
if ( 'order_sales' != $column_name )
return;
$sales_information = 'Your custom get_order_sales_information($post_id)';
if ( $sales_information )
echo "<strong style='color:#f00;'> $sales_information </strong>";
}
function column_sort_so_22237380( $columns )
{
$columns['order_sales'] = 'order_sales';
return $columns;
}
Setting the column as sortable is optional (manage_edit-{$posttype}_sortable_columns) and needs an extra action hook to make it work (pre_get_posts). This may be a complex function to build and requires its own research.

It will working fine if you include all the order hook on
add_action( 'admin_init', 'setup_all_order_column_hook' );
function setup_all_order_column_hook(){
//Just to make clear how the filters work
$posttype = "shop_order";
//Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
//Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column",column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns",'column_sort_so_22237380' );
}

Related

How to set a WooCommerce email template as default for all emails

I’m looking for a way to send all WordPress emails using a custom WooCommerce template so all emails will look the same.
The path to the template would be:
woocommerce/emails/my-custom-woocommerce-template.php
Does it have to all be templatized in a single file? If not, a combination of these entry points can probably get you the standardization you're looking for:
email-header.php lets you customize the start of the email including the header image (if you need to do more than change its URL). It opens the layout tags for the rest of the email content
email-footer.php lets you customize the footer, and closes the layout tags started in the header.
email-styles.php or the woocommerce_email_styles filter let you customize the CSS (see some gotchas in my article here).
Various actions/filters are scattered throughout the emails for customizing individual parts.
You can use the below function. It is working
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
// List of all templates that should be replaced with custom template
$woo_templates = array(
'emails/admin-new-order.php',
'emails/admin-failed-order.php',
'emails/admin-cancelled-order.php',
'emails/customer-completed-order.php',
'emails/customer-new-account.php',
'emails/customer-note.php',
'emails/customer-on-hold-order.php',
'emails/customer-processing-order.php',
'emails/customer-refunded-order.php',
'emails/customer-reset-password.php',
);
//Check whether template is in replacable template array
if( in_array( $template_name, $woo_templates ) ){
// Set your custom template path
$template = your_template_path.'emails/my-custom-woocommerce-template';
}
// Return what we found
return $template;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
add_filter( 'wp_mail', 'your_wp_mail_action' ); // $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
function your_wp_mail_action( $args ) {
global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
$your_prefix_your_email_args = $args;
ob_clean();
get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
$args['message'] = ob_get_clean();
// ... your logic
return $args;
}
To view and update email settings, log into your website dashboard. In the left-hand menu, click on WooCommerce → Settings.
There, you’ll find several options tabs at the top. Click Emails to view the following templates
you can custom all as you want

Set billing_address_2 label in Woocommerce checkout

I'm trying to set / show a label for the billing_address_2 field on the Woocommerce checkout page, but can't find a way to do this. Does anyone know a solution?
The code below (which works fine on other fields) does not do the job.
add_filter( 'woocommerce_checkout_fields' , 'custom_rename_wc_checkout_fields' );
function custom_rename_wc_checkout_fields( $fields ) {
$fields['billing']['billing_address_2']['label'] = 'Building number';
return $fields;
}
Updating this question with an answer for the sake of future readers.
As of WooCommerce 3.5.1, commit 87054ece9a4c05db72e139730ed1764c63fab635 adds the 'screen-reader-text' label_class to both Billing & Shipping Address 2 fields for accessibility purposes as per Issue #21182. This class will keep the label hidden unless it is also changed (e.g. make it blank, like the Address 1 field).
Here's how I updated my functions.php to get the billing/shipping_address_2 labels back (I'm using the separate billing/shipping filters as I have made changes to other fields I didn't include in the code for brevity).
// Billing Fields.
add_filter( 'woocommerce_billing_fields', 'custom_woocommerce_billing_fields' );
function custom_woocommerce_billing_fields( $fields ) {
$fields['billing_address_2']['label'] = 'Address 2';
$fields['billing_address_2']['label_class'] = '';
return $fields;
}
// Shipping Fields.
add_filter( 'woocommerce_shipping_fields', 'custom_woocommerce_shipping_fields' );
function custom_woocommerce_shipping_fields( $fields ) {
$fields['shipping_address_2']['label'] = 'Address 2';
$fields['shipping_address_2']['label_class'] = '';
return $fields;
}

template_include stop working on woocommerce update

The following code is to override the Product Details page template, It was working since the last update on WooCommerce. Can anyone help me out on this, thanks in advance.
add_filter('template_include', 'wpautomate_plugin_templates');
function wpautomate_plugin_templates( $template )
{
$plugin_path='';
$reflector = new ReflectionClass('Ze_Single_Product_Layout');
$file_name=plugin_dir_path($reflector->getFileName());
$plugin_path=$file_name;
$post_types = array('product');
$template_id=get_post_meta( get_the_ID(), '_product_layout', true );
if (is_singular('product') && !empty($template_id))
{
//render custom template for single product
$template = $plugin_path . 'template/woo-single-page.php';
}
return $template;
}//end of function
You need to call this filter
add_filter('template_include', 'wpautomate_plugin_templates');
with init action hook
add_action('init','load_custom_template_woo');
function load_custom_template_woo(){
add_filter('template_include', 'wpautomate_plugin_templates');
}
Thanks
For me, I had to set the priory for the hook callback greater than 10, like this
// priority = 11
add_action('template_include', 'wpautomate_plugin_templates', 11);

WooCommerce line_total and line_subtotal

Is there any way to change the line_total and line_subtotal in WooCommerce once an order is in add to cart.
Thanks in advance.
There two hook for check out page where you can modify line total and line sub total.
woocommerce_calculate_totals
function action_woocommerce_calculate_totals( $fee ) {
// add your logic
};
// add the action
add_action('woocommerce_calculate_totals','action_woocommerce_calculate_totals', 10, 1 );
woocommerce_cart_subtotal
function filter_woocommerce_cart_subtotal( $array, $int, $int ) {
// implement your logic
return $array;
};
// add the filter
add_filter( 'woocommerce_cart_subtotal', 'filter_woocommerce_cart_subtotal', 10, 3 );

Admin-side hooks don't work (WordPress)

I want to send an email whenever a file is attached to a certain CPT, however I can't make add_attachment hook work. In fact I can't seem to make any dashboard hook (such as post_updated) work. The code below does nothing whenever a file is attached to a post or post gets updated:
add_action( 'add_attachment', 'goldorak' );
add_action( 'post_updated', 'goldorak' );
function goldorak() {
echo 'Fired!';
echo "<script>alert('Fired!');</script>";
}
Note: my attachment is a file field created with Advanced Custom Fields plugin.
I am not sure ACF fires the same actions as the normal wordpress. Here is the ACF version of your code:
add_action( 'acf/save_post', 'goldorak', 15 ); // The saving is done with priority 10, so 15 is after the save to DB, 5 before it.
function goldorak() {
die('test');
}
But in your case, the hook acf/update_value/type=file would simplify your task:
add_action('acf/update_value', 'acf_hook_update_value', 1, 3);
function acf_hook_update_value($new_value, $post_id, $field_options) {
$key = $field_options['key']; // internal key name
$name = $field_options['name']; // pretty name
$old_value = get_field($key, $this->post_id, false);
$new_value = stripslashes($new_value);
if ($new_value != $old_value) {
die('test'); // Do something ...
}
}

Resources