I want a checkbox in some product pages of my woocommerce website that add an extra cost when the customer select that checkbox before to add to cart the product. We want to display the option to verify the design they have to upload to our web.
This snippet works well for one product unit (Qty x1: 100e + Verification 3,99e = Total 103,99e) but in the case the customer needs more than one unit of the same product the extracost is getting multiplied by the quantity (Qty x2: 200e + Verification 3,99e = Total 207,98e).
It have no sense because the design they upload it will be the same for one or more units of that unique product. I have used the iteration becasue the customer maybe wants to select the checkbox in differents products.
// Agregar la checkbox en la página del producto
add_action('woocommerce_before_add_to_cart_button', 'agregar_checkbox_verificacion_diseno');
function agregar_checkbox_verificacion_diseno() {
global $product;
// Verificar si el producto pertenece a las categorías excluidas
$excluded_categories = array('mastiles', 'fly-banners', 'banderas-institucionales');
$product_categories = wp_get_post_terms($product->get_id(), 'product_cat', array('fields' => 'slugs'));
if (!empty(array_intersect($excluded_categories, $product_categories))) {
return; // No mostrar la casilla de verificación para estos productos
}
// Mostrar la casilla de verificación para otros productos
echo '<p class="form-row verificacion-diseno">
<label><input type="checkbox" name="verificacion_diseno" id="verificacion_diseno"> <span class="verificacion-diseno-title">Verificación del diseño (+3,99€)</span><br><span class="verificacion-diseno-desc">Nuestro equipo de impresión revisará y verificará tu diseño, y te enviará un archivo a modo de boceto del producto final</span></label>
</p>';
}
// Procesar la selección de la checkbox
add_filter('woocommerce_add_cart_item_data', 'procesar_checkbox_verificacion_diseno', 10, 2);
function procesar_checkbox_verificacion_diseno($cart_item_data, $product_id) {
global $product;
// Verificar si el producto pertenece a las categorías excluidas
$excluded_categories = array('mastiles', 'fly-banners', 'banderas-institucionales');
$product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
if (!empty(array_intersect($excluded_categories, $product_categories))) {
return $cart_item_data; // No aplicar la funcionalidad para estos productos
}
// Aplicar la funcionalidad para otros productos
if (isset($_POST['verificacion_diseno'])) {
$cart_item_data['verificacion_diseno'] = true;
}
return $cart_item_data;
}
// Agregar el costo adicional al precio del producto en el carrito
add_action('woocommerce_before_calculate_totals', 'agregar_costo_checkbox_verificacion_diseno');
function agregar_costo_checkbox_verificacion_diseno($cart) {
if (is_admin() && !defined('DOING_AJAX'))
return;
// Iterar a través de los elementos del carrito
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
if (isset($cart_item['verificacion_diseno'])) {
// Obtener el precio del producto
$product_price = $cart_item['data']->get_price();
// Agregar el costo adicional
$extra_cost = 3.99;
$cart_item['data']->set_price($product_price + $extra_cost);
}
}
}
// Mostrar la anotación en el carrito y el checkout
add_filter('woocommerce_get_item_data', 'mostrar_anotacion_checkbox_verificacion_diseno', 10, 2);
function mostrar_anotacion_checkbox_verificacion_diseno($item_data, $cart_item) {
if (isset($cart_item['verificacion_diseno'])) {
$item_data[] = array(
'key' => 'Verificación del diseño',
'value' => '+3,99€',
);
}
return $item_data;
}
// Agregar estilos CSS
add_action('wp_head', 'verificacion_diseno_styles');
function verificacion_diseno_styles() {
echo '<style>
.verificacion-diseno-title { font-size: 16px; }
.verificacion-diseno-desc { font-size: 14px; font-weight: normal; color: #636363; }
.verificacion-diseno { margin-bottom: 20px; }
</style>';
}
Can someone help me to fix that? I have tried to limit the extracost to 3,99e per product but then the snippet didnt worked at all.
Carlos is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.