How to modify woocommerce_before_cart action

24,279

Solution 1

Woocommerce inserts the content in cart.php into page.php in the root of your theme. :)

Solution 2

Doesn't look like woocommerce creates action hooks for 'woocommerce_before_cart' or 'woocommerce_before_cart_table', you can check this with has_action(). They seem to be there as suggestions for developers to extend upon. Should be right to remove them from cart.php (although developers might have them there for future releases or popular plugins) or if you want to use them add this to your themes functions.php.

add_action('woocommerce_before_cart', 'sample', 1);

function sample() {
    echo '<h1>hello</h1>';
}

EDIT: Just read your response to the previous answer, looks like that theme you're using might be creating the hook in its functions.php file, look for

add_action('woo_commerce_before_cart', 'sample', X);

'sample' is the name of the function that is called and X is its priority. You can either modify the output of that function or add another function to the hook.

Solution 3

For anyone who works with child theme, please note that sometimes your parent theme already override the cart.php template, especially heavily customised like Themeforest products. So don't copy the original cart.php from Woocommerce, copy it from the parent theme template.

Share:
24,279
Ken Prince
Author by

Ken Prince

Marketing guy who's learning technology.

Updated on April 10, 2021

Comments

  • Ken Prince
    Ken Prince about 3 years

    I'm trying to make my woocommerce cart template display as a full 12 column layout.

    The existing layout is using bootstrap's col-sm-8 column. I need to change it to col-sm-12.

    <main class="main col-sm-8" role="main">
      <div class="page-header">
       <h1>Cart</h1>
      </div>
    
    <div class="woocommerce">...</div>
    
    <div class="woocommerce-info">...</div>
    
    <div class="cart-collaterals">
        // shipping code etc.
    </div>
    </main>
    

    I checked out the relevant woo-templates shown here, and copied the cart.php template into my theme to override. However, it looks like I need to modify the woocommerce_before_cart action to change the <main> layout and insert the col-sm-12 class. I found the relevant actions on this woocommerce page.

    I can see from the cart.php template the action called before the <form> element as shown below:

    global $woocommerce;
    
    wc_print_notices();
    
    do_action( 'woocommerce_before_cart' ); ?>
    
    <form action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
    
    <?php do_action( 'woocommerce_before_cart_table' ); ?>
    
    <table class="shop_table cart" cellspacing="0">enter code here
    

    I'm new to php, my question is how do I modify the output of this action so I can change the layout to 12 columns?

  • Ken Prince
    Ken Prince almost 10 years
    Well that's painfully obvious. Woocommerce was just using the page.php template, I'm using Roots theme and just added the is_cart tag to the sidebar-config array and the cart is displaying the full width. Thank you.