I am trying to add products to my cart using this function:
WC()->cart->add_to_cart( $id, $quantity=1,$variation_id );
However, it only works when the cart is empty. If there is already a product in the cart, then a new product does not get added.
For testing purposes, I am using a form button like this:
<form name="addpro" method="post" action="">
<input type="submit" name="addcustomcarts" value="ADD TOO CART">
</form>
Here is my code to add the product to the cart:
add_action('init', 'customcart');
function customcart() {
if (isset($_POST["addcustomcarts"])) {
// global $woocommerce;
//Create main product
$product = new WC_Product_Variable();
$product->set_name("Tdwo");
//Create the attribute object
$attribute = new WC_Product_Attribute();
//pa_size tax id
$attribute->set_id( 0 ); // -> SET to 0
//pa_size slug
$attribute->set_name( 'size' ); // -> removed 'pa_' prefix
//Set terms slugs
$attribute->set_options( array(
'blue',
'grey'
) );
$attribute->set_position( 0 );
//If enabled
$attribute->set_visible( 1 );
//If we are going to use attribute in order to generate variations
$attribute->set_variation( 1 );
$product->set_attributes(array($attribute));
//Save main product to get its id
$id = $product->save();
$variation = new WC_Product_Variation();
$variation->set_regular_price(10);
$variation->set_parent_id($id);
//Set attributes requires a key/value containing
// tax and term slug
$variation->set_attributes(array(
'size' => 'blue' // -> removed 'pa_' prefix
));
//Save variation, returns variation id
$variation_id = $variation->save() ;
echo $variation_id;
// echo get_permalink( $id ); // -> returns a link to check the newly created product
WC()->cart->add_to_cart( $id, $quantity=1,$variation_id );
exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
}
}
Try the following (cleaned a bit your code and added attributes array to add_to_cart()
method):
add_action('init', 'customcart');
function customcart() {
if (isset($_POST["addcustomcarts"])) {
## -- Settings -- ##
$variable_product_name = "Tdwo"; // Main variable product name
$attribute_name_slug = 'size'; // Define attriburte name (slug) to be used for variations
$attribute_terms_slugs = array( 'blue', 'grey'); // Define attriburte term slugs to be used with variations
// Get an empty instance of the WC_Product_Variable Object
$product = new WC_Product_Variable();
$product->set_name($variable_product_name);
// Get an empty instance of the WC_Product_Attribute Object
$attribute = new WC_Product_Attribute();
$attribute->set_id( 0 ); // Set attribute Id (First, set to 0)
$attribute->set_name( $attribute_name_slug ); // Set attribute name
$attribute->set_options( $attribute_terms_slugs ); // Set attribute term slugs to be used with variations
$attribute->set_position( 0 ); // Position: First one, set to 0
$attribute->set_visible( 1 ); // Visible on product page
$attribute->set_variation( 1 ); // Used for variations
$product->set_attributes( array($attribute) );
// Save main product to get its id
$product_id = $product->save();
// --------------------------------
## -- Settings -- ##
$chosen_term_slug = 'blue'; // Set chosen attribute term slug for the current variation
$variation_price = 10; // The variation regular price
$variation = new WC_Product_Variation();
$variation->set_parent_id( $product_id );
$variation->set_regular_price( $variation_price );
$variation->set_price( $variation_price );
// Set attributes requires a key/value containing
$variation->set_attributes( array( $attribute_name_slug => $attribute_term_slug ) );
//Save variation, returns variation id
$variation_id = $variation->save();
// Attribute formatted name slug and term slug for add to cart
$attributes = array( 'attribute_pa_' . $attribute_name => $attribute_term_slug );
WC()->cart->add_to_cart( $product_id, 1, $variation_id, $attributes, array() );
exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );
}
}
Code goes in functions.php file of the active child theme (or active theme). It could works.