Search code examples
phpwordpresswoocommercecart

WooCommerce Customization of the cart page


How to customize the cart pages in WooCommerce so that,

  1. The column with Xs (remove buttons) will move to the right.
  2. The first column will contain checkboxes
  3. Functionaly: the products that have been selected via the checkboxes go to the "Checkout" page, not everything.

Something like this: Cart-Page


Questions:

  1. Is there a setting/something that already does that ?
  2. "proceed to checkout" should be changed functionally via the plugin edit, in order to reach the mentioned functionality ?

Best Regards, Roman


Solution

  • There are a lot of hooks available in and around the table elements but sadly the rows themselves do not offer a method to e.g. add columns. Most columns have hooks which allow you to manipulate very specific details of how the data is displayed inside it.

    I suggest to override the cart.php template by copying it to a theme or child theme. Then look at the table and move around/add <td> elements.

    The proceed to checkout button is just a link to the checkout page. The cart contents live in a session that is accessible by calling WC()->cart. This returns a WC_Cart instance with methods like remove_cart_item.

    // Get items as array keyed by what $cart_item_key refers to.
    $cart_items = WC()->cart->get_cart();
    
    // Apply logic here to identify the key you need to pass as arg.
    WC()->cart->remove_cart_item($cart_item_key);
    

    Then change the cart before going to checkout. E.g. swap the proceed button with a form submit and use JS to populate hidden fields before submit, so you can use the $_POST data to edit the cart before redirecting to checkout. Or instead let JS send it to a (custom or WC) WP REST endpoint which can edit your cart before proceeding to checkout.

    These are fairly simple ideas if you are somewhat experienced in php/wp (& potentially JS). If this project is serious and you aren't too familiar with php or the internal workings of WP, I'd advise against changing cart-related logic. It quickly gets complex or breaks the checkout.

    If it's a fun/learning project, I'd say go for it! It'll be a frustrating process but also teach you a lot about WP, and its quirks to make it not completely unusable while maintaining unreasonably far backward-compatibility.

    Do note it won't teach you any modern programming, but trying to apply modern ideas to the hot mess that is WP can be super helpful for learning how to extract info from complex, large, sub-optimal or plain old codebases. (Also WP is almost un-avoidable in the real world so good to know some stuff about).