L
L
ligisayan2019-04-29 12:33:24
JavaScript
ligisayan, 2019-04-29 12:33:24

How to pass the total changed amount in the cart in woocommerce?

Hello! There is an online store on woocommerce
In the cart there is an arbitrary field for additional sales beds , depending on the increase of which the price increases


Now I need to put the price received in the total script when the order button is pressed into the $totalprice variable inside the woocommerce_before_calculate_totals hook , which is responsible for the final calculation of the amount. How to achieve this?

add_action('woocommerce_before_calculate_totals', 'set_custom_price');

function set_custom_price($cart_obj) {
  if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  return;
  $getcart = WC()->cart->get_cart();
  foreach ( $getcart as $cart_item_key => $cart_item ) {
     $cart_item['data']->set_price( ( $totalprice ) );
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WP Panda, 2019-04-29
@ligisayan

foreach ( $cart_obj->get_cart() as $cart_item ) {
     $cart_item['data']->set_price( $totalprice  );
  }

and raise the priority of the hook
add_action('woocommerce_before_calculate_totals', 'set_custom_price', 50);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question