I'm trying to customize a plugin function for my needs, and have managed to get my altered version to show up as required. Unfortunately I can't work out how to remove the original function, so now have two instances showing up.
Here's the original function from the plugin:
function aspwc_split_package_total_rows( $total_rows, $order, $tax_display ) {
if ( get_option( 'advanced_shipping_packages_display_separately', 'no' ) != 'yes' ) {
return $total_rows;
}
/** @var WC_Order $order */
$shipping_items = $order->get_items( 'shipping' );
$key_position = array_search( 'shipping', array_keys( $total_rows ) );
$new_shipping_rows = array();
foreach ( $shipping_items as $item ) {
$package_name = $item->get_meta( 'package_name' ) ?: str_replace( ':', '', __( 'Shipping:', 'woocommerce' ) );
$new_shipping_rows[ 'shipping_' . $item->get_id() ] = array(
'label' => $package_name . ':', // Package name
'value' => '<strong>' . $item->get_name() . ':</strong> ' . aspwc_get_shipping_to_display( $item, $order, $tax_display ) . '<br/><small>' . $item->get_meta( 'Items' ) . '</small>',
);
}
// Remove original shipping line - Only when new rows are set (which should only happen when package names are stored)
if ( ! empty( $new_shipping_rows ) ) {
unset( $total_rows['shipping'] );
}
// Add package line(s)
array_splice( $total_rows, $key_position, 0, $new_shipping_rows ); // splice in at position 3
return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10, 3 );
Which I'm trying to remove using:
remove_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10, 3 );
And my custom function that is replacing it:
add_filter( 'woocommerce_get_order_item_totals', 'custom_split_package_total_rows', 10, 3 );
function custom_split_package_total_rows( $total_rows, $order, $tax_display ) {
if ( get_option( 'advanced_shipping_packages_display_separately', 'no' ) != 'yes' ) {
return $total_rows;
}
/** @var WC_Order $order */
$shipping_items = $order->get_items( 'shipping' );
$key_position = array_search( 'shipping', array_keys( $total_rows ) );
$new_shipping_rows = array();
foreach ( $shipping_items as $item ) {
$package_name = $item->get_meta( 'package_name' ) ?: str_replace( ':', '', __( 'Shipping:', 'woocommerce' ) );
$new_shipping_rows[ 'shipping_' . $item->get_id() ] = array(
'label' => $package_name . ':', // Package name
'value' => aspwc_get_shipping_to_display( $item, $order, $tax_display ) . '<br/><strong>via ' . $item->get_name() . '</strong><br/><small>' . $item->get_meta( 'Items' ) . '</small>',
);
}
// Remove original shipping line - Only when new rows are set (which should only happen when package names are stored)
if ( ! empty( $new_shipping_rows ) ) {
unset( $total_rows['shipping'] );
}
// Add package line(s)
array_splice( $total_rows, $key_position, 0, $new_shipping_rows ); // splice in at position 3
return $total_rows;
}
Everything works as it should, but I can't work out why remove_filter
isn't doing anything.
Any suggestions would be very much appreciated!
remove_filter
should have only three attributes. So, try the following remove_filter code
remove_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10 );
If it still not working try it inside init
or plugins_loaded
hooks. Check the following code.
function gs_plugins_loaded(){
remove_filter( 'woocommerce_get_order_item_totals', 'aspwc_split_package_total_rows', 10 );
add_filter( 'woocommerce_get_order_item_totals', 'custom_split_package_total_rows', 10, 3 );
}
add_action('plugins_loaded', 'gs_plugins_loaded');