sacar saber restar porcentaje hacer descuento decimales como calculo calcular aplicar php wordpress woocommerce variations

php - saber - Agregar el porcentaje de descuento a productos variables a la venta



restar porcentaje php (1)

Actualización: para productos variables es más complicado ya que tiene 2 ubicaciones diferentes con precios, la primera muestra el precio mínimo y máximo (cuando tiene múltiples variaciones) y la segunda muestra el precio de las variaciones seleccionadas. He cambiado mucho tu código original.

Aquí está el código correcto para mostrar las etiquetas dinámicas personalizadas alrededor de los porcentajes de descuento:

add_filter(''woocommerce_variation_sale_price_html'',''adventure_tours_sales_price'', 10, 2 ); add_filter(''woocommerce_sale_price_html'',''adventure_tours_sales_price'', 10, 2 ); function adventure_tours_sales_price( $price, $product ){ // Variables initialisation $regular_price = $product->regular_price; $sale_price = $product->sale_price; $save_text = __(''Save'', ''woocommerce'') . '' ''; if(!empty($sale_price)) { // Percentage calculation $percentage = ''<span class="save-percent"> '' .$save_text . round( ( ( $regular_price - $sale_price ) / $regular_price ) * 100 ) . ''%</span>''; $price = ''<del class="strike">'' . woocommerce_price( $regular_price ) . ''</del> <ins class="highlight">'' . woocommerce_price( $sale_price ) . $percentage . ''</ins>''; } else { $price = ''<ins class="highlight">''.woocommerce_price( $regular_price ).''</ins>''; } return $price; } add_filter(''woocommerce_variable_sale_price_html'', ''adventure_tours_sales_min_max_prices'', 20, 2); function adventure_tours_sales_min_max_prices( $price, $product) { // Variables initialisation $variation_min_reg_price = $product->get_variation_regular_price(''min'', true); $variation_max_reg_price = $product->get_variation_regular_price(''max'', true); $variation_min_sale_price = $product->get_variation_sale_price(''min'', true); $variation_max_sale_price = $product->get_variation_sale_price(''max'', true); $percentage_min = ''''; $percentage_max = ''''; $save_text = __(''Save'', ''woocommerce'') . '' ''; // Percentage calculations if($variation_min_reg_price != $variation_min_sale_price) $percentage_min = ''<span class="save-percent-min"> ('' .$save_text . round( ( ( $variation_min_reg_price - $variation_min_sale_price ) / $variation_min_reg_price ) * 100 ) . ''%)</span>''; if($variation_max_reg_price != $variation_max_sale_price) $percentage_max = ''<span class="save-percent-max"> ('' .$save_text . round( ( ( $variation_max_reg_price - $variation_max_sale_price ) / $variation_max_reg_price ) * 100 ) . ''%)</span>''; if (($variation_min_reg_price != $variation_min_sale_price) || ($variation_max_reg_price != $variation_max_sale_price)) { $price = ''<del class="strike">'' . woocommerce_price($variation_min_reg_price) . ''-'' . woocommerce_price($variation_max_reg_price) . ''</del> <ins class="highlight">'' . woocommerce_price($variation_min_sale_price) . $percentage_min . '' - '' . woocommerce_price($variation_max_sale_price) . $percentage_max . ''</ins>''; } return $price; }

El código va en el archivo function.php de su tema hijo activo (o tema) o también en cualquier archivo de complemento.

Este código está probado y funciona.

Aquí está lo que obtendrá (Capturas de pantalla de mi servidor de prueba sin procesar):

Respuestas relacionadas:

Intento agregar un porcentaje de descuento aparte de precio en un sitio que usa WooCommerce.

He aplicado este script para el precio estándar y el precio de venta:

// Add save percentage next to sale item prices. add_filter( ''woocommerce_get_price_html'', ''adventure_tours_sales_price'', 10, 2 ); function adventure_tours_sales_price( $price, $product ){ $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); return $price . sprintf( __('' Save %s'', ''woocommerce'' ), $percentage . ''%'' ); }

El script anterior funciona.

En el front-end, tengo el porcentaje de precio.

Ahora quiero aplicar la misma secuencia de comandos al precio de variación del producto.

Revisé la opción de variación del producto e intenté algo como esto:

// Add save percentage next to sale item prices. add_filter( ''woocommerce_get_price_html'', ''adventure_tours_sales_price'', 10, 2 ); function adventure_tours_sales_price( $price, $product ){ if( $product->is_type( ''variable'' ) ) { $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); return $price . sprintf( __('' Save %s'', ''woocommerce'' ), $percentage . ''%'' ); }else{ $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); return $price . sprintf( __('' Save %s'', ''woocommerce'' ), $percentage . ''%'' ); } }

Pero no funciona, el porcentaje no se aplica al precio.

Ni en el front-end.