twitter-bootstrap 如何修改woocommerce_before_cart动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23521142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:40:58  来源:igfitidea点击:

How to modify woocommerce_before_cart action

phpwordpresstwitter-bootstrapwoocommerce

提问by Ken Prince

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

我正在尝试使我的 woocommerce 购物车模板显示为完整的 12 列布局。

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

现有布局使用引导程序的 col-sm-8 列。我需要将其更改为 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_cartaction to change the <main>layout and insert the col-sm-12class. I found the relevant actionson this woocommerce page.

我查看了此处显示的相关 woo 模板,并将 cart.php 模板复制到我的主题中以进行覆盖。但是,看起来我需要修改woocommerce_before_cart操作以更改<main>布局并插入col-sm-12类。我在这个 woocommerce 页面上找到了相关操作

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

我可以从cart.php模板中看到在<form>元素之前调用的操作,如下所示:

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?

我是 php 新手,我的问题是如何修改此操作的输出,以便将布局更改为 12 列?

回答by CaitlinHavener

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

Woocommerce 将 cart.php 中的内容插入到主题根目录下的 page.php 中。:)

回答by Luke Thompson

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.

看起来 woocommerce 不会为“woocommerce_before_cart”或“woocommerce_before_cart_table”创建动作挂钩,您可以使用 has_action() 进行检查。它们似乎是作为开发人员扩展的建议。从cart.php 中删除它们应该是正确的(尽管开发人员可能会将它们放在那里用于未来的版本或流行的插件),或者如果您想使用它们,请将其添加到您的主题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

编辑:只需阅读您对上一个答案的回复,看起来您正在使用的主题可能正在它的 functions.php 文件中创建钩子,查找

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.

'sample' 是被调用函数的名称,X 是它的优先级。您可以修改该函数的输出或向钩子添加另一个函数。

回答by Sang ??ng

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.

对于使用子主题的任何人,请注意有时您的父主题已经覆盖了 cart.php 模板,尤其是像 Themeforest 产品这样高度定制的模板。所以不要从 Woocommerce 复制原始的cart.php,从父主题模板复制它。