Updating WooCommerce product price dynamically whilst adding custom information to order

I currently have a function that runs when a user selects an option on part of the site, this retrieves a price. I need to basically run a function that adds the specific product to the basket, alters its price to that price and also adds some custom meta thats also passed from the first function.

sendPrices(5000, 'Nice Title', 'Wednesday', 809);

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

function sendPrices($price, $orderTitle, $orderDispatch, $productID) {
   global $woocommerce; 
   $woocommerce->cart->add_to_cart($productID);
   add_custom_price($price, $orderTitle, $orderDispatch, $productID)
}

function add_custom_price( $cart_object, $price, $orderTitle, $orderDispatch ) {
    $custom_price = $price; // This will be your custome price  
    $target_product_id = $productID;
    foreach ( $cart_object->cart_contents as $value ) {
        if ( $value['product_id'] == $productID ) {
            $value['data']->price = $custom_price;
        }

    }
}

As you can see from the above code, Its repeating information and I’m struggling to get it working as imagined, I don’t know how to incorporate the meta information $orderTitle, and $orderDispatch. I need these to pull through to the checkout and complete order invoice.

Any help would be great!

Thanks

Read more here: Updating WooCommerce product price dynamically whilst adding custom information to order

Leave a Reply

Your email address will not be published. Required fields are marked *