我使用Woocommerce插件在wordpress中开发了购物车。我需要按产品价格在购物车中显示产品,请帮我这样做
谢谢
答案 0 :(得分:0)
mmmm,就在Woo管理页面上!
Woocommerce - >调整 - >目录 - >默认产品订购
答案 1 :(得分:0)
要在Woocommerce购物车中订购价格从低到高或从高到低的产品,请尝试将以下内容添加到您的functions.php文件(或插件)中:
function 12345_cart_updated() {
$products_in_cart = array();
// Assign each product's price to its cart item key (to be used again later)
foreach ( WC()->cart->cart_contents as $key => $item ) {
$product = wc_get_product( $item['product_id'] );
$products_in_cart[ $key ] = $product->get_price();
}
// SORTING - use one or the other two following lines:
asort( $products_in_cart ); // sort low to high
// arsort( $products_in_cart ); // sort high to low
// Put sorted items back in cart
$cart_contents = array();
foreach ( $products_in_cart as $cart_key => $price ) {
$cart_contents[ $cart_key ] = WC()->cart->cart_contents[ $cart_key ];
}
WC()->cart->cart_contents = $cart_contents;
}
add_action( 'woocommerce_cart_loaded_from_session', '12345_cart_updated' );
此功能与https://businessbloomer.com/woocommerce-sort-cart-items-alphabetically-az/相似,该功能与之前发布的功能https://gist.github.com/maxrice/6541634
几乎相同答案 2 :(得分:0)
在大多数情况下,对于 WordPress 生态系统上的数据操作,答案是 wp filter
,没有 wp action
。
另外,WC_car.cart_contents
数组,在$cart_contents['data']; //WC_Product_Object
上保存了它自己的product对象,所以我们不需要再次获取product。
add_filter( 'woocommerce_get_cart_contents', 'prefix_cart_items_order' );
function prefix_cart_items_order( $cart_contents ) {
uasort($cart_contents,
fn($a, $b) =>
$a['data']->get_price() < $b['data']->get_price() ? -1 : 1
);
return $cart_contents;
}
PHP < 7
add_filter( 'woocommerce_get_cart_contents', 'prefix_cart_items_order' );
function prefix_cmp ($a, $b) {
return $a['data']->get_price() < $b['data']->get_price() ? -1 : 1;
}
function prefix_cart_items_order( $cart_contents ) {
uasort($cart_contents, 'prefix_cmp');
return $cart_contents;
}