Adjust price if addons are set

Hi i have woocommerce addons plugin in my wordpress shop, for users to choose a weight and calculate the price upon the weight,
i have this code that adds the main price to the price of KG,
For example the cake costs 100$ per kg

in Cart it shows 700$ for 6 KG’s. but it must show 600. So it adds the main price after the calculation.

Read More

here is the code

public function add_cart_item( $cart_item ) {
    // Adjust price if addons are set
    if ( ! empty( $cart_item['addons'] ) && apply_filters( 'woocommerce_product_addons_adjust_price', true, $cart_item ) ) {



        foreach ( $cart_item['addons'] as $addon ) {
            if ( $addon['price']  ) {

            }
        }

        $cart_item['data']->adjust_price($addon['price'] );
    }

    return $cart_item;
}

Related posts

Leave a Reply

1 comment

  1. $cart_item['data']->adjust_price($addon['price'] ); doesn’t do what you are expecting to have. In wooocmmerce/includes/abstructs/abstruct-wc-product.php on line 776 you will see that the price you passed is being added with the previous price. In this place is have to override the old price, not just adjusting. Or may be you can subtract the original price prior to the adjustment.

    Option 1: overriding the price

    $cart_item['data']->set_price($addon['price'] );
    

    Option 2: adjusting the price

    $price = (float) $cart_item['data']->get_price($addon['price'] );
    $adjustable_price = (float) $addon['price'] 
    $cart_item['data']->adjust_price( $adjustable_price );
    

    Hope this might help you.