Search
 
SCRIPT & CODE EXAMPLE
 

PHP

wp wc php sort products archive cheapest price

add_action( 'woocommerce_cart_loaded_from_session', 'prefix_cart_order_prod_cat' );
/**
 * WooCommerce - sort cart order by price
 * @link https://stackoverflow.com/questions/17194899/woocommerce-cart-page-display-products-order-by-product-price/57136404#57136404
 */
function prefix_cart_order_prod_cat() {

    $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;

}
Comment

PREVIOUS NEXT
Code Example
Php :: how to convert php code to html 
Php :: when user click back clear form data laravel 
Php :: php echo variable name 
Php :: cara looping abjad with range kapital 
Php :: launch new tab and refresh original page codeigniter 
Php :: __sleep and __wakeup 
Php :: php numeros enteros 
Php :: hook into admin add order item / product on add/submit 
Php :: PhpDebugBar is not defined nginx 
Php :: Prevent infinite loop when saving Statamic entry 
Php :: sendinblue send email 
Php :: smarty shorthand if 
Php :: remove public/index.php from laravel url 
Php :: Lity in Wordpress 
Php :: how can i check that a json file already has something inside 
Php :: php display result from html 
Php :: laravel , How can I increment and decrement value with unique id 
Php :: php options list view sidebar (240 x 500), list view results (600 x 180), listing page (450 x 200) 
Php :: symfony translation variable in twig 
Php :: laravel form collective add asterisk 
Php :: veue laravel remove #/ 
Php :: expression is not allowed as parameter 
Php :: PHP SSRF Wrapper/URL Schema 
Php :: php if 2 files in dir unlink the olderst 
Php :: php 5.6 xampp 
Php :: select all matched text phpstrom 
Php :: acf select multiple choice array in loop 
Php :: php get header language 
Php :: Laravel polimorfic faker 
Php :: how to restrict user to some pages using php 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =