yasin
yasin

Reputation: 79

Woocommerce change product price programmatically

I want to show the price of the products to the user with a little change. These changes are actually some calculations that are applied to the price of each product.

Everything works fine. And it shows the calculated price correctly. The only problem is that the price is wrong through the WooCommerce api.

In WooCommerce, we have a price field for each product. My code applies a calculation on that price And finally it shows to the user.

But when I call the product through rest-api, it only shows the price entered in the product price field. Not the calculation price.

This is my code:

add_filter('woocommerce_get_price_html', 'didoprice_override_price', 200, 2);
function didoprice_override_price($price='', $product){
    
    $gram       = $product->get_weight();

    if ( empty( $gram ) ){
        $gram = 100;
    }

    $all_tax_rates = [];
    $tax_classes = WC_Tax::get_tax_classes(); // Retrieve all tax classes.
    if ( !in_array( '', $tax_classes ) ) { // Make sure "Standard rate" (empty class name) is present.
        array_unshift( $tax_classes, '' );
    }
    foreach ( $tax_classes as $tax_class ) { // For each tax class, get all rates.
        $taxes = WC_Tax::get_rates_for_tax_class( $tax_class );
        $all_tax_rates = array_merge( $all_tax_rates, $taxes );
    }
    $tax_rate = $all_tax_rates[0]->tax_rate/100;
    
    $reg_price = '';
 
    if($product->is_type( 'variable' ))
    {
    $variations = $product->get_children();
    $reg_prices = array();
    $sale_prices = array();
    foreach ($variations as $value) {
    $single_variation=new WC_Product_Variation($value);
    array_push($reg_prices, $single_variation->get_regular_price());
    array_push($sale_prices, $single_variation->get_price());
    }

    sort($reg_prices);
    sort($sale_prices);
    $min_price = $reg_prices[0];
    $min_price = didoprice_calculation( $min_price, $gram );
    $min_price += $tax_rate*$min_price;
    $max_price = $reg_prices[count($reg_prices)-1];
    $max_price = didoprice_calculation( $max_price, $gram );
    $max_price += $tax_rate*$max_price;
    if($min_price == $max_price)
    {
    $reg_price = wc_price($min_price);
    }
    else
    {
    $reg_price = wc_format_price_range($min_price, $max_price);
    }
    $min_price = $sale_prices[0];
    $min_price = didoprice_calculation( $min_price, $gram );
    $min_price += $tax_rate*$min_price;
    $max_price = $sale_prices[count($sale_prices)-1];
    $max_price = didoprice_calculation( $max_price, $gram );
    $max_price += $tax_rate*$max_price;
    if($min_price == $max_price)
    {
    $sale_price = wc_price($min_price);
    }
    else
    {
    $sale_price = wc_format_price_range($min_price, $max_price);
    }
    $suffix = $product->get_price_suffix($price);
    return wc_format_sale_price($reg_price, $sale_price).$suffix;
}


    $orig_price = wc_get_price_excluding_tax( $product );

    $regular    =  wc_get_price_excluding_tax( $product, array( 'price' => $product->get_regular_price() ) ); 
    
    $sale       = wc_get_price_excluding_tax( $product, array( 'price' => $product->get_sale_price() ) ); 
   
    
    $regular            = didoprice_calculation( $regular, $gram );
    $regular += $regular*$tax_rate;
    $sale               = didoprice_calculation( $sale, $gram );
    $sale += $sale *$tax_rate;

   if ( $product->is_on_sale() ) {
    return wc_format_sale_price( $regular, $sale );
   }else{
    return wc_price( $regular );
   }



// return $price;
}




/**
 * override cart items price based on the Formula.
 */
add_action( 'woocommerce_before_calculate_totals', 'didoprice_alter_price_cart', 9999 );
 
function didoprice_alter_price_cart( $cart ) {
 
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
 
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
 
    // IF CUSTOMER NOT LOGGED IN, DONT APPLY DISCOUNT
    // if ( ! wc_current_user_has_role( 'customer' ) ) return;
 
    // LOOP THROUGH CART ITEMS & APPLY 20% DISCOUNT
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $price = $product->get_price();
        $gram = $product->get_weight();
        if ( empty( $gram ) ){
            $gram = 100;
        }
        $price = didoprice_calculation( $price, $gram );
        $cart_item['data']->set_price( $price );
    }
 
}

Upvotes: 0

Views: 414

Answers (1)

thomasberends
thomasberends

Reputation: 89

You're using the hook woocommerce_get_price_html, which is to adjust the outputted HTML for the price.

The correct hook(s) to use would be either woocommerce_get_price_excluding_tax or woocommerce_get_price_including_tax, depending on what you prefer.

So the first line would be, for example:

add_filter('woocommerce_get_price_including_tax', 'didoprice_override_price', 200, 2);

You can find more information about the hooks within WooCommerce in the documentation.

Upvotes: 0

Related Questions