在 WooCommerce 中更改购物车的顺序
Change order of Cart in WooCommerce
我想在 WordPress 上的 WooCommerce 的购物车页面上重新订购产品 table。目前列出的产品从最旧到最新(从添加到购物车的顺序)并希望有相反的顺序,希望最近添加的在顶部,最旧的在底部。
do_action( 'woocommerce_before_cart' ); ?>
<div class="cart_container">
<form class="cart-form" action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
是否可以在调用cart_url
时添加orderby
?
您可以修改 woocommerce 插件的 cart/cart.php 模板文件。当循环在购物车页面以"WC()->cart->get_cart()"开始时,你可以先把这个数组拆成一个单独的数组,反转,然后用这个反转数组倒序显示购物车产品。
建议使用此选项,因为您实际上并不与 woocommerce 对象交互,因此它涉及的处理较少。你只是把它们颠倒了。
To do any kind of cart ordering you have to use
woocommerce_cart_loaded_from_session
hook; and to reverse the
order simply use PHP array_reverse
function.
代码如下:
add_action('woocommerce_cart_loaded_from_session', 'wh_cartOrderItemsbyNewest');
function wh_cartOrderItemsbyNewest() {
//if the cart is empty do nothing
if (WC()->cart->get_cart_contents_count() == 0) {
return;
}
//array to collect cart items
$cart_sort = [];
//add cart item inside the array
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$cart_sort[$cart_item_key] = WC()->cart->cart_contents[$cart_item_key];
}
//replace the cart contents with in the reverse order
WC()->cart->cart_contents = array_reverse($cart_sort);
}
代码进入您的活动子主题(或主题)的 function.php
文件。或者在任何插件 php 文件中。
代码已经过测试并且可以工作。
希望对您有所帮助!
接受的答案有一个主要缺陷:它创建了一个竞争条件和一个无限的 AJAX 刷新循环,同时打开了多个选项卡 (see here)。
我能够解决这个问题的方法是使用动作挂钩:
- 在循环浏览购物车内容之前,我们反转内容并保存新的反转顺序
- 购物车内容循环后,我们重复步骤1恢复原来的顺序
在前端,购物车项目在三个区域(默认情况下)循环,因此我使用的操作挂钩涵盖了每个区域。
测试代码如下:
function reverse_cart_contents() {
$cart_contents = WC()->cart->get_cart_contents();
if($cart_contents) {
$reversed_contents = array_reverse($cart_contents);
WC()->cart->set_cart_contents($reversed_contents);
}
}
add_action('woocommerce_before_mini_cart', 'reverse_cart_contents');
add_action('woocommerce_after_mini_cart', 'reverse_cart_contents');
add_action('woocommerce_before_cart', 'reverse_cart_contents');
add_action('woocommerce_after_cart', 'reverse_cart_contents');
add_action('woocommerce_review_order_before_cart_contents', 'reverse_cart_contents');
add_action('woocommerce_review_order_after_cart_contents', 'reverse_cart_contents');
我想在 WordPress 上的 WooCommerce 的购物车页面上重新订购产品 table。目前列出的产品从最旧到最新(从添加到购物车的顺序)并希望有相反的顺序,希望最近添加的在顶部,最旧的在底部。
do_action( 'woocommerce_before_cart' ); ?>
<div class="cart_container">
<form class="cart-form" action="<?php echo esc_url( WC()->cart->get_cart_url() ); ?>" method="post">
<?php do_action( 'woocommerce_before_cart_table' ); ?>
是否可以在调用cart_url
时添加orderby
?
您可以修改 woocommerce 插件的 cart/cart.php 模板文件。当循环在购物车页面以"WC()->cart->get_cart()"开始时,你可以先把这个数组拆成一个单独的数组,反转,然后用这个反转数组倒序显示购物车产品。
建议使用此选项,因为您实际上并不与 woocommerce 对象交互,因此它涉及的处理较少。你只是把它们颠倒了。
To do any kind of cart ordering you have to use
woocommerce_cart_loaded_from_session
hook; and to reverse the order simply use PHParray_reverse
function.
代码如下:
add_action('woocommerce_cart_loaded_from_session', 'wh_cartOrderItemsbyNewest');
function wh_cartOrderItemsbyNewest() {
//if the cart is empty do nothing
if (WC()->cart->get_cart_contents_count() == 0) {
return;
}
//array to collect cart items
$cart_sort = [];
//add cart item inside the array
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$cart_sort[$cart_item_key] = WC()->cart->cart_contents[$cart_item_key];
}
//replace the cart contents with in the reverse order
WC()->cart->cart_contents = array_reverse($cart_sort);
}
代码进入您的活动子主题(或主题)的 function.php
文件。或者在任何插件 php 文件中。
代码已经过测试并且可以工作。
希望对您有所帮助!
接受的答案有一个主要缺陷:它创建了一个竞争条件和一个无限的 AJAX 刷新循环,同时打开了多个选项卡 (see here)。
我能够解决这个问题的方法是使用动作挂钩:
- 在循环浏览购物车内容之前,我们反转内容并保存新的反转顺序
- 购物车内容循环后,我们重复步骤1恢复原来的顺序
在前端,购物车项目在三个区域(默认情况下)循环,因此我使用的操作挂钩涵盖了每个区域。
测试代码如下:
function reverse_cart_contents() {
$cart_contents = WC()->cart->get_cart_contents();
if($cart_contents) {
$reversed_contents = array_reverse($cart_contents);
WC()->cart->set_cart_contents($reversed_contents);
}
}
add_action('woocommerce_before_mini_cart', 'reverse_cart_contents');
add_action('woocommerce_after_mini_cart', 'reverse_cart_contents');
add_action('woocommerce_before_cart', 'reverse_cart_contents');
add_action('woocommerce_after_cart', 'reverse_cart_contents');
add_action('woocommerce_review_order_before_cart_contents', 'reverse_cart_contents');
add_action('woocommerce_review_order_after_cart_contents', 'reverse_cart_contents');