How to modify woocommerce_before_cart action

2019-07-02 14:52发布

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?

3条回答
啃猪蹄的小仙女
2楼-- · 2019-07-02 15:13

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.

查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-02 15:21

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

查看更多
我只想做你的唯一
4楼-- · 2019-07-02 15:32

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.

查看更多
登录 后发表回答