скидка - число в %
Thu Aug 04 2022 12:49:14 GMT+0000 (Coordinated Universal Time)
Saved by @mastaklance
/** * Show sale price in percentage */ function my_product_block( $html, $data, $product ) { $html = '<li class="wc-block-grid__product"> <div class="image-wrap"> <a href="' . $data->permalink . '" class="wc-block-grid__product-link">' . $data->image . '</a> </div> <h3><a href="' . $data->permalink . '">' . $data->title . '</a></h3> ' . $data->badge . ' ' . $data->price . ' ' . $data->rating . ' <a href="' . $data->permalink . '" class="button addtocartbutton">View Product</a> </li>'; return $html; } add_filter( 'woocommerce_blocks_product_grid_item_html', 'my_product_block', 10, 3); // Display the Woocommerce Discount Percentage on the Sale Badge for variable products and single products add_filter( 'woocommerce_sale_flash', 'display_percentage_on_sale_badge', 20, 3 ); function display_percentage_on_sale_badge( $html, $post, $product ) { if( $product->is_type('variable')){ $percentages = array(); // This will get all the variation prices and loop throughout them $prices = $product->get_variation_prices(); foreach( $prices['price'] as $key => $price ){ // Only on sale variations if( $prices['regular_price'][$key] !== $price ){ // Calculate and set in the array the percentage for each variation on sale $percentages[] = round( 100 - ( floatval($prices['sale_price'][$key]) / floatval($prices['regular_price'][$key]) * 100 ) ); } } // Displays maximum discount value $percentage = max($percentages) . '%'; } elseif( $product->is_type('grouped') ){ $percentages = array(); // This will get all the variation prices and loop throughout them $children_ids = $product->get_children(); foreach( $children_ids as $child_id ){ $child_product = wc_get_product($child_id); $regular_price = (float) $child_product->get_regular_price(); $sale_price = (float) $child_product->get_sale_price(); if ( $sale_price != 0 || ! empty($sale_price) ) { // Calculate and set in the array the percentage for each child on sale $percentages[] = round(100 - ($sale_price / $regular_price * 100)); } } // Displays maximum discount value $percentage = max($percentages) . '%'; } else { $regular_price = (float) $product->get_regular_price(); $sale_price = (float) $product->get_sale_price(); if ( $sale_price != 0 || ! empty($sale_price) ) { $percentage = round(100 - ($sale_price / $regular_price * 100)) . '%'; } else { return $html; } } return '<span class="onsale">' . esc_html__( 'up to -', 'woocommerce' ) . ' '. $percentage . '</span>'; // If needed then change or remove "up to -" text } // v2 - new option add_filter( 'woocommerce_sale_flash', 'ds_replace_sale_text' ); function ds_replace_sale_text($text) { global $product; // Get product stock status and type $stock = $product->get_stock_status(); $product_type = $product->get_type(); // Initialize sale price and regular price $sale_price = 0; $regular_price = 0; // Variable products if ($product_type == 'variable') { $product_variations = $product->get_available_variations(); foreach ($product_variations as $variation) { if ($variation['display_price'] < $variation['display_regular_price']) { $sale_price = $variation['display_price']; $regular_price = $variation['display_regular_price']; break; // Stop loop after finding the first sale price } } // Calculate discount percentage if ($regular_price > $sale_price && $stock != 'outofstock') { $product_sale = intval(((intval($regular_price) - floatval($sale_price)) / floatval($regular_price)) * 100); if ($product_sale > 5) { return '<span class="onsale"> <span class="sale-icon" aria-hidden="true" data-icon=""></span> Supersale ' . esc_html($product_sale) . '% OFF</span>'; } else { return '<span class="onsale"> <span class="sale-icon" aria-hidden="true" data-icon=""></span>Sale!</span>'; } } else { return ''; } } // Simple products else { $regular_price = get_post_meta(get_the_ID(), '_regular_price', true); $sale_price = get_post_meta(get_the_ID(), '_sale_price', true); // Calculate discount percentage if ($regular_price > 5) { $product_sale = intval(((floatval($regular_price) - floatval($sale_price)) / floatval($regular_price)) * 100); return '<span class="onsale"> <span class="sale-icon" aria-hidden="true" data-icon=""></span> Supersale ' . esc_html($product_sale) . '% OFF</span>'; } elseif ($regular_price >= 0 && $regular_price <= 5) { $product_sale = intval(((floatval($regular_price) - floatval($sale_price)) / floatval($regular_price)) * 100); return '<span class="onsale"> <span class="sale-icon" aria-hidden="true" data-icon=""></span>Sale!</span>'; } else { return ''; } } }
2ой блок для вариативного товара
https://wpsimplehacks.com/how-to-display-the-discount-percentage-on-the-sale-badge/
Comments