Advertisement
majeedraza

WooCommerce - Show price on add-to-cart button

Jun 8th, 2020
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.07 KB | None | 0 0
  1. <?php
  2.   // WooCommerce - Show price on add-to-cart button
  3.   // credit for most of it: LoicTheAztec
  4.   add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Shop and other archives pages
  5.   add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_to_cart_price', 20, 2 ); // Single product pages
  6.   function custom_add_to_cart_price( $button_text, $product ) {
  7.     // variable products
  8.     if( $product->is_type( 'variable' ) ) {
  9.       // shop and archives
  10.       if( ! is_product() ){
  11.         $product_price = wc_price( wc_get_price_to_display( $product, array( 'price' => $product->get_variation_price() ) ) );
  12.         return $button_text . ' - From ' . strip_tags( $product_price );
  13.       }
  14.       // single product pages
  15.       else {
  16.         return $button_text. ' - From ' . $product->get_variation_price( 'min' );
  17.       }
  18.     }
  19.     // all other product types
  20.     else {
  21.       $product_price = wc_price( wc_get_price_to_display( $product ) );
  22.       return $button_text . ' - Just ' . strip_tags( $product_price );
  23.     }
  24.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement