Support » Plugin: WooCommerce » How to override "class-wc-meta-box-order-data.php" in child-theme?

  • Resolved Jason Wong

    (@eljkmw)


    I like to know how to override the /woocommerce/includes/admin/meta-boxes/class-wc-meta-box-order-data.php file within a child-theme. Is that even possible, or I’ve no choice but to break this plugin’s core file?

    I’m doing this because I want to include a single line break between $order->paid_date and $ip_address.

    Thank you in advance.

    https://wordpress.org/plugins/woocommerce/

Viewing 15 replies - 1 through 15 (of 15 total)
  • In addition to the above, I applied the following lines of code to remove the “Regenerate Downloads Permission” action from the Order Actions, which was in vain.

    // Remove "Regenerate Download Permissions" from Order Action
    function wc_remove_order_action( $actions ) {
    	unset ( $actions['regenerate_download_permissions'] );
    	return $actions;
    }
    add_action( 'woocommerce_order_actions', 'wc_remove_order_action', 10, 1 );

    That’s because the regenerate_download_permissions option was added separately from the array of woocommerce_order_actions.

    Plugin Author Mike Jolley

    (@mikejolley)

    Core admin files cannot be overridden via themes.

    There are some websites that don’t sell intangible products (such as digital documents, photos, software, etc.), which means there is no need for this order action. So, is there a possibility to include the “Regenerate Download Permissions” portion into a hook in future releases?

    Caleb Burks

    (@icaleb)

    Automattic Happiness Engineer

    Not sure I understand the scenario here. Are you just wanting to remove this from the list: http://cld.wthms.co/1dQ2V/5jtdxDYA?

    Could just hide is with some CSS.

    Yes, Sir. That’s the pesky link that I wanna rid off. Using CSS to set “display: none;” is somewhat like child’s play. Since there’s no hook, looks like I’ve no other choice but to be playful …

    (><)”

    In the end I used CSS, and avoided JS. This is what I came up with:

    /* WooCommerce : Order Data - Actions
    To hide "Regenerate Download Permissions" option */
    select[name="wc_order_action"] option:last-child { display: none; }

    Stuck this into admin.css of the child-theme, with the following snippet in functions.php

    /* Enqueue admin styles */
    function admin_custom_styles() {
    	wp_enqueue_style( 'admin-styles', get_stylesheet_directory_uri(). '/css/admin.css' );
    }
    add_action( 'admin_head', 'admin_custom_styles' );

    The result hides the “Regenerate Download Permissions” option from the Order Actions selector. So much of trying to avoid any child’s play … Sigh. (–“)

    Hey @mikejolley

    So what’s the best way in this case? I’m also want to override the function “output” of the class:

    woocommerce-subscriptions/includes/admin/meta-boxes/class-wcs-meta-box-subscription-data.php

    because I’ve added a change in just one line and in this case I can’t solve it using CSS as @eljkmw

    Please, share with us more information please!
    Thanks heaps!

    G’day @imoyano

    I added the CSS into wp-content/themes/child-theme/css/admin.css
    Then, the snippet into wp-content/themes/child-theme/functions.php

    Please check that these are correct in order for it to work.

    • This reply was modified 3 years, 6 months ago by Jason Wong.
    Plugin Author Mike Jolley

    (@mikejolley)

    @imoyano

    > So what’s the best way in this case? I’m also want to override the function “output” of the class:

    Really depends on what you’re doing. if you’re trying to modify core/extension plugin files which don’t allow customisation via hooks (which is reasonable if it’s admin facing or not intended to be changed) you would need to run a custom version of the extension.

    Thanks @mikejolley

    Well, I thought there was a way to override the class or only that function. In this case I only changed a line from:

    if ( ! $subscription->can_be_updated_to( $status ) && ! $subscription->has_status( str_replace( 'wc-', '', $status ) ) ) {

    to

    if ( $status != "wc-active" && ! $subscription->can_be_updated_to( $status ) && ! $subscription->has_status( str_replace( 'wc-', '', $status ) ) ) {

    So I don’t want to create a custom version of the extension…. is there another option??

    PS: Thanks @eljkmw I think in this case I can’t do that via CSS :/

    Plugin Support thenbrent

    (@thenbrent)

    @imoyano you can achieve what you want indirectly. From your snippets, it looks like all you want to do is make sure that the active status isn’t displayed as an option on the WooCommerce > Edit Subscription screen. This is effectively the same as saying a subscription can’t be updated to wc-active status from that screen.

    You can use the 'woocommerce_can_subscription_be_updated_to_active' filter to enforce that.

    You should also add some additional code to make sure you are only acting on that filter when in the WCS_Meta_Box_Subscription_Data::output() function (i.e. on the WooCommerce > Edit Subscription screen).

    To do that, you can use something like the code below:

    function eg_add_status_filter( $post_type, $context, $post_object ) {
    	if ( 'shop_subscription' === $post_type && 'normal' == $context ) {
    		add_filter( 'woocommerce_can_subscription_be_updated_to_active', '__return_false', 100 );
    	}
    }
    add_action( 'do_meta_boxes', 'eg_add_status_filter', 0, 3 ); // 
    
    function eg_remove_status_filter() {
    	if ( 'shop_subscription' === $post_type && 'normal' == $context ) {
    		remove_filter( 'woocommerce_can_subscription_be_updated_to_active', '__return_false', 100 );
    	}
    }
    add_action( 'woocommerce_admin_order_data_after_order_details', 'eg_remove_status_filter' );
    

    Note: that code is not tested or meant to be a final working snippet, it’s intended to illustrate the workaround and provide you with a good head start on the code needed.

    The eg_add_status_filter() function in that snippet attaches WP’s __return_false() function to make sure no subscriptions can be updated to the “active” status. It is attached to the ‘do_meta_boxes’ hook, which is triggered just before WCS_Meta_Box_Subscription_Data::output() is called to print the status select box on the the Edit subscription screen.

    The __return_false() callback is then removed in eg_remove_status_filter with eg_add_status_filter() just after printing the status select field on the the Edit subscription screen but removing that callback on the 'woocommerce_admin_order_data_after_order_details' hook.

    Wow mate, thanks for your reply, we are very close now.

    Actually, what I need to do is to be able to change the subscription status from “cancelled” to “active”. One part is achieved using your code (in the view side):

    function eg_add_status_filter( $post_type, $context, $post_object ) {
    	add_filter( 'woocommerce_can_subscription_be_updated_to_active', '__return_true', 100 );
    }
    add_action( 'do_meta_boxes', 'eg_add_status_filter', 0, 3 ); 

    However, in the server side, I need to override the “update_status” function (class-wc-subscription.php), I need to make this simple change in line 305:

    if ( ! $this->can_be_updated_to( $new_status ) ) {

    to

    if ( ! $this->can_be_updated_to( $new_status ) && !( $old_status == 'cancelled' && $new_status == 'active' ) ) {

    Your suggested code has been so useful so far, so hope you can help me in the second part.

    Thanks heaps!!

    Plugin Support thenbrent

    (@thenbrent)

    I need to make this simple change in line 305:

    You can use a very similar approach to the one I mentioned previously – find hooks before/after when that code is called, then use them to attach a callback to the 'woocommerce_can_subscription_be_updated_to_' . $status hook.

    For this case, the hook you’ll want is 'woocommerce_can_subscription_be_updated_to_cancelled'. You’ll have to find the appropriate hooks to attach your callback on your own though. 🙂

    cool… thanks heaps @thenbrent !

    Hi to all,

    I’ve returned to my original thread here, and I want to inform the WooCommerce developers about a small glitch on line 405 in class-wc-meta-box-order-data.php

    The glitch happens to be the display of special characters, e.g., &amp (and character). I recently found out when an order came in with this character in the Order Comments, and it will show “&amp” instead of just “&”. Since there isn’t hook to override this, I hacked this file instead, and replace line 405 with
    echo '<p><strong>' . __( 'Customer provided note:', 'woocommerce' ) . '</strong> ' . htmlspecialchars_decode( nl2br( esc_html( $post->post_excerpt ) ) ) . '</p>';

    Hope this fix can be incorporated in the next WooCommerce release. Cheers!

    • This reply was modified 3 years ago by Jason Wong.
Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘How to override "class-wc-meta-box-order-data.php" in child-theme?’ is closed to new replies.