php 如何修改 WooCommerce 购物车、结帐页面(主题部分)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26288722/
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
How to modify WooCommerce cart, checkout pages (main theme portion)
提问by mikato
I have been researching and tweaking away at my custom Wordpress theme and overridden WooCommerce templates with now WooCommerce installed to rectify a bunch of small formatting issues that occur on the WooCommerce pages. I'm down to now the cart and checkout, which yeah they use cart.php
(and whatever checkout php), but also uses the main Wordpress theme page.php
. I've been able to use is_woocommerce()
in conditionals other places but not here as I've learned the WooCommerce docs say that can't be used on checkout and cart.
我一直在研究和调整我的自定义 Wordpress 主题并覆盖 WooCommerce 模板,现在安装了 WooCommerce 以纠正 WooCommerce 页面上发生的一系列小格式问题。我现在使用购物车和结帐,是的,他们使用cart.php
(以及任何结帐 php),但也使用主要的 Wordpress 主题page.php
。我已经能够is_woocommerce()
在其他地方使用条件语句,但不能在这里使用,因为我了解到 WooCommerce 文档说不能在结帐和购物车上使用。
is_woocommerce()- Returns true if on a page which uses WooCommerce templates (cart and checkout are standard pages with shortcodes and thus are not included). http://docs.woothemes.com/document/conditional-tags/
is_woocommerce()- 如果在使用 WooCommerce 模板的页面上返回 true(购物车和结账是带有短代码的标准页面,因此不包括在内)。 http://docs.woothemes.com/document/conditional-tags/
So how can I alter the appearance of these pages? I have a "View All Posts" and a date/time at the top, and categories sidebar of page.php that I don't want showing up for obvious reasons on the checkout and cart since they don't make sense at all. What are my options?
那么我怎样才能改变这些页面的外观呢?我有一个“查看所有帖子”和顶部的日期/时间,以及 page.php 的类别侧边栏,由于明显的原因,我不想在结账和购物车上显示,因为它们根本没有意义。我有哪些选择?
Can I make it use a different template other than page.php?
我可以让它使用 page.php 以外的其他模板吗?
Is this an option? Is it bad practice... does it cause more load on every page with this loop? Where do I put it? http://saiyedfaishal.wordpress.com/2014/01/06/check-if-it-is-woocommerce-page/
这是一个选项吗?这是不好的做法......它会在这个循环的每个页面上造成更多的负载吗?我把它放在哪里? http://saiyedfaishal.wordpress.com/2014/01/06/check-if-it-is-woocommerce-page/
What's the best way to go about this? Thanks for any help! This question is somewhat related to - How to modify woocommerce_before_cart action
解决这个问题的最佳方法是什么?谢谢你的帮助!这个问题有点关系 - How to modify woocommerce_before_cart action
采纳答案by Patrick van Efferen
You can use function: wc_get_page_id( 'cart' )to get the ID of the page. This function will use the page setup as 'cart' page and not the slug. Meaning it will keep working also when you setup a different url for your 'cart' on the settings page. This works for all kind of Woocommerce special page, like 'checkout', 'shop' etc.
您可以使用函数:wc_get_page_id( 'cart' )来获取页面的 ID。此功能将使用页面设置作为“购物车”页面而不是 slug。这意味着当您在设置页面上为“购物车”设置不同的 url 时,它也会继续工作。这适用于所有类型的 Woocommerce 特殊页面,例如“结帐”、“商店”等。
example:
例子:
if (wc_get_page_id( 'cart' ) == get_the_ID()) {
// Do something.
}
回答by ken
Another way to totally override the cart.php is to copy:
完全覆盖cart.php的另一种方法是复制:
woocommerce/templates/cart/cart.php to
yourtheme/woocommerce/cart/cart.php
Then do whatever you need at the yourtheme/woocommerce/cart/cart.php
然后在 yourtheme/woocommerce/cart/cart.php 做任何你需要的事情
回答by PimPiet
回答by ratskin
WooCommerce has a number of options for modifying the cart, and checkout pages. Here are the three I'd recomend:
WooCommerce 有许多用于修改购物车和结帐页面的选项。以下是我推荐的三个:
Use WooCommerce Conditional Tags
使用WooCommerce 条件标签
is_cart()
and is_checkout()
functions return true on their page. Example:
is_cart()
和is_checkout()
函数在其页面上返回 true。例子:
if ( is_cart() || is_checkout() ) {
echo "This is the cart, or checkout page!";
}
Modify the template file
修改模板文件
The main, cart template file is located at wp-content/themes/{current-theme}/woocommerce/cart/cart.php
主要的购物车模板文件位于 wp-content/themes/{current-theme}/woocommerce/cart/cart.php
The main, checkout template file is located at wp-content/themes/{current-theme}/woocommerce/checkout/form-checkout.php
主要的结帐模板文件位于 wp-content/themes/{current-theme}/woocommerce/checkout/form-checkout.php
To edit these, first copy them to your child theme.
要编辑这些,首先将它们复制到您的子主题。
Use wp-content/themes/{current-theme}/page-{slug}.php
用 wp-content/themes/{current-theme}/page-{slug}.php
page-{slug}.php
is the second template that will be used, coming after manually assigned ones through the WP dashboard.
page-{slug}.php
是将使用的第二个模板,在通过 WP 仪表板手动分配的模板之后。
This is safer than my other solutions, because if you remove WooCommerce, but forget to remove this file, the code inside (that may rely on WooCommerce functions) won't break, because it's never called (unless of cause you have a page with slug {slug}
).
这比我的其他解决方案更安全,因为如果您删除 WooCommerce,但忘记删除此文件,则内部代码(可能依赖 WooCommerce 函数)不会中断,因为它从未被调用(除非您有一个页面蛞蝓{slug}
)。
For example:
例如:
wp-content/themes/{current-theme}/page-cart.php
wp-content/themes/{current-theme}/page-checkout.php
wp-content/themes/{current-theme}/page-cart.php
wp-content/themes/{current-theme}/page-checkout.php
回答by mikato
I've found this works well as a conditional within page.php
that includes the WooCommerce cart and checkout screens.
我发现这在page.php
包含 WooCommerce 购物车和结帐屏幕的条件下效果很好。
!is_page(array('cart', 'checkout'))
回答by fryvisuals
I used the page-checkout.php template to change the header for my cart page. I renamed it to page-cart.php in my /wp-content/themes/childtheme/woocommerce/. This gives you more control over the wrapping html, header and footer.
我使用 page-checkout.php 模板来更改购物车页面的标题。我在 /wp-content/themes/childtheme/woocommerce/ 中将其重命名为 page-cart.php。这使您可以更好地控制包装 html、页眉和页脚。