【问题标题】:add colum discount on cart table woocommerce在购物车表 woocommerce 上添加列折扣
【发布时间】:2022-12-20 04:25:22
【问题描述】:

你好我想在购物车表中添加一列折扣百分比 你能帮助我吗?

我有产品页面的代码

////__________________________________________________________________________________________////
//AGREGA EL PORCENTAJE DE DESCUENTO JUNTO AL PRECIO MAYORISTA
// Only for WooCommerce version 3.0+
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 );
function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) {
    $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%';
    $percentage_txt = ' ' . __(' (-', 'woocommerce' ) . $percentage . __(' )', 'woocommerce' );
    $price = '<del>' . ( is_numeric( $regular_price ) ? wc_price( $regular_price ) : $regular_price ) . '</del> <ins>' . ( is_numeric( $sale_price ) ? wc_price( $sale_price ) . $percentage_txt : $sale_price . $percentage_txt ) . '</ins>';
    return $price;
}

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    向购物车页面添加一列的值取决于购物车项目的最简单方法是覆盖 cart.php 模板。

    从 WooCommerce 插件中,将 woocommerce/cart/cart.php 复制到 yourTheme/woocommerce/cart/。如果您没有使用子主题,我建议您创建一个子主题并通过它覆盖模板,以便在您的主题更新的情况下,您的模板更改不会丢失。更多关于child themes

    从那里您可以查看 cart.php 以查找要插入折扣百分比标题的位置,并插入数据(在本例中为百分比折扣)。

    要获取表头的标签,很简单。只需在表格的thead 中添加标签的 HTML。在我的示例中,这是在cart.php line 51-59 中找到的:

    <thead>
      <tr>
        <th class="product-name" colspan="3"><?php esc_html_e( 'Product', 'woocommerce' ); ?></th>
        <th class="product-price"><?php esc_html_e( 'Price', 'woocommerce' ); ?></th>
        <th class="product-discount"><?php esc_html_e( 'Discount', 'woocommerce' ); ?></th> // added this line
        <th class="product-quantity"><?php esc_html_e( 'Quantity', 'woocommerce' ); ?></th>
        <th class="product-subtotal"><?php esc_html_e( 'Subtotal', 'woocommerce' ); ?></th>
      </tr>
    </thead>
    

    There will then be a discount label in the head of the table in the cart.

    要获取并显示折扣百分比,您必须浏览模板并为其找到正确的位置。在我的例子中,我将把它放在价格和数量之间,直接在折扣标题下。在 cart.php 中,这将是 line 102。从那里您可以编写 HTML 和 PHP 代码来计算基于购物车商品正常价格和促销价格的百分比:

    <td class="product-discount">
        <?php
         if($_product->get_sale_price() != ''){
            $reg_price = $_product->get_regular_price();
            $sale_price = $_product->get_sale_price();
            $percentage = ((($sale_price / $reg_price) - 1) * -1) * 100 . "%";
            echo $percentage;
            }
          ?>
    </td>
    

    You can now see that on the cart page it shows the discount percentage based on the cart item. 在我的例子中,最上面的产品在打折,最下面的产品没有打折。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-24
      • 1970-01-01
      • 2019-06-28
      • 2014-03-28
      • 2015-01-25
      • 1970-01-01
      • 2018-10-11
      • 2020-11-27
      相关资源
      最近更新 更多