danyo
danyo

Reputation: 5846

Edit woocommerce line total

I am trying to edit the line total of a product in the cart. But for some reason the output is not getting updated. Here is what I have:

foreach ( $woocommerce->cart->get_cart() as $key => $value ) {
  $value['data']->price = 10;
  $value['line_total'] = 1;
  $value['line_subtotal'] = 1;
}

Updating $value['data']->price, works fine. But when I try and update $value['line_total'] or $value['line_subtotal'], the output is the same.

Any ideas?

Upvotes: 3

Views: 3781

Answers (1)

solomonculaste
solomonculaste

Reputation: 313

Change the $value['line_total'] or $value['line_subtotal'] to $woocommerce->cart_contents[ $key ]['line_total'] and/or $woocommerce->cart_contents[ $key ]['line_subtotal']

You may also want to modify the cart contents total by overriding the value of $woocommerce->cart_contents_total

Those variables will just alter the cart total calculations but if you also want to reflect on the cart markup you may want to hook into 'woocommerce_cart_product_subtotal'

add_filter( 'woocommerce_cart_product_subtotal', 'modify_cart_product_subtotal', 10, 4 ); function modify_cart_product_subtotal( $product_subtotal, $product, $quantity, $cart ) { // Add your logic here. // You can use the $cart instead of using the global $woocommerce variable. return $product_subtotal; }

For more detailed reference you can check the WC_Cart class documentation here: Woocommerce Cart

Upvotes: 3

Related Questions