The WooCommerce Cart add_to_cart function takes a parameter for cart_item_data. To make this work, you simply need to pass the addon data to add_to_cart. Something like this:
$addons = array(
array(
"name" => "Size",
"value" => "2.5x7",
"price" => 0,
"field_name" => "5186-0",
"field_type" => "multiple_choice",
"id" => "1683555538",
"price_type" => "flat_fee"
),
array(
"name" => "Medium",
"value" => "Print",
"price" => 0,
"field_name" => "5186-1", // {product_id}-{addon_index}
"field_type" => "multiple_choice",
"id" => "1683555539",
"price_type" => "flat_fee"
)
);
$product_id = 11;
$quantity = 1;
$variation_id = null;
$variation = null;
$cart_item_data = array('addons' => $addons);
WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation, $cart_item_data);
Related Posts:
- How to check if woocommerce is activated in theme
- Woocommerce get selected shipping zone id for the current user
- Query WooCommerce orders grouped by several order statuses
- How to change “Shipping Calculator “field label on Cart Page – woocommerce? [closed]
- Cleanest/Fastest way to avoid calling and retrieving data from the database multiple times?
- Automatically check the option “Enable stock management at product level” on product creation
- How to show order data by multiple ID?
- Problem with Woocommerce REST API Authentication
- Remove General Tab and Plugin Tab in Settings in Woocommerce
- WooCommerce – Display variation custom field value [closed]
- Woocommerce HTML email option unavailable
- How to add SVG icon above product tab title
- How does WooCommerce ensure that a product doesn’t run out of stock during payment? [closed]
- How to change / delete product short description in Woocommerce
- How to trash multiple post/product programmatically
- OrderItem get_total() returns lower value than $order_item->get_product()->get_price() * quantity
- Change the URL of an image from wp_get_attachment_image_src except in the frontpage
- Setting up 2 SMTP accounts: 1 for wordpress and 1 for woocommerce
- WooCommerce – Reset quantity input field when variation changes
- Custom Text in WooCommerce Order Processing Email Based on Products
- woocommerce subscriptions – get expiration date [closed]
- Automatically remove a canceled order in Woocommerce
- Woocommerce hook run after an Order been created through REST API
- How can I apply_filters from inside a function?
- Pass Category Name, Description and Photo into variables to pass to jQuery
- Woocommerce api: create product with images – bad request
- Different Minimum Order Amounts based on Shipping Zone [closed]
- Woocommerce: hook action/filter I could use to add variation id and price with each attribute opt on WooCommerce Rest api
- Displaying Woocommerce Product Category in WordPress
- Displaying custom taxonomy on WooCommerce product page
- Change “add to cart” button color based on Woocommerce product category [closed]
- Add to cart function not working for first product when cart is empty [closed]
- Woocommerce : Can’t get product info in loop
- WooCommerce custom product dimensions
- WordPress Woocommerce – Product Type [closed]
- Pagination for custom shop loop woocommerce
- Is there a way to hook or call a custom woocomerce template that is not part of the default templates of woocommerce?
- get taxonomy thumbnail and use it as a variable in code
- Single Quotes in .php file doesn’t load website
- get_total () returns 0 – woocommerce [closed]
- How to upload WP user avatar on woocommerce account page [closed]
- email alert for product availability
- How to relocate paypal button gateway on checkout page in woocommerce
- How to rename woocommerce-billing-fields
- Only show size attributes in product box woocommerce when available
- How do I run code every 24 hours?
- Woocommerce Product_cat for current category page
- Create a package of products as one Item in woocommerce?
- After disabling WPML multi currency, the currency is still handled by the WPML
- How do i wrap woocomerce attribute in list?
- Removing files programmatically
- WooCommerce “Checkout Now” button [closed]
- How can i programmatically change the SKU of a product based on product variation?
- Execute PHP code only with specific user role
- Add a custom WooCommerce settings tab with sections
- Woocommerce. Get a list of products in the current category on the archive page and product page
- How can I enter on the Thank you page in woocommerce the discount code I assigned to the product category?
- WordPress Fresh Install Errors (Woocommerce specifically now)
- Woocommerce append shipping method form
- Woocommerce – Exclude variation attribute on specific variation
- PHP function for horizontal Woocommerce thumbnails and badges
- How do I check if a product is in two categories?
- php code for outputting a custom page in my account
- Hide specific shipping methode depending on day and time of day
- Adding custom field to product category and show it to shop/category shop page
- How to hide a div section when user is logged in, in single product woocommerce
- WooCommerce display price before add to cart [closed]
- Get the same SKU from woocommerce itself for simple and variable products
- Remove span tags from WooCommerce Downloads page
- Woocommerce checkout page – custom field checkbox value into email
- Global variable doesnt’t pass to another function
- Exclude category from woocommerce product category list
- Edit woocommerce_product_archive_description using hook
- WooCommerce – template_redirect if is_checkout AND order has been paid?
- How to empty price of simple and variable product when product changed into out off stock
- Make WooCommerce product categories only show images on homepage
- How to create loop inside wordpress function?
- How to auto-generate random numbers in username?
- Remove generated category and tag class names from woocommerce product & blog listings markup
- WooCommerce Quantity in Dropdown menu instead of clasic [closed]
- WP_Query To Display Product Of Brand On Taxonomy Page
- Hot to edit WooCOmmerce dashboard?
- How to echo a PHP code into WooCommerce variation product?
- WooCommerce subcategories help
- Get Billing Email from WooCommece Checkout and Pass to Fullstory JS
- Woocommerce custom attributes list help
- WooCommerce multiple custom fields code
- How to have more than one ID included in the exclude command?
- wp_query beginner
- Global filter not working
- WooCommerce remove_action and add_action [closed]
- WordPress using get_term to retreive slug not working as expected
- Can’t make product images clickable
- WooCommerce – Print Processing orders [closed]
- Hide cart when empty [closed]
- How to add margin underneath woocommerce total price amount -> productpages
- Order woo products, but first products must be with acf fields
- How to find the value of the user password in wordpress and send it to another server via rest api
- Change Checkout “Place Order” text if cart has a specific product
- Warning: Attempt to read property “term_id” on int – Woocommerce