Last active
August 2, 2020 04:16
-
-
Save damiencarbery/13ef781596d67640a8b8f1c68e20039e to your computer and use it in GitHub Desktop.
Show regular price in cart and checkout when using WooCommerce Wholesale Prices - The plugin does not show regular prices but one developer wanted to see them. https://www.damiencarbery/2019/07/show-regular-price-in-cart-and-checkout-when-using-woocommerce-wholesale-prices/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Wholesale Prices - Show Original Prices | |
Plugin URI: https://www.damiencarbery.com/2019/07/show-regular-price-in-cart-and-checkout-when-using-woocommerce-wholesale-prices/ | |
Description: Show the original prices in the Cart and Checkout pages when using WooCommerce Wholesale Prices plugin. | |
Author: Damien Carbery | |
Version: 0.3 | |
$Id: wwp-show-orig-prices.php 4747 2019-07-10 15:13:51Z damien $ | |
*/ | |
add_filter( 'woocommerce_cart_product_price', 'dcwd_cart_product_price', 10, 2 ); | |
function dcwd_cart_product_price( $formatted_price, $product ) { | |
if ( !class_exists( 'WooCommerceWholeSalePrices' ) ) { | |
return $formatted_price; | |
} | |
if ( !is_user_logged_in() ) { | |
return $formatted_price; | |
} | |
// Check whether the current user has a wholesale role that may have a different price. | |
$wwp_class = WooCommerceWholeSalePrices::instance(); | |
$user_wholesale_role = $wwp_class->wwp_wholesale_roles->getUserWholesaleRole(); | |
if ( empty( $user_wholesale_role ) ) { | |
return $formatted_price; | |
} | |
//error_log( 'Price: Product ID: '. $product->get_id() ); | |
//error_log( 'Price: Formatted price: ' . $formatted_price ); | |
$_product = wc_get_product( $product->get_id() ); | |
//error_log( 'Price: Product price: '. $_product->get_price() ); | |
//error_log( 'Price: Original price, formatted: ' . wc_price( $_product->get_price() ) ); | |
return $formatted_price . '<br /><del>' . wc_price( $_product->get_price() ) . '</del>'; | |
} | |
add_filter( 'woocommerce_cart_item_subtotal', 'dcwd_cart_item_subtotal', 10, 3 ); | |
function dcwd_cart_item_subtotal( $product_subtotal, $cart_item, $cart_item_key ) { | |
if ( !class_exists( 'WooCommerceWholeSalePrices' ) ) { | |
return $product_subtotal; | |
} | |
if ( !is_user_logged_in() ) { | |
return $product_subtotal; | |
} | |
// Check whether the current user has a wholesale role that may have a different price. | |
$wwp_class = WooCommerceWholeSalePrices::instance(); | |
$user_wholesale_role = $wwp_class->wwp_wholesale_roles->getUserWholesaleRole(); | |
if ( empty( $user_wholesale_role ) ) { | |
return $product_subtotal; | |
} | |
//error_log( 'Subtotal: Product subtotal: ' . $product_subtotal ); | |
//error_log( 'Subtotal: Product ID: '. $cart_item[ 'product_id' ] ); | |
$_product = wc_get_product( $cart_item[ 'product_id' ] ); | |
// If it's a variable product get the info about this variation. | |
if ( $_product->is_type( 'variable' ) ) { | |
$_product = wc_get_product( $cart_item[ 'variation_id' ] ); | |
} | |
//error_log( 'Subtotal: Original price, formatted: ' . wc_price( $_product->get_price() * $cart_item['quantity'] ) ); | |
return $product_subtotal . '<br /><del>' . wc_price( $_product->get_price() * $cart_item['quantity'] ) . '</del>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment