Skip to content

Instantly share code, notes, and snippets.

@lucasstark
Created December 12, 2019 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucasstark/548bc6279277cbd1d3e1f24b9c455236 to your computer and use it in GitHub Desktop.
Save lucasstark/548bc6279277cbd1d3e1f24b9c455236 to your computer and use it in GitHub Desktop.
class WC_Custom_Dynamic_Pricing_Context {
/**
* @var WC_Custom_Dynamic_Pricing_Context
*/
private static $instance;
/**
* Helper to bootstrap the class.
*/
public static function register() {
if ( self::$instance == null ) {
self::$instance = new WC_Custom_Dynamic_Pricing_Context();
}
}
/**
* @return WC_Custom_Dynamic_Pricing_Context
*/
public static function instance() {
if ( self::$instance == null ) {
self::$instance = new WC_Custom_Dynamic_Pricing_Context();
}
return self::$instance;
}
/**
* @var array Stores references to products in the cart.
*/
private $_products_in_cart;
private function __construct() {
add_filter( 'woocommerce_get_cart_item_from_session', array(
$this,
'on_get_cart_item_from_session'
), 9999, 3 );
}
/**
* Record the reference to the cart item product object.
*
* @param $session_data
* @param $values
* @param $cart_item_key
*
* @return mixed
*/
public function on_get_cart_item_from_session( $session_data, $values, $cart_item_key ) {
if ( isset( $session_data['discounts'] ) ) {
unset( $session_data['discounts'] );
}
$this->_products_in_cart[ spl_object_hash( $session_data['data'] ) ] = $cart_item_key;
return $session_data;
}
/**
* @param $product
*
* @return array|null Item data or null if product instance is not in the cart.
*/
public function get_cart_item_for_product( &$product ) {
if ( WC()->cart ) {
$cart_item_key = null;
$cart_item = null;
$hash_key = spl_object_hash( $product );
if ( $this->_products_in_cart && isset( $this->_products_in_cart[ $hash_key ] ) ) {
$cart_item_key = $this->_products_in_cart[ $hash_key ];
$cart_item = null;
if ( $cart_item_key ) {
$cart_item = WC()->cart->get_cart_item( $cart_item_key );
}
}
return $cart_item;
} else {
return false;
}
}
}
WC_Custom_Dynamic_Pricing_Context::register();
function ejc_price_for_wholesale( $price, $product ) {
if ( current_user_can( 'wholesaler' ) ):
$cart_item = WC_Custom_Dynamic_Pricing_Context::instance()->get_cart_item_for_product( $product );
if ( $cart_item ) {
$base_price = get_post_meta( $product->get_id(), 'custom_wholesale_price_simple', true );
$gravity_form_price = isset( $cart_item['_gform_total'] ) ? floatval( $cart_item['_gform_total'] ) : 0;
$price = $base_price + $gravity_form_price;
} else {
$price = get_post_meta( $product->get_id(), 'custom_wholesale_price_simple', true );
}
endif;
return $price;
}
add_filter( 'woocommerce_product_get_price', 'ejc_price_for_wholesale', 0, 2 );
add_filter( 'woocommerce_product_get_sale_price', 'ejc_price_for_wholesale', 0, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment