【问题标题】:Display WooCommerce Regular Price in Table Cell在表格单元格中显示 WooCommerce 常规价格
【发布时间】:2020-04-29 03:16:10
【问题描述】:

我正在尝试将以下代码中的表格单元格替换为 woocommerce 常规产品价格。

我的 functions.php 文件中有这段代码,用于将定价表添加到内容区域。

如何动态更改单元格 (class="pricing-table-full-price") 以显示常规产品价格?

add_filter ('the_content', 'insertPricingTable');
function insertPricingTable($content) {
if(is_single() && has_term( 'resin', 'product_cat' ) ) {
  $content.= '<table class="pricing-table">';
  $content.= '<tbody>';
  $content.= '<tr>';
  $content.= '<th>1 - 5</th>';
  $content.= '<th>6 - 11</th>';
  $content.= '<th>12+</th></tr>';
  $content.= '<tr>';
  $content.= '<td class="pricing-table-full-price">Full Price</td>';
  $content.= '<td class="pricing-table-35">35% OFF</td>';
  $content.= '<td class="pricing-table-45">45% OFF</td>';
  $content.= '</tr></tbody></table>';
}
return $content;
}

【问题讨论】:

    标签: php wordpress woocommerce price


    【解决方案1】:

    为此,您需要以某种方式获取您希望显示其价格的产品 ID。在您的情况下,这可以使用全局变量 $post 来完成。

        function insertPricingTable($content) {
        if(is_single() && has_term( 'resin', 'product_cat' ) ) {
          global $post;
          $product = new WC_Product($post->ID);
          $product_price = $product->get_price();
          $content.= '<table class="pricing-table">';
          $content.= '<tbody>';
          $content.= '<tr>';
          $content.= '<th>1 - 5</th>';
          $content.= '<th>6 - 11</th>';
          $content.= '<th>12+</th></tr>';
          $content.= '<tr>';
          $content.= '<td class="pricing-table-full-price">'.product_price.'</td>';
          $content.= '<td class="pricing-table-35">35% OFF</td>';
          $content.= '<td class="pricing-table-45">45% OFF</td>';
          $content.= '</tr></tbody></table>';
        }
        return $content;
        }
    

    【讨论】:

    • 谢谢,这成功了。我稍作修改,使用 bcmul 使用 35%/45% 折扣字段。有一个更好的方法吗? $num_str1 = $product_price; $num_str2 = "0.65"; $scaleVal = 2; $resone = bcmul($num_str1, $num_str2, $scaleVal); $content.= '&lt;td class="pricing-table-35"&gt;'.$resone.'&lt;/td&gt;';
    猜你喜欢
    • 2017-05-15
    • 1970-01-01
    • 2020-08-21
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多