Adding a product to cart with custom info and price
When building a custom shop page in WordPress, you often encounter a scenario where the base product price is static in the database (often set to 0.00), but the actual cost depends entirely on user-selected variables like dimensions or materials. If you simply call $woocommerce->cart->add_to_cart($_POST['custom_product_id']), WooCommerce treats this as a standard item. It pulls the default price from the product object and ignores any custom logic you might have calculated in JavaScript. To inject a dynamic price and attach specific metadata without creating a duplicate product entry, you need to manipulate the cart contents directly after adding the item.
The most robust approach involves retrieving the added variation (or product) from the cart object and updating its properties via the WC_Cart_Fees_Totals or, more accurately in this context, the internal data arrays that drive the line items. While you cannot directly set the price on a standard simple product without using a variable structure, you can simulate a variable product experience by adding a custom meta key to the cart item and then hooking into the rendering logic.
However, there is a critical distinction to make regarding how WooCommerce handles prices. The add_to_cart method expects either a valid product ID or a variation ID. If your database stores unique SKUs for every configuration but marks them as simple products with a zero price, you must ensure that the cart item object reflects the calculated cost before it is committed to the session.
To achieve this, you should intercept the addition process and manually construct the line item data. One effective pattern involves using the woocommerce_cart_add_to_cart action hook immediately after adding the product. Within that hook, you can access the $cart_item_data array, which stores custom information associated with each cart entry.
Here is how you might structure the logic to pass your custom description and build data:
add_action( 'woocommerce_cart_add_to_cart', 'custom_cart_product_data', 10, 2 );
function custom_cart_product_data( $cart_item_key, $product_id ) {
// Retrieve calculated price from session or POST data
$custom_price = isset( $_POST['calculated_price'] ) ? floatval( $_POST['calculated_price'] ) : 0;
// Prepare custom image URL and description
$custom_image = isset( $_POST['product_image'] ) ? $_POST['product_image'] : get_the_post_thumbnail_url( $product_id );
$custom_desc = isset( $_POST['product_dimensions'] ) ? $_POST['product_dimensions'] : '';
// Store build data for admin only (e.g., internal notes)
$build_data = isset( $_POST['internal_build_notes'] ) ? sanitize_textarea_field( $_POST['internal_build_notes'] ) : '';
// Set the cart item data.
// Note: Direct price manipulation here is often overwritten by the product definition.
// The most reliable way to force a custom price on a simple product is via a filter.
$cart_item_data = wc_get_cart_item_data( $product_id );
if ( ! empty( $cart_item_data ) ) {
$cart_item_data['custom_description'] = $custom_desc;
$cart_item_data['build_notes'] = $build_data; // Hidden meta
$cart_item_data['image_url'] = $custom_image;
// Set custom price via filter hook to override the zero base price
add_filter( 'woocommerce_product_get_price', function() use ( $product_id, $custom_price ) {
global $product;
if ( $product->get_id() == $product_id && $custom_price > 0 ) {
return $custom_price;
}
}, 10, 1 );
// Store the calculated price in cart item data for reference
$cart_item_data['_custom_price'] = $custom_price;
}
return $cart_item_data;
}
The code snippet above demonstrates a common pattern: storing metadata in the $cart_item_data array and using the woocommerce_product_get_price filter to override the displayed cost. The build_notes field can be stored as a custom meta key on the cart item itself, accessible via WC_Cart_Fees_Totals::get_cart_item_meta() or by iterating through WC_Cart::get_cart().
A significant trade-off here is performance and complexity. Modifying prices via filters for every page load can impact site speed if not scoped correctly. Additionally, standard checkout forms do not display custom images or descriptions unless the product type supports them natively (like Variable Products). To ensure the custom image and description appear on the checkout page, you may need to hook into woocommerce_cart_item_data or use a specific cart item data filter that WooCommerce recognizes for rendering front-end details.
Another consideration is the visibility of "Build Data." Since this information needs to remain hidden during checkout but visible in the WordPress admin, store it as a custom meta key with a prefix (e.g., _custom_build_info) to prevent accidental display on public pages. You can retrieve these values in the admin using WC_Cart::get_cart_item_meta( $cart_item_key, 'build_notes', false ).
This approach ensures that your dynamic pricing logic remains decoupled from the core product definitions while preserving the integrity of WooCommerceâs cart session. It allows you to leverage standard e-commerce flows without needing to refactor your entire product architecture into variable products for every unique configuration.