Hi there, I was wondering how to come up with a code snippet that forces the client to select a minimum amount of a listintg article and following your docs I managed to write this, however is not working.
Is there any mistake in the syntax or functions calling?
Is it possible to have a optional custom field similar to the one existing but establishing a minimum instead of a maximum?
Code:
<?php
// Agregar campo de cantidad mínima a productos de Hivepress
function hivepress_agregar_campo_minimo() {Preformatted text
hivepress()->product->add_field(
array(
'id' => '_minimo',
'label' => __('Cantidad Mínima', 'hivepress'),
'type' => 'number',
'step' => 1,
'min' => 1
)
);
}
add_action('hivepress_product_price', 'hivepress_agregar_campo_minimo');
// Validar cantidad mínima antes de agregar producto al carrito
function hivepress_validar_cantidad_minima($passed, $product_id, $quantity) {
$minimo = get_post_meta($product_id, '_minimo', true);
if ($quantity < $minimo) {
wc_add_notice(__('La cantidad mínima para este producto es de '.$minimo.' unidades.', 'woocommerce'), 'error');
$passed = false;
}
return $passed;
}
add_filter('woocommerce_add_to_cart_validation', 'hivepress_validar_cantidad_minima', 10, 3);
?>
Thanks in advance,
Eduardo