Hello, i have a function to get custom price from a .js calculation and put it in a specific product.
Now i need to create a product every time I run this function and set the custom price in that new product.

My first function is :

 add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
 function add_custom_price( $cart_object ) {
 global $woocommerce;

    $cart_item_meta['estimated_fare'] = WC()->session->get( 'estimated_fare' );
    $custom_price = $cart_item_meta['estimated_fare'] ; // This will be your custome price  
    $target_product_id = get_option('product_id_wc'); // product_id_wc = my pre-defined product
    foreach ( $cart_object->cart_contents as $key => $value ) {
        if ( $value['product_id'] == $target_product_id ) {
            $value['data']->set_price( $custom_price );
        }
    }
}

any ideas how?

Thank you in advance

Read more here: Woocommerce create product and custom price in the same function


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: