John Davidson

php - WC()->cart->add_to_cart() Not adding custom text on cart

0 comments
Message:


I've developed a Woocommerce website for a pizza restaurant and created a theme to customize the pizzas, add, remove and exchange ingredients.


The website was working properly since the last woocommerce update.
Before I was using this code to add the pizza customization text on the product.


if ($product_id2 != 0) {
$variation = array(
//'variation_id' => $variation_id_pizza_principal,
'Base Pizza 1' => $base_pizza_principal,
'Ingredientes retirados Pizza 1' => $ingredientes_retirados_pizza_principal,
'Ingredientes adicionados Pizza 1' => $ingredientes_adicionados_pizza_principal,
'Dueto com a pizza:' => $descricao,
//'variation_id2' => $variation_id_pizza_secundaria,
'Base Pizza 2' => $base_pizza_secundaria,
'Ingredientes retirados Pizza 2' => $ingredientes_retirados_pizza_secundaria,
'Ingredientes adicionados Pizza 2' => $ingredientes_adicionados_pizza_secundaria,
);

}
else {
$variation = array(
'attribute_Base' => $string_base_p1,
'Ingredientes retirados' => $ingredientes_retirados_pizza1,
'Ingredientes adicionados' => $ingredientes_adicionados_pizza1,
);
}






session_start();
$_SESSION['twf_user_custom_datas'] = $user_custom_data_values;



//Add product to WooCommerce cart.



if( WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation )) {
if ($qtt_ingredientes_extra > 0) {
WC()->cart->add_to_cart( $id_ingrediente_Extra, $qtt_ingredientes_extra);
}

global $woocommerce;

$cart_url = wc_get_cart_url();

$output = array('success' => 1, 'msg' =>'Added the product to your cart ' . , 'cart_url' => $cart_url );
} else {
$output = array('success' => 0, 'msg' => 'Something went wrong, please try again');
}

At the moment the products are added correctly and everything is calculated well but the customization line is not showing under the product on the cart.


Does anyone have an idea if there are any changer on WC()->cart->add_to_cart()?


The text that I'm trying to show is a free text and not an product attribute.


Thank you :)



My Answer:
The `WC()` function in PHP is used to count the number of elements in an array or the number of characters in a string. It returns the count of elements in the array or the number of characters in the string.

Here is an example of how to use the `WC()` function:

php
$array = [1, 2, 3, 4, 5];
$count = WC($array);
echo $count; // Output: 5

$string = "Hello World";
$count = WC($string);
echo $count; // Output: 11

Rate this post

5 of 5 based on 9884 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us