Home > other >  Woocommerce sort by price after set custom discount on product
Woocommerce sort by price after set custom discount on product

Time:02-05

I added a custom discount in all products via functions.php, because we want to run a campaign based on specific brand.

Prices are showing in product page, product list and cart page.

//Change price in product and product list
add_filter( 'woocommerce_get_price_html', 'set_discount_by_brand', 9999, 2 );
function set_discount_by_brand( $price_html, $product ) {

    $discount = 0.25;
    $brand = $product->get_attribute('pa_brand');
    if ($brand == 'MY BRAND') {
        $regular_price = $product->get_regular_price();
        $sale_price = $regular_price*(1-$discount);
        $price_html = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) : $sale_price ) . '</ins>';
    }
    
    return $price_html;
}

//Change price in cart item
add_action( 'woocommerce_before_calculate_totals', 'discount_price_cart_by_brand', 9999 );
function discount_price_cart_by_brand( $cart ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
 
    // Apply discount to cart
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $product_id = $product->get_id();
        $discount = 0.25;
        $brand = $product->get_attribute('pa_brand');
        if ($brand == 'MY BRAND') {
            $regular_price = $product->get_regular_price();
            $sale_price = $regular_price*(1-$discount);
            $cart_item['data']->set_price( $sale_price );
        }
    }
}

My problem is when i try to sort products by price (asc & desc). I know that sorting based in meta_key _price but i don't want to make any change in database.

I also thought to add a custom meta_key named _sorting_price and calculate again all prices based in discount.

//Sort by custom meta_key
add_filter('woocommerce_get_catalog_ordering_args', function ($args) {
    $orderby_value = isset($_GET['orderby']) ? wc_clean($_GET['orderby']) : apply_filters('woocommerce_default_catalog_orderby', get_option('woocommerce_default_catalog_orderby'));
        if ('price' == $orderby_value) {
            $args['orderby'] = 'meta_value_num';
            $args['order'] = 'ASC';
            $args['meta_key'] = '_sorting_price';
        }
    
        if ('price-desc' == $orderby_value) {
            $args['orderby'] = 'meta_value_num';
            $args['order'] = 'DESC';
            $args['meta_key'] = '_sorting_price';
        }
    return $args;
});

But i would like to try first another option instead to add 10000 rows in postmeta table.

Does anyone know if i can do it via functions.php?

Thank you

CodePudding user response:

Because you are doing this via filter and just adjusting the display price there is no way to do it via built in functions.

You could also use a separate table to do this vs adding more data to the postmeta. This would make it easier to just remove later as you could just delete the table when done.

You could update all the products to be on sale programmatically which would work with the default sort.

  •  Tags:  
  • Related