Untitled
unknown
php
2 years ago
1.6 kB
9
Indexable
add_filter('woocommerce_cart_item_name', function($name, $cart_item, $cart_item_key){
// 1. Get product
$product_id = $cart_item['product_id'];
if (isset($cart_item['variation_id'])){
if (!empty($cart_item['variation_id']) && $cart_item['variation_id'] != 0){
$product_id = $cart_item['variation_id'];
}
}
$product = wc_get_product($product_id);
// 2. Get current product price (final price, unit price)
if ($product->is_on_sale()){
$current_price = $product->get_sale_price();
} else {
$current_price = $product->get_regular_price();
}
// 3. Adjust current price for tax (this should be identical to the final unit price)
$current_price_tax_adjusted = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $current_price));
// 4. Now let's get the price we compare to. Let's compare it with the user's group price.
$user_group = apply_filters( "b2bking_b2b_group_for_pricing", b2bking()->get_user_group( b2bking()->get_top_parent_account(get_current_user_id()) ), get_current_user_id() );
$group_price = b2bking()->tofloat( get_post_meta( $product_id, "b2bking_regular_product_price_group_" . $user_group, true ) );
// tax adjust it
$group_price_tax_adjusted = b2bking()->b2bking_wc_get_price_to_display( $product, array( 'price' => $group_price ));
// 5. Get difference
$difference = $group_price_tax_adjusted - $current_price_tax_adjusted;
// 6. Display on cart page
$name .= '<br>';
$name .= 'Unit price: '.wc_format_sale_price($group_price_tax_adjusted, $current_price_tax_adjusted);
$name .= '<br>';
$name .= 'Saved: '.wc_price($difference);
return $name;
}, 10, 3);Editor is loading...
Leave a Comment