Skip to content

Instantly share code, notes, and snippets.

@AshlinRejo
Created November 16, 2021 06:29
Show Gist options
  • Save AshlinRejo/6440158d3819752151c82ac9e17fdf60 to your computer and use it in GitHub Desktop.
Save AshlinRejo/6440158d3819752151c82ac9e17fdf60 to your computer and use it in GitHub Desktop.
Discount rules v2: Table customization all ranges in a row
<?php
/**
* Discount table
*
* This template can be overridden by copying it to yourtheme/advanced_woo_discount_rules/discount_table.php.
*
* HOWEVER, on occasion Discount rules will need to update template files and you
* (the theme developer) will need to copy the new files to your theme to
* maintain compatibility. We try to do this as little as possible, but it does
* happen. When this occurs the version of the template file will be bumped and
* the readme will list any important changes.
*/
if (!defined('ABSPATH')) exit; // Exit if accessed directly
global $product;
if (!empty($ranges) && !empty($woocommerce)) {
if ($ranges['layout']['type'] == 'advanced') {
$i = 0;
$existing_rule_id = 0;
$tag_opened = false;
/* This have been added from v2.3.9 */
if ( ! did_action( 'advanced_woo_discount_rules_before_display_discount_bar' ) ) {
do_action('advanced_woo_discount_rules_before_display_discount_bar');
?>
<style>
.awdr_discount_bar{
padding: 10px;
margin-bottom: 10px;
border-radius: 4px;
}
</style>
<?php
}
foreach ($ranges as $key => $badge_settings){
if($key !== 'layout'){
$current_rule_id = isset($badge_settings['rule_id'])? $badge_settings['rule_id'] : '';
$badge_bg_color = (!empty($badge_settings['badge_bg_color'])) ? $badge_settings['badge_bg_color'] : false;
$badge_text_color = (!empty($badge_settings['badge_text_color'])) ? $badge_settings['badge_text_color'] : false;
$badge_text = (!empty($badge_settings['badge_text'])) ? htmlspecialchars_decode($badge_settings['badge_text']) : false;
if($current_rule_id !== $existing_rule_id){
$tag_opened = true;
if($existing_rule_id !== 0){
?>
</div>
<?php
}
$existing_rule_id = $current_rule_id;
?>
<div class="awdr_discount_bar awdr_row_<?php echo $i;?>" style="<?php if($badge_bg_color){
echo "background-color:".$badge_bg_color.';';
}if($badge_text_color) {
echo "color:".$badge_text_color.';';
}?>">
<?php
}
?>
<div class="awdr_discount_bar_content">
<?php echo $badge_text; ?>
</div>
<?php
$i++;
}
}
if($tag_opened){
?>
</div>
<?php
}
} elseif ($ranges['layout']['type'] == 'default') {
if(isset($ranges['layout']['bulk_variant_table']) && $ranges['layout']['bulk_variant_table'] == "default_variant_empty"){?>
<div class="awdr-bulk-customizable-table"> </div><?php
}else{
$tbl_title = $base::$config->getConfig('customize_bulk_table_title', 0);
$tbl_discount = $base::$config->getConfig('customize_bulk_table_discount', 2);
$tbl_range = $base::$config->getConfig('customize_bulk_table_range', 1);
$tbl_title_text = $base::$config->getConfig('table_title_column_name', 'Title');
$tbl_discount_text = $base::$config->getConfig('table_discount_column_name', 'Discount');
$tbl_range_text = $base::$config->getConfig('table_range_column_name', 'Range');
$table_sort_by_columns = array(
'tbl_title' => $tbl_title,
'tbl_discount' => $tbl_discount,
'tbl_range' => $tbl_range,
);
asort($table_sort_by_columns); ?>
<div class="awdr-bulk-customizable-table">
<table id="sort_customizable_table" class="wdr_bulk_table_msg sar-table">
<tbody>
<tr class="wdr_bulk_table_tr bulk_table_row">
<th id="customize-bulk-table-title" class="wdr_bulk_table_td awdr-dragable"
style="<?php if(!$base::$config->getConfig('table_column_header', 0)){
echo 'display:none';
}else{
echo((!$base::$config->getConfig('table_title_column', 0)) ? 'display:none' : '');
} ?>"><span><?php _e("QTY", 'woo-discount-rules') ?></span>
</th>
<?php
foreach ($ranges as $range) :
$cart_discount_text = '';
$discount_type_value = isset($range['discount_value']) ? $range['discount_value'] : 0;
if (!isset($range['discount_value'])){
continue;
}
if (isset($range['discount_method']) && $range['discount_method'] != 'cart') {
$discounted_price_for_customizer = $woocommerce->formatPrice(isset($range['discounted_price']) ? $range['discounted_price'] : 0);
}else{
$discounted_price_for_customizer = $discount_value;
}
/**
* Discount Range
*/
if (isset($range['discount_method']) && $range['discount_method'] == 'set') {
$for_text = '';
} else {
$for_text = '+';
}
if (isset($range['from']) && !empty($range['from']) && isset($range['to']) && !empty($range['to'])) {
$discount_range = $range['from'] . '+';
} elseif (isset($range['from']) && !empty($range['from']) && isset($range['to']) && empty($range['to'])) {
$discount_range = $range['from']. $for_text;
} elseif (isset($range['from']) && empty($range['from']) && isset($range['to']) && !empty($range['to'])) {
$discount_range = '0+ ';
} elseif (isset($range['from']) && empty($range['from']) && isset($range['to']) && empty($range['to'])) {
$discount_range = '';
}?><?php
/**
* Table Data <td>'s
*/
$j=1;
foreach ($table_sort_by_columns as $column => $order) {
if ($column == "tbl_title") {
} elseif ($column == "tbl_discount") {
} else {?>
<td class="wdr_bulk_table_td wdr_bulk_range col_index_<?php echo $j;?>" data-colindex="<?php echo $j;?>"
style="<?php echo (!$base::$config->getConfig('table_range_column', 0) || isset($range['discount_method']) && in_array($range['discount_method'], array('product', 'cart'))) ? 'display:none':'';?>"><?php echo $discount_range ?></td><?php
}
$j++;
}
endforeach;
?>
</tr>
<tr class="wdr_bulk_table_tr bulk_table_row">
<th id="customize-bulk-table-title" class="wdr_bulk_table_td awdr-dragable"
style="<?php if(!$base::$config->getConfig('table_column_header', 0)){
echo 'display:none';
}else{
echo((!$base::$config->getConfig('table_title_column', 0)) ? 'display:none' : '');
} ?>"><span><?php _e("Per Item", 'woo-discount-rules') ?></span>
</th>
<?php
foreach ($ranges as $range) :
$cart_discount_text = '';
$discount_type_value = isset($range['discount_value']) ? $range['discount_value'] : 0;
if (!isset($range['discount_value'])){
continue;
}
/**
* Discount value
*/
if (isset($range['discount_method']) && $range['discount_method'] == 'cart') {
$cart_discount_text = __(' (in cart)', 'woo-discount-rules');
}
$discount_type = isset($range['discount_type']) ? $range['discount_type'] : 'flat';
if ($discount_type == "flat") {
$discount_value = $woocommerce->formatPrice($discount_type_value);
$discount_value .= __(' flat', 'woo-discount-rules');
$discount_value .= !empty($cart_discount_text) ? $cart_discount_text : '';
} elseif ($discount_type == "percentage") {
$discount_value = isset($range['discount_value']) ? $range['discount_value'] : 0;
$discount_value .= '%';
$discount_value .= !empty($cart_discount_text) ? $cart_discount_text : '';
} else {
$discount_value = $woocommerce->formatPrice($discount_type_value);
}
if (isset($range['discount_method']) && $range['discount_method'] != 'cart') {
$discounted_price_for_customizer = $woocommerce->formatPrice(isset($range['discounted_price']) ? $range['discounted_price'] : 0);
}else{
$discounted_price_for_customizer = $discount_value;
}
/**
* Table Data <td>'s
*/
$j=1;
foreach ($table_sort_by_columns as $column => $order) {
if ($column == "tbl_title") {} elseif ($column == "tbl_discount") {?>
<td class="wdr_bulk_table_td wdr_bulk_table_discount col_index_<?php echo $j;?>" data-colindex="<?php echo $j;?>"
style="<?php echo (!$base::$config->getConfig('table_discount_column', 0)) ? 'display:none' : '';?>">
<span class="wdr_table_discounted_value" style="<?php echo ( !$base::$config->getConfig('table_discount_column_value', 0)) ? 'display: none' : '';?>"><?php echo $discount_value; ?></span>
<span class="wdr_table_discounted_price" style="<?php echo ( $base::$config->getConfig('table_discount_column_value', 0)) ? 'display: none' : '';?>"><?php echo $discounted_price_for_customizer; ?></span>
</td><?php
}
$j++;
}
endforeach;
?>
</tr>
<tr class="wdr_bulk_table_tr bulk_table_row">
<th id="customize-bulk-table-title" class="wdr_bulk_table_td awdr-dragable"
style="<?php if(!$base::$config->getConfig('table_column_header', 0)){
echo 'display:none';
}else{
echo((!$base::$config->getConfig('table_title_column', 0)) ? 'display:none' : '');
} ?>"><span><?php _e("Savings", 'woo-discount-rules') ?></span>
</th>
<?php
foreach ($ranges as $range) :
$cart_discount_text = '';
$discount_type_value = isset($range['discount_value']) ? $range['discount_value'] : 0;
if (!isset($range['discount_value'])){
continue;
}
$percentage_value = 0;
if(method_exists($product, 'get_price')){
$product_price = $product->get_price();
$discounted_price = isset($range['discounted_price'])? $range['discounted_price']: 0;
if($product_price > 0 && $discounted_price > 0){
if(( $product_price - $discounted_price ) > 0){
$percentage_value = (( $product_price - $discounted_price ) / $product_price) * 100;
}
}
}
/**
* Table Data <td>'s
*/
$j=1;
foreach ($table_sort_by_columns as $column => $order) {
if ($column == "tbl_title") {} elseif ($column == "tbl_discount") {?>
<td class="wdr_bulk_table_td wdr_bulk_table_discount col_index_<?php echo $j;?>" data-colindex="<?php echo $j;?>"
>
<span class="wdr_table_discounted_value" ><?php echo round($percentage_value, 2)."%"; ?></span>
</td><?php
}
$j++;
}
endforeach;
?>
</tr>
</tbody>
</table>
</div><?php
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment