woocommerce BACS add custom field - wordpress

I need to put custom BACS field in the Thank you page and also for the emails. My country needs 'variable symbol' for the BACS method, which would be the order number. I dont want to change core files.
This is what i do:
add_filter( 'woocommerce_bacs_account_fields', 'custom_bacs_account_field', 10, 2);
function custom_bacs_account_field( $account_fields, $order_id ) {
$account_fields['variable_symbol'] = array(
'label' => 'Variabilní symbol',
'value' => $order_id
);
return $account_fields;
}
For some reason this displays the variable symbol twice.
Thank you!

Alright so this works for me:
add_filter( 'woocommerce_bacs_account_fields', 'custom_bacs_account_field', 10, 2);
function custom_bacs_account_field( $account_fields, $order_id ) {
static $call_counter = 0;
if ( $call_counter>0 ) {
return $account_fields;
}
$account_fields['variable_number' ] = array(
'label' => 'Variabliní symbol',
'value' => $order_id
);
$call_counter++;
return $account_fields;
}

Related

Product query results are not correct

I need a custom fields on the inventory tab that will be filled in with the zipcode as the location of the agent.
Armed with the references I got here, now I have that field.
what I want to ask is how to concatenate this meta data with woocommerce shortcode?
I think like :
['products limit="12" columns="4" zipcode="12345"]
where "12345" will be changing dynamically as needed (zipcode filled based on agent location).
I have tried to do something but it is not working properly.
Here's the full code.
function action_woocommerce_product_options_inventory_product_data_zipcode() {
woocommerce_wp_text_input( array(
'id' => '_zipcode',
'label' => __( 'Zipcode', 'woocommerce' ),
'description' => __( 'Please fill your zipcode.', 'woocommerce' ),
'desc_tip' => 'true',
'placeholder' => __( '12345', 'woocommerce' )
) );
}
add_action( 'woocommerce_product_options_inventory_product_data', 'action_woocommerce_product_options_inventory_product_data_zipcode' );
// Save zipcode
function action_woocommerce_admin_process_product_object_zipcode( $product ) {
// Isset
if ( isset( $_POST['_zipcode'] ) ) {
// Update
$product->update_meta_data( '_zipcode', sanitize_text_field( $_POST['_zipcode'] ));
}
}
add_action( 'woocommerce_admin_process_product_object', 'action_woocommerce_admin_process_product_object_zipcode', 10, 1 );
Try put to shortcode products :
function filter_shortcode_atts_products_zipcode ( $out, $pairs, $atts, $shortcode) {
if ( isset ( $atts['zipcode'] ) && !empty($atts['zipcode']) ) {
$out['zipcode'] = true;
} else {
$out['zipcode'] = false;
}
return $out;
}
add_filter( 'shortcode_atts_products', 'filter_shortcode_atts_products_zipcode', 10, 4);
function filter_woocommerce_shortcode_products_query_zipcode( $query_args, $atts, $type) {
if ( $type == 'products' && $atts['zipcode'] ) {
// Meta query
$query_args['meta_query'] = array(
array(
'key' => '_zipcode',
'value' => $atts['zipcode'],
'compare' => 'LIKE',
)
);
}
return $query_args;
}
add_filter( 'woocommerce_shortcode_products_query', 'filter_woocommerce_shortcode_products_query_zipcode', 10, 3 );
On testing I tried :
['products limit="12" columns="4" zipcode="12345"]
shortcode has displayed the product but does not refer to the postal code in the short code ("12345") but to all products that have a postal code.
Can somebody help me?
Thank you
Something doesn't feel right about the shortcode attribute function. I think you're setting the attribute as true and therefore it's comparing anything "truthy." Maybe just try setting the true one as the zipcode, like this:
function filter_shortcode_atts_products_zipcode ( $out, $pairs, $atts, $shortcode) {
if ( isset ( $atts['zipcode'] ) && !empty($atts['zipcode']) ) {
$out['zipcode'] = $atts['zipcode'];
} else {
$out['zipcode'] = false;
}
return $out;
}
That way it'll return the zipcode if it exists, and false if it doesn't, which the query can then use for the comparison.

function to show purchase note in Woocommerce email deprecated

I am trying to show the purchase note beneath the product in the customer_processing_order email that Woocommerce generates.
I have added the following to my functions.php file:
function sww_add_images_woocommerce_emails( $output, $order ) {
// set a flag so we don't recursively call this filter
static $run = 0;
// if we've already run this filter, bail out
if ( $run ) {
return $output;
}
$args = array(
'show_purchase_note' => true,
);
// increment our flag so we don't run again
$run++;
// if first run, give WooComm our updated table
return $order->email_order_items_table( $args );
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_images_woocommerce_emails', 10, 2 );
This works, however it is printing an error message in the email stating the following:
"Notice: WC_Order::email_order_items_table is deprecated since version
3.0! Use wc_get_email_order_items instead. in /nas/content/staging/ishgamultisite/wp-includes/functions.php on line
3853"
if I change woocommerce_email_order_items_table to wc_get_email_order_items the function doesn't work.
I'm hoping someone can tell me how I should modify the code as I'm not sure?
Bit late but if anybody is still needing to show purchase notes, then there's an easier hook:
/**
* PURCHASE NOTE
* Edits the email order items args to show purchase notes
*/
function ag_add_wc_order_email_purchase_notes( $args ) {
$args['show_purchase_note'] = true;
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'ag_add_wc_order_email_purchase_notes', 10, 1 );
The filter woocommerce_email_order_items_args contains the arguments for what to display in the order emails.
$array — Optional. (callback) => array( 'order' => $order, 'items' => $order->get_items(), 'show_download_links' => $order->is_download_permitted() && ! $args['sent_to_admin'], 'show_sku' => $args['show_sku'], 'show_purchase_note' => $order->is_paid() && ! $args['sent_to_admin'], 'show_image' => $args['show_image'], 'image_size' => $args['image_size'], 'plain_text' => $args['plain_text'], 'sent_to_admin' => $args['sent_to_admin'], )
Source: http://hookr.io/filters/woocommerce_email_order_items_args/
Tested with WooCommerce 3.6.2 and works fine.
replace return $order->email_order_items_table( $args );
with
return wc_get_email_order_items( $order, $args );

Show product images on woocommerce new order email and completed order email

I'd like show images product in a new order and completed an order for customers.
I tried this code, but show big images in email, I need eg 100 x 100 px
function sww_add_wc_order_email_images( $table, $order ) {
ob_start();
$template = $plain_text ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
wc_get_template( $template, array(
'order' => $order,
'items' => $order->get_items(),
'show_download_links' => $show_download_links,
'show_sku' => $show_sku,
'show_purchase_note' => $show_purchase_note,
'show_image' => true,
'image_size' => array( 100, 50 ),
'image_size' => $image_size
) );
return ob_get_clean();
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_wc_order_email_images', 10, 2 );
I don't know if you already fixed this issue. I'm new here at StackOverflow and saw this question when looking for some other stuff :)
But I've just added this simple snippet to my functions.php:
/**
* Display Product Image in WooCommerce Order Emails
*
* #author James Kemp (Iconic)
*/
add_filter( 'woocommerce_email_order_items_args', 'iconic_email_order_items_args', 10, 1 );
function iconic_email_order_items_args( $args ) {
$args['show_image'] = true;
return $args;
}

add_action() but function is never called

I've created a custom wordpress plugin in which I'm defining a custom post type and at one point I'm calling add_action to set custom column in the admin.
The 'manage_[custompostname]_posts_columns' is working great, but the 'manage_[custompostname]_posts_custom_column' do not.
add_filter('manage_bbc_cp_game_posts_columns', bbc_cp_game_table_head');
function bbc_cp_game_table_head( $defaults ) {
$columns = array(
'team_id' => 'Equipe',
'opponent_id' => 'Adversaire',
'gamedatetime' => 'Date / heure',
'score' => 'Score'
);
return $columns;
}
add_action( 'manage_bbc_cp_game_posts_custom_column', 'bbc_cp_game_table_content', 10, 2 );
function bbc_cp_game_table_content( $column, $post_id ) { die('it should work'); }
The second add_action returns true, but the function is never called.
I'd really appreciate if any of you had an idea of what could be the issue, or how to debug it.
Thanks,

Add custom column to woocommerce report - csv

I've got a question - is it possible to add one more column (which will present data from custom field) to csv export file in woocommerce? all necessary data is stored for each order as meta_key: 'authors_income'. I have no idea how to make it. I've tried to adapt below code, but it doesnt work:
function wpg_add_columns($cols) {
$cols['wc_settings_tab_payment_method'] = __('Payment Method', 'mytheme');
return $cols;
}
add_filter('wpg_order_columns', 'wpg_add_columns');
This should work. Let me know this goes.
<?php
//Step 1: Add field to column
function wpg_add_columns($cols) {
$cols['wc_settings_tab_authors_income'] = __('Authors Income', 'mytheme');
return $cols;
}
add_filter('wpg_order_columns', 'wpg_add_columns');
//Step 2: Add same column to settings, so that it will appear in settings page.
function wpg_add_fields($settings) {
$settings['authors_income'] = array(
'name' => __( 'Authors Income', 'woocommerce-simply-order-export' ),
'type' => 'checkbox',
'desc' => __( 'Authors Income', 'woocommerce-simply-order-export' ),
'id' => 'wc_settings_tab_authors_income'
);
return $settings;
}
add_filter('wc_settings_tab_order_export', 'wpg_add_fields');
//Note: Notice that in above step, id attribute is identical to that of array key in step 1. ( wc_settings_tab_authors_income )
//Step 3: Add it to csv.
function csv_write( &$csv, $od, $fields ) {
if( !empty( $fields['wc_settings_tab_authors_income'] ) && $fields['wc_settings_tab_authors_income'] === true ){
$authors_income = get_post_meta( $od->id, 'authors_income', true );
array_push( $csv, $authors_income );
}
}
add_action('wpg_add_values_to_csv', 'csv_write', 10, 3);

Resources