I am trying to add product variations using the add_product() function from woocommerce.
But everytime I add the variation product. It just adds the parent product and not the variation.
$product_id = 40;
$variation_id = 42;
$quantity = 1;
$variation_name = color;
$variation_option = red;
$args = array(
'attribute_'.$variation_name => $variation_option,
);
$order = wc_create_order();
$order->add_product( get_product( $product_id ), $quantity, $args );
I also have tried this which also doesn’t work
$order->add_product( get_product( $variation_id ), $quantity, $args );
I have doubled checked the variation id, attribute, name and option.
I think something wrong pass args parameter try this
1 – The
get_product()
function is deprecated and thewc_get_product()
function should be used.2 – To calculate the final price,
calculate_totals()
function should be used to set the paid price.UPDATE CODE :
Create a manual WooCommerce order:
and set a desired product to order.
And we use the function
wc_downloadable_product_permissions()
to set the access of product download files.