I am looking to switch off all the order status updates that get used by default on every Woocommerce order but still be able to use manual notes that I add myself whether public or private notes.
Is this possible perhaps via a hook or something similar?
This is how the order notes look at the moment:
Actually there isn't any message specific hook for order notes. Instead you can use woocommerce_order_status_changed action hook which is fired just after changing the order status, to get translated value of order note. In the next step you should use woocommerce_new_order_note_data filter hook in the function, which is hooked to the woocommerce_order_status_changed action, to compare and unset the order note in case of status change.
The final code would be something like this:
add_action('woocommerce_order_status_changed', 'remove_order_status_change_notes', 10, 3);
function remove_order_status_change_notes($order_id, $status_from, $status_to)
{
$transition_note = sprintf( __( 'Order status changed from %1$s to %2$s.', 'woocommerce' ), wc_get_order_status_name($status_from), wc_get_order_status_name($status_to) );
add_filter('woocommerce_new_order_note_data', function ($args) use ($transition_note)
{
if ($args['comment_content'] === $transition_note) {
return [];
} else {
return $args;
}
});
}
Tested and it's working
Related
I want to remove the URL from the WooCommerce My account Orders page and I really don't know how to do it by editing functions.php. Does anyone have any suggestions? Thank you!
order details url
I tried some of the answers I found on StackOverflow but I doesn't seem to work.
Remove the existing order-number column using the first hook as it prints the order number along with the link.
Add a custom order order-number column to print as per your requirement using the second hook
function remove_defualt_order_number_column($columns) {
unset($columns['order-number']);
$custom_order_number = array('order-number-custom' => __('Order', 'woocommerce'));
$columns = array_merge($custom_order_number, $columns);
return $columns;
}
add_filter('woocommerce_account_orders_columns', 'remove_defualt_order_number_column', 10, 1);
add_action('woocommerce_my_account_my_orders_column_order-number-custom', 'add_custom_order_number_column', 10, 1);
function add_custom_order_number_column($order) {
echo esc_html(_x('#', 'hash before order number', 'woocommerce') . $order->get_order_number());
}
I'm creating a really huge system in PHP and I would like to make things simpler for me and probably for the future developers.
I'm looking to achieve a functionality where when a certain action is triggered in the system, a custom function is called, just the same idea used by WordPress add_action();
This is exactly what I mean,
For example in WordPress, if one needs to execute code in the header section, a hook called wp_head is used, that's
function add_script_to_header(){
echo "this one will be print in the header";
}
add_action( 'wp_head', 'add_script_to_header' );
function another_script_on_header(){
echo "this onather script on the header";
}
add_action( 'wp_head', 'another_script_on_header' );
Now that was just an example, I would like to implement the same thing with custom PHP completely independent of WordPress, which means, my code is not in any way related to WordPress.
I've tried to use callable functions in PHP, but at some point, it gets tongue-twisting
In my case, I have a class called Orders that records, payments, shift orders from one state to another, and a lot more things, I would like to have a hook or way of triggering action with a function when certain actions are triggered.
The following code indicates some of my trials
function next_order($orderId, $status, $autoApprove = 0){
// this is function that is responsible for shifting orders from one status to another
// or from one state to another
// I would like add_action() function to be linked here, so that I can track
// order status with external functions
}
function add_action( $action_name, $function_name ){
if( $action_name == "order_next" ){
call_user_func( $function_name, $action_name);
}
if( $action_name == 'order_placed' ){
// here we can place an action for that
}
if( $action_name == 'order_paid' ){
}
}
function benson_karue( $orderId ){
echo "This is the order id ".$orderId;
}
add_action( 'order_next', 'benson_karue' );
I'd like to add an order note to log the time I added tracking number to existing order. I'm using Advanced Custom Fields to store trackings. Tried function below, and it breaks my site:
function tt_add_tracking_note( $order ) {
$tt_order = $order->id;
if (get_field('tt-track', $tt_order)) {
$tt_tracknum_note = 'Added tracking number ' . the_field('tt-track', $tt_order);
}
$tt_order->add_order_note( $tt_tracknum_note );
}
add_action( 'woocommerce_process_shop_order_meta', 'tt_add_tracking_note', 10, 2 );
What is wrong and how do I add a note the right way?
I'm trying to override, or simply customize, the admin orders list view.
I understood the method to customize is render_shop_order_columns in includes/admin/class-wc-admin-post-types.php but I cannot remove the action (method) from theme functions.php neither by a custom plugin in the plugins_loaded hook: always get bool(false) on
var_dump(remove_action( 'manage_shop_order_posts_custom_column', array( $GLOBALS['wc_admin_post_type'], 'render_shop_order_columns' ) ));
I see there is the woocommerce_order_item_name filter, but if I add a picture there (that's what I need), I get a wrong output since it is used in the title attribute of link to product too.
Could anyone please advice?
Thank you!
I was getting a wrong way...
Maybe the right one is to unset the column and add your own.
See here:
https://wordpress.org/support/topic/hooking-and-adding-new-column-on-woocommerce-order-admin-page
basically:
add_filter('manage_edit-shop_order_columns', 'show_custom_product_column', 15);
function show_custom_column($columns) {
$new_columns = (is_array($columns)) ? $columns : array();
//remove column
unset($new_columns['column_to_unset']);
//add custom column
$new_columns['custom_column'] = __( 'Translation', 'woocommerce' );
return $new_columns;
}
add_action('manage_shop_order_posts_custom_column', 'my_custom_column', 10, 2);
function my_custom_column($column) {
global $post, $woocommerce, $the_order;
switch ($column) {
case 'custom_column' :
// Custom code
break;
}
}
I was working on implementing my own pre-order system, where I set a is_preorder custom field for each product.
I was trying to modify the WooCommerce's Is_Purchasable option so that, if the product has pre-order status and it's already passed the pre-order deadline, it shouldn't be able to be purchased. I've tried a bunch of ways, but nothing seems working.
Here's something that I did (rough idea)
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable');
function preorder_is_purchasable() {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder');
if($is_preorder && "not yet passed deadline")
return true;
else
return false;
}
I don't just wanna disable the add_to_cart button, I also want to disable the functionality (should prompt error if user tried to add product by hardcoding in url).
How should I go on with this?
===========================================================================
Here's my final code:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
// if product is Pre-Order
if($is_preorder)
{
$today = date('Ymd');
// another field added using 'Advance Custom Fields' plugin
$preorder_deadline = get_field('preorder_deadline', $object->id);
if($today <= $preorder_deadline) // if not yet pass deadline
return true;
else
return false;
}
else
return $is_purchasable; // normal
Update 2019: please see dev_masta answer for correct solution nowadays.
Not sure if it solves the issue as this has to be tested on your own custom set up. But you're using get_field wrong: if it is not used inside a Loop, you should provide the post ID.
Analyzing the filter woocommerce_is_purchasable, we see that it takes two parameters, a boolean (is_purchasable) and an object (WC_Product).
Try this:
add_filter('woocommerce_is_purchasable', 'preorder_is_purchasable', 10, 2);
function preorder_is_purchasable( $is_purchasable, $object ) {
// this is a field added using 'Advance Custom Fields' plugin
$is_preorder = get_field('is_preorder', $object->id);
if($is_preorder && $is_purchasable)
return true;
else
return false;
}
The accepted answer is a bit outdated today.
Instead of using $object->id you should use $object->get_id(), otherwise you'll get a PHP notice about incorrect use.
function disable_purchased_products( $is_purchasable, $object ){
// custom function to get the array of purchased products ID's
$already_purchased = get_purchased_products();
if( in_array( $object->get_id(), $already_purchased ) ){
return false;
} else {
return $is_purchasable;
}
}
add_filter( 'woocommerce_is_purchasable', 'disable_purchased_products', 10, 2 );
I hope this will help someone, I've seen this (outdated) code all around the net..