2

I want to hide or remove a column from the cart of woocommerce, Im making a plugin for woocommerce so it wouldn't be good to modify directly cart.php or functions.php (which would be the solution) but when the woocommerce plugin release an update the code will be gone, right now i just could hide the item for price but not the entire column.

add_filter( 'woocommerce_cart_item_price', '__return_empty_string' ); 

Aditionally to this I could do it with CSS display:none but people will be watch the items if they inspect the code, currently the cart is like this

enter image description here

.

Do you know how to hide the entire price column from the cart using hooks, thanks.

MY ANSWER :

With the help of Ben HartLenn I add to my plugin this code in order to replace the cart.php for my plugin's cart.php and some other file I need to replace

define( 'PLUGIN_DIR', plugin_dir_path( __FILE__ ) );

add_filter( 'woocommerce_locate_template', 'plugin_locate_template', 10, 3 );

function plugin_locate_template( $template, $template_name, $template_path ) {

  global $woocommerce;
  $_template = $template;

  if ( ! $template_path ) $template_path = $woocommerce->template_url;

  $plugin_path  = PLUGIN_DIR . '/woocommerce/';

  $template = locate_template(
    array(
      $template_path . $template_name,
      $template_name
    )
  );

  // Get the template from this plugin, if it exists
  if ( ! $template && file_exists( $plugin_path . $template_name ) )
    $template = $plugin_path . $template_name;

  // Use default template
  if ( ! $template )
    $template = $_template;

  // Return what we found
  return $template;

}

Now I create in this case the folder cart and inside of it a cart.php file modified by me deleting the column Price and Total, it works with any file you need to replace.

You create that file on

/plugins/yourplugin/woocommerce/cart/cart.php

and the code will automatically replace the default woocommerce cart.php for your file, works with any file is inside woocommerce/templates/, that way you'll have your own customize woocommerce template files.

If this solution doesn't work and this code is not loading your own customized files it might be because the template (like avada for example) is already replacing the cart.php or someother file, check out the path

/themes/your_theme/woocommerce

To be sure the template is not replacing the same file you try to replacing with your plugin.

Thanks Ben for your help :D

1
  • A simpler solution might be to combine the two original solutions, by removing the cart contents using the hook you mentioned, and then using CSS to hide the empty column
    – shea
    Commented Nov 22, 2017 at 13:12

1 Answer 1

1

After looking in cart.php, it doesn't look like you can do that with hooks, as there is static html that wraps around the product price content. That product price content is what is affected by the woocommerce_cart_item_price hook that you've tried, but not the containing static html markup for the column structure itself.

The good news is that you can override the cart.php template file by putting it in your theme, and then removing the static html output for the Price column in your version of the template file. You won't have to worry about losing your changes this way, but you will have to keep an eye out for and merge any changes that Automattic makes to the Woocommerce plugins cart.php template file in the future.

To do the template override, just copy the Woocommerce plugin file from /wp-content/plugins/woocommerce/templates/cart/cart.php, and move it over to your theme so that Woocommerce recognizes your version as an override. In this case you would put the cart.php file into /wp-content/themes/your-theme/woocommerce/cart/cart.php after creating the woocommerce/cart/ directories inside your theme. After that Woocommerce should load your version of the file from your theme, instead of the plugin version of cart.php.

Finally you can just remove or comment out line 96 to line 100(in the latest version of cart.php), and that should remove the whole Price column from your output. There's no way for me to tell, but you might need to touch up other visual things after doing this.

Here's a little more info on overriding Woocommerce template files just in case: https://docs.woocommerce.com/document/template-structure/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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