programmatically add values to custom date fields
-
Hello,
I am adding a product to the cart programmatically using the following:WC()->cart->add_to_cart( $product_id, $quantity, 0, array(), $cart_item_data );
I would like to add values to a custom field I have set on all products. It is a DATE field with label “Date of Birth”. I can’t seem to get it to work. The product adds to cart successfully, but when viewing the cart the date field displayed under it is empty. So the value is not actually being set in the custom field.
I’m not sure if I should be using the field’s label or ID, but neither of those are working.
Here is an example of my code.
function add_custom_product_to_cart( $product_id, $quantity = 1, $custom_fields = array() ) {
// Prepare cart item data
$cart_item_data = array();
// Add custom fields to cart item data
foreach ( $custom_fields as $key => $value ) {
$cart_item_data[ $key ] = $value;
}
// Add the product to the cart with custom fields
$result = WC()->cart->add_to_cart( $product_id, $quantity, 0, array(), $cart_item_data );
}
// Example usage
add_custom_product_to_cart(
1234, // Replace 123 with your product ID
1, // Quantity
$custom_fields = array(
'Date of Birth' => '2001-02-12',
)
);Any help would be greatly appreciated. Thank you.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- You must be logged in to reply to this topic.