calculate_totals() not recalculating
-
I have written a plugin that allows a user to change the quantity of items in an order that has already been placed.
Below is a snippet of my code:
{ $subscription_id = $_POST['sub_id'] ?? false; $item_id = $_POST['line_item_id'] ?? false; $qty = $_POST['qty'] ?? false; if(empty($subscription_id) || empty($item_id) || empty($qty)){ echo "Subscription ID, Item ID and Quantity are required fields."; wp_die(); } $subscription = wcs_get_subscription($subscription_id); $item = $subscription->get_item($item_id); $item_data = $item->get_data(); $_qty_before_update = $item_data['quantity']; $product_id = $item_data['product_id']; $variation_id = $item_data['variation_id']; if(intval($qty) === intval($_qty_before_update)){ ?><div class='alert alert-warning'><i class='fa fa-exclamation-circle'></i> Nothing Updated.<hr/>Quantity didn't change.</div><?php wp_die(); } $product = wc_get_product($product_id); if($product->get_type() === 'variable-subscription'){ $variations = $product->get_available_variations(); foreach($variations as $variation){ if($variation['variation_id'] == $variation_id){ $price = $variation['display_regular_price']; $title = $product->get_title(); $attributes = $variation['attributes']; foreach($attributes as $attribute) $title .= ", $attribute"; break; } } } else { $price = $product->get_regular_price(); $title = $product->get_title(); } $sub_total = intval($qty) * floatval($price); global $wpdb; $queries = array(); $sql = "UPDATE %s SET meta_value='%s' WHERE order_item_id = '%s' AND meta_key = '%s'"; $queries[] = sprintf($sql,"{$wpdb->prefix}woocommerce_order_itemmeta",esc_sql($qty),$item_id,"_qty"); $queries[] = sprintf($sql,"{$wpdb->prefix}woocommerce_order_itemmeta",$sub_total,$item_id,"_line_subtotal"); $queries[] = sprintf($sql,"{$wpdb->prefix}woocommerce_order_itemmeta",$sub_total,$item_id,"_line_total"); foreach($queries as $query){ $wpdb->get_results($query); } $subscription->add_order_note(sprintf("Changed quantity for %s from %s to %s. (%s)",$title,$_qty_before_update,$qty,$item_id),0,1); $subscription->save(); $subscription->calculate_totals(); $subscription->save(); $new_subscription_total = $this->recalculate_subscription_totals($subscription_id); update_post_meta($subscription_id,"_order_total",$new_subscription_total); $this->update_subscription_order_note($subscription_id); ?><div class='alert alert-info'><i class='fa fa-info-circle'></i> Quantity Updated!</div><?php wp_die(); }
The quantity gets updated correctly in the orders table prior to the
$subscription->save();
call, the order in the $subscription variable should be recalculating the correct total for the order, but its leaving the total as the same prior to the order.Therefor it leads me to believe that the problem lies within the
$subscription->calculate_totals();
function. This function is located in the WC classwp-content/plugins/woocommerce/includes/abstracts/abstract-wc-order.php
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘calculate_totals() not recalculating’ is closed to new replies.