简体   繁体   中英

add colum discount on cart table woocommerce

hello I want to add a column in the cart table with the discount percentage can you help me?

I have this code for the product page

////__________________________________________________________________________________________////
//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;
}

The easiest way to add a column to the cart page with values depending on the cart item is to override the cart.php template.

From the WooCommerce plugin, copy woocommerce/cart/cart.php to yourTheme/woocommerce/cart/ . If you are not using a child theme, I suggest you create a child theme and override templates through that so in the case your theme gets updated, your template changes won't be be lost. More on child themes .

From there you can look through the cart.php for where you want to insert the header for the discount percentage, and also insert the data (in this case the discount in percentage).

To get the label for the table header, it's simple. Just add the HTML for the label in the thead of the table. In my example, this is found in 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.

To get and display the discount percentage, you'll have to go through the template and find the correct location for it. In my example, I will put it in between the price and quantity, directly under the discount heading. In cart.php , this would be line 102 . From there you can just write the HTML and PHP code to calculate the percentage based on the cart items regular price and sale price:

<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. In my example, the top product is on sale and bottom product is not on sale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM