Is there a way to override woocommerce core functions in functions.php ?

for example file location in: wp-content/plugins/woocommerce/includes/class-wc-order-item-shipping.php

there is a following public function

public function set_method_title( $value ) {
  $this->set_prop( 'name',  wc_clean($value) );
  $this->set_prop( 'method_title',  wc_clean($value)  );

How to change it to this:

public function set_method_title( $value ) {
  $this->set_prop( 'name',  $value );
  $this->set_prop( 'method_title',  $value  );

You can't replace just any plugin function with your own code, unless:

  1. The plugin is wrapped in a function_exists() check. This makes the function pluggable, which means that if you define it first the plugin won't try to define it again, and yours will be used. This function is not pluggable.
  2. There is a filter somewhere, indicated by the use of the apply_filters() function, that lets you replace a value. That value might be the output of a function, and by using the filter you could replace the output with your own.

The specific function you identify does not have either, so cannot be replaced. I'd consider that WooCommerce probably has a very good reason for applying wc_clean() to these properties, and if it weren't there things might not function as expected, or it could pose a security risk.

| improve this answer | |

Removing those functions is very ill-advised. It is prepping the data for entry into the database. Those entries only accept simple text, so the wc_clean function runs the WordPress sanitize_text_field function. As per WordPress documentation, this function:

  • Checks for invalid UTF-8
  • Converts single < characters to entities
  • Strips all tags Removes line breaks, tabs, and extra whitespace
  • Strips octets

Even if you could remove them, they may not save correctly in the database. It would also open you up to possible attacks because it would allow potentially harmful code to be injected. Data should always be sanitized before saving it, and the data must match the table column settings to avoid some being cut off.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.