如何在结账页面上显示WooCommerce购物车项目


How to display WooCommerce cart items on the checkout page?

我正在这个网站上工作,在那里我被要求将购物车项目的列表添加到结账页面。我决定添加

[woocommerce_cart] 

上方结账页面的短代码

[woocommerce_checkout]

shortcode,只需使用CSS在结账页面的购物车中隐藏"继续结账->"按钮。

然而,这带来了一个新问题。现在,当在"结账"页面上选择运输时,它默认为"购物车"页面上为购物车设置的任何运输。。。即使在结账页面上选择了新发货。

显然,这不是将购物车输出添加到结账页面的方法。

是否有显示购物车商品本身的快捷代码?

我需要编辑什么才能在结账页面上显示购物车项目?

我将回答我自己的问题,因为我用一些额外的技巧解决了这个问题。希望以后它能帮助其他人。

我没有找到简单地将购物车添加到结账页面顶部的快捷方式。我不得不直接编辑模板文件。

所以,我复制了:

/wp-content/plugins/woocommerce/templates/checkout/form-checkout.php

至:

/wp-content/mytheme/woocommerce/checkout/form-checkout.php

直接对该文件进行编辑,这样我就不会在WooCommerce升级时丢失它们。然后我从复制了表单代码

/wp-content/plugins/woocommerce/templates/cart/cart.php

并将其粘贴到我复制到主题目录的文件中:

/wp-content/mytheme/woocommerce/checkout/form-checkout.php

我希望表单出现的位置。

也许还有更优雅的方式,但这解决了我的问题。

您也可以使用挂钩

// put this in functions.php, it will produce code before the form
add_action('woocommerce_before_checkout_form','show_cart_summary',9);
// gets the cart template and outputs it before the form
function show_cart_summary( ) {
  wc_get_template_part( 'cart/cart' );
}

我创建了一个cart part.php模板,其中包含cart表,并将代码替换为wc_get_template_part("cart/cart","part")

一种更简单的方法是将以下代码添加到子主题中的functions.php文件中。

这样,您就不需要添加任何模板或更改任何核心的wooccommerce代码。

function remove_cart_collaterals() {
    if (is_checkout()) {
        remove_action('woocommerce_cart_collaterals', 'woocommerce_cross_sell_display');
        remove_action('woocommerce_cart_collaterals', 'woocommerce_cart_totals', 10);
    }
}
add_action('wp', 'remove_cart_collaterals');