【问题标题】:Updating values in summary tables after script loads all products脚本加载所有产品后更新汇总表中的值
【发布时间】:2019-12-26 10:01:17
【问题描述】:

我正在构建一个汇总产品信息的脚本,我想将汇总表放在页面顶部。

问题在于,在处理产品信息时,每个产品的值会被累加。因此,总计表显示所有内容为 0,因为在处理产品信息之前正在加载总计表。

如果我将汇总表放在代码的底部,情况会发生变化 - 然后它可以正常工作。

有什么方法可以使汇总表在顶部进行汇总(汇总表 ID = itemtabtotals)?

我正在谈论的代码中的值如下: $NUM_products、$SUM_sold_QTY、$SUM_prices、$AVG_price、$SUM_bena_prices、$AVG_bena_price、$SUM_costs、$AVG_costs、$SUM_profits、$AVG_profit、$soldQuantity

                if ($wp_query->have_posts()) {
                        $SUM_sold_QTY = 0;
                        $NUM_products = 0;
                        $AVG_price = 0;
                        $AVG_bena_price = 0;
                        $AVG_costs = 0;
                        $AVG_profit = 0;

                        ?>
                        <table style="border: 1px solid black;" class="widefat" id="itemtabtotals">
                            <thead>
                                <th style="font-weight: 600; text-align: center;">Celkem<br>Prodáno (Ks)<br></th>
                                <th style="font-weight: 600; text-align: center;">Vzorek produktů<br>(Ks)</th>
                                <th style="font-weight: 600; text-align: center;">Průměr<br>Prodejka (CZK)</th>
                                <th style="font-weight: 600; text-align: center; color: #009944;">Průměr<br>BENAclub (CZK)</th>
                                <th></th>
                                <th style="font-weight: 600; text-align: center;">Průměr<br>Náklady (CZK)</th>
                                <th style="font-weight: 600; text-align: center;">Průměr<br>Zisk (CZK)</th>
                                <th style="font-weight: 600; text-align: center;">Průměr<br>Zisk (%)</th>
                            </thead>
                            <tr style="font-size: 15px; font-weight: 700; text-align: center;">
                                <td class="num"><?php echo $SUM_sold_QTY; ?></td>
                                <td class="num"><?php echo $NUM_products; ?></td>
                                <td class="num"><?php echo $AVG_price; ?></td>
                                <td class="num" style="color: #009944;"><?php echo $AVG_bena_price; ?></td>
                                <td class="num"></td>
                                <td class="num"><?php echo $AVG_costs; ?></td>
                                <td class="num"><?php echo $AVG_profit; ?></td>
                                <td><?php echo round($AVG_profit / $AVG_costs * 100);?>%</td>
                            </tr>
                        </table><br>
                        <table class="widefat" id="itemtab">
                            <thead>
                                <tr>
                                    <th style="min-width: 100px; font-weight: 600;">Prodáno (Ks)<br><input style='font-size: 12px; max-width: 60px; min-height: 20px; margin-left: -3px; margin-top: 3px;' id='solditems' onkeyup='searchTablesolditems()' type='text' placeholder='Filtrovat..'></th>
                                    <th style="font-weight: 600;">Produkt<br><input style='font-size: 12px; max-width: 60px; min-height: 20px; margin-left: -3px; margin-top: 3px;' id='items' onkeyup='searchTableitems()' type='text' placeholder='Filtrovat..'></th>
                                    <th style="font-weight: 600; text-align: center;">Prodejka (CZK)</th>
                                    <th style="font-weight: 600; text-align: center; color: #009944;">BENAclub (CZK)</th>
                                    <th style="font-weight: 600; text-align: center; color: #c62201;">Akce/Sleva (CZK)</th>
                                    <th style="font-weight: 600; text-align: center;">Náklady (CZK)</th>
                                    <th style="font-weight: 600; text-align: center;">Zisk (CZK)</th>
                                    <th style="font-weight: 600; text-align: center; color: #000;">Zisk (%)</th>
                                </tr>
                            </thead>
                            <?php $i = 0; 

                            while ($wp_query->have_posts()) {
                                $wp_query->the_post();

                                $product = wc_get_product(get_the_ID());

                                if ( $product->is_type( 'simple' ) ) {
                                    $product = new WC_Product(get_the_ID());

                                    $product_price = $product->get_regular_price();
                                    $product_bena_price = (ceil(($product_price - ($product_price*(0.06)))/10) *10-1);
                                    $product_sale_price = $product->sale_price;

                                    $costs = (ceil((get_post_meta($product, 'custom_cost_field', true ))/100) *85); // Suppose 15% margin on products

                                    if ($costs == '') {
                                        $costs = ceil($product_price*0.7);
                                    }

                                    $profit = intval($product_price) - $costs;
                                    $profitsale = intval($product_sale_price) - $costs;
                                    $profitperc = round(($profit / $costs)*100);
                                    $profitpercsale = round(($profitsale / $costs)*100);
                                }

                                elseif ($product->is_type( 'variable' )) {
                                    $product = new WC_Product_Variable(get_the_ID());

                                        foreach ($product->get_available_variations() as $variation) {
                                            $variation_id = $variation['variation_id'];
                                            $variation = new WC_Product_Variation($variation_id);

                                            $var_regular_price = get_post_meta($variation_id, '_regular_price', true); // $variation->get_regular_price();
                                            $var_bena_price = (ceil(($var_regular_price-($var_regular_price*(0.06)))/10) *10-1);
                                            $var_sale_price = get_post_meta($variation_id, '_sale_price', true); // $variation->sale_price;

                                            $var_costs = ceil($var_regular_price*0.7);
                                            $var_profit = intval($var_regular_price) - $var_costs;
                                            $var_profitsale = intval($var_sale_price) - $var_costs;

                                            $var_profitperc = round(($var_profit / $var_costs)*100);
                                            $var_profitpercsale = round(($var_profitsale / $var_costs)*100);
                                        }
                                }

                                // $stockQuantity = get_post_meta(get_the_ID(), '_stock', true);
                                $NUM_products = $show_posts;

                                $SUM_sold_QTY += $soldQuantity;
                                $SUM_prices += $product_price += $var_regular_price;
                                $AVG_price = round($SUM_prices / $NUM_products);

                                $SUM_bena_prices += $product_bena_price += $var_bena_price;
                                $AVG_bena_price = round($SUM_bena_prices / $NUM_products);

                                $SUM_costs += $costs += $var_costs;
                                $AVG_costs = round($SUM_costs / $NUM_products);

                                $SUM_profits += $profit += $var_profit;
                                $AVG_profit = round($SUM_profits / $NUM_products);
                                $soldQuantity = get_post_meta(get_the_ID(),'total_sales', true);
                                $product_price_ok = true;

                                if ($costs > $product_price) {
                                    $product_price_ok = false;
                                } ?>
                                <tr <?php echo($i % 2 == 0 ? 'class="alternate" style="background-color: #f0ffe0;"' : ''); ?>>
                                    <td class="num"><div><?php echo $soldQuantity; ?></div></td>      
                                    <td><a href="<?php echo get_edit_post_link(); ?>"><?php the_title(); ?></a></td>
                                    <td class="num" <?php echo(!$product_price_ok ? 'style="background-color: #FFA4A4;"' : 'style="background-color: #B2FFB2;"'); ?>><div><strong><?php if ( $product->is_type( 'simple' ) ) { echo $product_price; } elseif ($product->is_type( 'variable' )) { echo $var_regular_price; }?></strong></div></td>
                                    <td style="color: #009944;" class="num"><div><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $product_sale_price; } else { echo $product_bena_price; }} elseif ( $product->is_type( 'variable' ) ) { if ($product_sale_price > 0) { echo $var_sale_price; } else { echo $var_bena_price; }} ?></strong></div></td>
                                    <td style="color: #c62201;" class="num"><div><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $product_sale_price; }} elseif ( $product->is_type( 'variable' ) ) { if ($product_sale_price > 0) { echo $var_sale_price; }}?></strong></div></td>
                                    <td <?php echo(!$product_price_ok ? 'style="background-color: #FFA4A4;"' : ''); ?> class="num"><?php if ( $product->is_type( 'simple' ) ) { echo $costs; } elseif ( $product->is_type( 'variable' ) ) { echo $var_costs; }?></td>
                                    <td class="num"><font color="green"><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $profitsale; } else { echo $profit; }} elseif ( $product->is_type( 'variable' ) ) { if ($var_sale_price > 0) { echo $var_profitsale; } else { echo $var_profit; }}?></strong></font></td>
                                    <td class="num" style="font-size: 14px; <?php if ( $product->is_type( 'simple' ) ) { echo($profit < 20 ? 'background-color: yellow;' : ''); echo($profit > 59 ? 'background-color: transparent;' : ''); echo($profit > 19 && $profit < 60 ? 'background-color: lightgreen;' : ''); } elseif ( $product->is_type( 'variable' ) ) { echo($var_profit < 20 ? 'background-color: yellow;' : ''); echo($var_profit > 59 ? 'background-color: transparent;' : ''); echo($var_profit > 19 && $var_profit < 60 ? 'background-color: lightgreen;' : ''); } ?>"><font color="#000"><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $profitpercsale; } else { echo $profitperc; }} elseif ( $product->is_type( 'variable' ) ) { if ($var_sale_price > 0) { echo $var_profitpercsale; } else { echo $var_profitperc; }} ?>%</strong></font></td>
                                </tr>
                            <?php $i++;
                            }
                            ?>
                        </table>
                        <?php
                    }
                    wp_reset_query();

【问题讨论】:

  • 您的问题中没有 javascript 代码,甚至不清楚您是否使用 jQuery DataTables(您用它来标记您的问题)。如果您寻求 DataTables 解决方案,请分享相关代码,我很确定,我可以提供简单的 jQuery 解决方案。
  • 好吧,我想知道是否可以使用 javascript 刷新表格 - 我已经尝试了一些选项,但没有成功。如果您能分享一些解决方案,我会很高兴。谢谢你:o)

标签: javascript php wordpress html-table datatables


【解决方案1】:

您可以使用以下几种方法:

方法一:使用2个查询。第一个是聚合查询,它将返回所有 SUM、AVG 等值。第二个查询只会获取原始数据 - 就像您现在所做的那样,但不需要计算。

方法 2:不是立即输出数据 - 将其存储在字符串中。可以将字符串视为允许您更改部分字符串的模板。例如:你可以使用 $str = '&lt;td class="num"&gt;[SUMSOLDQTY]&lt;/td&gt;'; 而不是 &lt;td class="num"&gt;&lt;?php echo $SUM_sold_QTY; ?&gt;&lt;/td&gt; 稍后在你的代码中你可以使用 $x = str_replace("[SUMSOLDQTY]", $SUM_sold_Qty, $str);

处理完成后,只需回显变量。

【讨论】:

  • 我将回复粘贴到另一个答案中 - 不幸的是,它不起作用:(。
【解决方案2】:

好的,所以我使用了方法 2,它目前最适合我的代码,但最终由于某种原因这些值不会被替换。

我把 x 放在了错误的地方吗? :S 我尝试了几个位置...但没有结果。

请参阅下面的完整代码。我还添加了结果屏幕。最上面的表甚至没有行 - 只有头部:S。

                    if ($wp_query->have_posts()) {
                        $SUM_sold_QTY = 0;
                        $NUM_products = 0;
                        $AVG_price = 0;
                        $AVG_bena_price = 0;
                        $AVG_costs = 0;
                        $AVG_profit = 0;
                        ?>

                        <table style="border: 1px solid black;" class="widefat" id="itemtabtotals">
                            <thead>
                                <tr>
                                    <th style="font-weight: 600; text-align: center;">Celkem<br>Prodáno (Ks)<br></th>
                                    <th style="font-weight: 600; text-align: center;">Vzorek produktů<br>(Ks)</th>
                                    <th style="font-weight: 600; text-align: center;">Průměr<br>Prodejka (CZK)</th>
                                    <th style="font-weight: 600; text-align: center; color: #009944;">Průměr<br>BENAclub (CZK)</th>
                                    <th style="font-weight: 600; text-align: center;">Průměr<br>Náklady (CZK)</th>
                                    <th style="font-weight: 600; text-align: center;">Průměr<br>Zisk (CZK)</th>
                                    <th style="font-weight: 600; text-align: center;">Průměr<br>Zisk (%)</th>
                                </tr>
                            </thead>
                            <tbody>
                                <tr style="font-size: 15px; font-weight: 700; text-align: center;">
                                    <?php

                                    $str1 = '<td class="num">[SUMSOLDQTY]</td>';
                                    $str2 = '<td class="num">[NUMPRODUCTS]</td>';
                                    $str3 = '<td class="num">[AVGPRICE]</td>';
                                    $str4 = '<td class="num">[AVGBENAPRICE]</td>';
                                    $str5 = '<td class="num">[AVGCOSTS]</td>';
                                    $str6 = '<td class="num">[AVGPROFIT]</td>';
                                    $str7 = '<td class="num">[AVGPROFITPERC]</td>';

                                    ?>
                                </tr>
                            </tbody>
                        </table><br>
                        <table class="widefat" id="itemtab">
                            <thead>
                                <tr>
                                    <th style="min-width: 100px; font-weight: 600;">Prodáno (Ks)<br><input style='font-size: 12px; max-width: 60px; min-height: 20px; margin-left: -3px; margin-top: 3px;' id='solditems' onkeyup='searchTablesolditems()' type='text' placeholder='Filtrovat..'></th>
                                    <th style="font-weight: 600;">Produkt<br><input style='font-size: 12px; max-width: 60px; min-height: 20px; margin-left: -3px; margin-top: 3px;' id='items' onkeyup='searchTableitems()' type='text' placeholder='Filtrovat..'></th>
                                    <th style="font-weight: 600; text-align: center;">Prodejka (CZK)</th>
                                    <th style="font-weight: 600; text-align: center; color: #009944;">BENAclub (CZK)</th>
                                    <th style="font-weight: 600; text-align: center; color: #c62201;">Akce/Sleva (CZK)</th>
                                    <th style="font-weight: 600; text-align: center;">Náklady (CZK)</th>
                                    <th style="font-weight: 600; text-align: center;">Zisk (CZK)</th>
                                    <th style="font-weight: 600; text-align: center; color: #000;">Zisk (%)</th>
                                </tr>
                            </thead>
                            <?php $i = 0; 

                            while ($wp_query->have_posts()) {
                                $wp_query->the_post();

                                $product = wc_get_product(get_the_ID());

                                if ( $product->is_type( 'simple' ) ) {
                                    $product = new WC_Product(get_the_ID());

                                    $product_price = $product->get_regular_price();
                                    $product_bena_price = (ceil(($product_price - ($product_price*(0.06)))/10) *10-1);
                                    $product_sale_price = $product->sale_price;

                                    $costs = (ceil((get_post_meta($product, 'custom_cost_field', true ))/100) *85); // Suppose 15% margin on products

                                    if ($costs == '') {
                                        $costs = ceil($product_price*0.7);
                                    }

                                    $profit = intval($product_price) - $costs;
                                    $profitsale = intval($product_sale_price) - $costs;
                                    $profitperc = round(($profit / $costs)*100);
                                    $profitpercsale = round(($profitsale / $costs)*100);
                                }

                                elseif ($product->is_type( 'variable' )) {
                                    $product = new WC_Product_Variable(get_the_ID());

                                        foreach ($product->get_available_variations() as $variation) {
                                            $variation_id = $variation['variation_id'];
                                            $variation = new WC_Product_Variation($variation_id);

                                            $var_regular_price = get_post_meta($variation_id, '_regular_price', true); // $variation->get_regular_price();
                                            $var_bena_price = (ceil(($var_regular_price-($var_regular_price*(0.06)))/10) *10-1);
                                            $var_sale_price = get_post_meta($variation_id, '_sale_price', true); // $variation->sale_price;

                                            $var_costs = ceil($var_regular_price*0.7);
                                            $var_profit = intval($var_regular_price) - $var_costs;
                                            $var_profitsale = intval($var_sale_price) - $var_costs;

                                            $var_profitperc = round(($var_profit / $var_costs)*100);
                                            $var_profitpercsale = round(($var_profitsale / $var_costs)*100);
                                        }
                                }

                                // $stockQuantity = get_post_meta(get_the_ID(), '_stock', true);
                                $NUM_products = $show_posts;

                                $SUM_sold_QTY += $soldQuantity;
                                $SUM_prices += $product_price += $var_regular_price;
                                $AVG_price = round($SUM_prices / $NUM_products);

                                $SUM_bena_prices += $product_bena_price += $var_bena_price;
                                $AVG_bena_price = round($SUM_bena_prices / $NUM_products);

                                $SUM_costs += $costs += $var_costs;
                                $AVG_costs = round($SUM_costs / $NUM_products);

                                $SUM_profits += $profit += $var_profit;
                                $AVG_profit = round($SUM_profits / $NUM_products);
                                $AVG_profitperc = round($AVG_profit / $AVG_costs * 100);
                                $soldQuantity = get_post_meta(get_the_ID(),'total_sales', true);
                                $product_price_ok = true;

                                if ($costs > $product_price) {
                                    $product_price_ok = false;
                                } ?>
                                <tbody>
                                    <tr <?php echo($i % 2 == 0 ? 'class="alternate" style="background-color: #f0ffe0;"' : ''); ?>>
                                        <td class="num"><div><?php echo $soldQuantity; ?></div></td>      
                                        <td><a href="<?php echo get_edit_post_link(); ?>"><?php the_title(); ?></a></td>
                                        <td class="num" <?php echo(!$product_price_ok ? 'style="background-color: #FFA4A4;"' : 'style="background-color: #B2FFB2;"'); ?>><div><strong><?php if ( $product->is_type( 'simple' ) ) { echo $product_price; } elseif ($product->is_type( 'variable' )) { echo $var_regular_price; }?></strong></div></td>
                                        <td style="color: #009944;" class="num"><div><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $product_sale_price; } else { echo $product_bena_price; }} elseif ( $product->is_type( 'variable' ) ) { if ($product_sale_price > 0) { echo $var_sale_price; } else { echo $var_bena_price; }} ?></strong></div></td>
                                        <td style="color: #c62201;" class="num"><div><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $product_sale_price; }} elseif ( $product->is_type( 'variable' ) ) { if ($product_sale_price > 0) { echo $var_sale_price; }}?></strong></div></td>
                                        <td <?php echo(!$product_price_ok ? 'style="background-color: #FFA4A4;"' : ''); ?> class="num"><?php if ( $product->is_type( 'simple' ) ) { echo $costs; } elseif ( $product->is_type( 'variable' ) ) { echo $var_costs; }?></td>
                                        <td class="num"><font color="green"><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $profitsale; } else { echo $profit; }} elseif ( $product->is_type( 'variable' ) ) { if ($var_sale_price > 0) { echo $var_profitsale; } else { echo $var_profit; }}?></strong></font></td>
                                        <td class="num" style="font-size: 14px; <?php if ( $product->is_type( 'simple' ) ) { echo($profit < 20 ? 'background-color: yellow;' : ''); echo($profit > 59 ? 'background-color: transparent;' : ''); echo($profit > 19 && $profit < 60 ? 'background-color: lightgreen;' : ''); } elseif ( $product->is_type( 'variable' ) ) { echo($var_profit < 20 ? 'background-color: yellow;' : ''); echo($var_profit > 59 ? 'background-color: transparent;' : ''); echo($var_profit > 19 && $var_profit < 60 ? 'background-color: lightgreen;' : ''); } ?>"><font color="#000"><strong><?php if ( $product->is_type( 'simple' ) ) { if ($product_sale_price > 0) { echo $profitpercsale; } else { echo $profitperc; }} elseif ( $product->is_type( 'variable' ) ) { if ($var_sale_price > 0) { echo $var_profitpercsale; } else { echo $var_profitperc; }} ?>%</strong></font></td>
                                    </tr>
                                </tbody>
                            <?php $i++;
                                $x1 = str_replace("[SUMSOLDQTY]", $SUM_sold_Qty, $str1);
                                $x2 = str_replace("[NUMPRODUCTS]", $NUM_products, $str2);
                                $x3 = str_replace("[AVGPRICE]", $AVG_price, $str3);
                                $x4 = str_replace("[AVGBENAPRICE]", $AVG_bena_price, $str4);
                                $x5 = str_replace("[AVGCOSTS]", $AVG_costs, $str5);
                                $x6 = str_replace("[AVGPROFIT]", $AVG_profit, $str6);
                                $x7 = str_replace("[AVGPROFITPERC]", $AVG_profitperc, $str7);
                            }
                            ?>
                        </table>
                        <?php
                    }
                    wp_reset_query();

【讨论】:

  • 您的问题可以使用 DataTables drawCallback 选项轻松解决,该选项在 DataTable 准备就绪时触发自定义代码(填充您的汇总表)。但是,不幸的是,您没有分享您的 jQuery 代码,所以我无法发布解决您问题的确切代码。
  • 还有两件事 - 你不应该将答案作为另一个答案发布,并且在服务器端制作表格的 HTML 是一个糟糕的选择,DataTables 可以自己完美地做到这一点。
  • 这是我目前所拥有的,我没有任何 jQuery :(.
  • 我已经多次回答过类似的问题(例如herehere),但它需要您修改jQuery 代码。否则,我能为你做的不多。
猜你喜欢
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多