En mi sitio web de WooCommerce tengo algunos productos con el mismo precio de 80$ .
Quiero agregar un descuento por la cantidad de productos.
La lógica es así:
if (Products Quantity is 2){ // the original product price change from 80$ to 75$ each. } if(Products Quantity is 3 or more){ //the original product price change from 80$ to 70$ each. }por ejemplo,
si un cliente elige 2 productos, el precio original será
(80$ x 2)=>160$.
Pero después del descuento, será:(75$ x 2)=>150$.
Y…
si el visitante elige 3 productos, el precio original será
(80$ x 3)=>240$.
Pero después de la tarifa, será:(70$ x 3)=>210$.
¿Alguna ayuda, por favor?
Gracias
Esta función enganchada personalizada debería hacer lo que espera. Puede establecer en él su límite de descuento progresivo basado en la cantidad de artículos individuales.
Aquí está el código
## Tested and works on WooCommerce 2.6.x and 3.0+ add_action( 'woocommerce_cart_calculate_fees', 'progressive_discount_by_item_quantity', 10, 1 ); function progressive_discount_by_item_quantity( $cart ) { if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return; # Progressive quantity until quantity 3 is reached (here) # After this quantity limit, the discount by item is fixed # No discount is applied when item quantity is equal to 1 // Set HERE the progressive limit quantity discount $progressive_limit_qty = 3; // <== <== <== <== <== <== <== <== <== <== <== $discount = 0; foreach( $cart->get_cart() as $cart_item_key => $cart_item ){ $qty = $cart_item['quantity']; if( $qty <= $progressive_limit_qty ) $param = $qty; // Progressive else $param = $progressive_limit_qty; // Fixed ## Calculation ## $discount -= 5 * $qty * ($param - 1); } if( $discount < 0 ) $cart->add_fee( __( 'Quantity discount' ), $discount); // Discount }El código va en el archivo function.php de su tema secundario activo (o tema) o también en cualquier archivo de complemento.
Probado y funciona en WooCommerce 2.6.x y 3.0+