0

I'm trying to add products to the cart programmatically but with addons from WooCommerce Product Addons. I'm not finding anything in their documentation and am struggling to find anything in their code that seems to do it.

1 Answer 1

0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.