Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify woocommerce_before_cart action

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?

like image 861
Ken Prince Avatar asked Nov 30 '22 19:11

Ken Prince


2 Answers

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

like image 136
CaitlinHavener Avatar answered Dec 04 '22 01:12

CaitlinHavener


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.

like image 27
Luke Thompson Avatar answered Dec 04 '22 00:12

Luke Thompson