You've already forked wc-tier-and-package-prices
Release version 1.0.1
- Add Twig template engine integration - Migrate all templates to Twig format - Add German (Switzerland, Informal) translation - Improve template organization and security - Add Composer dependency management - Create comprehensive changelog
This commit is contained in:
94
includes/class-wc-tpp-cart.php
Normal file
94
includes/class-wc-tpp-cart.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
/**
|
||||
* Cart price calculation for tier and package pricing
|
||||
*/
|
||||
|
||||
if (!defined('ABSPATH')) {
|
||||
exit;
|
||||
}
|
||||
|
||||
class WC_TPP_Cart {
|
||||
|
||||
public function __construct() {
|
||||
add_action('woocommerce_before_calculate_totals', array($this, 'apply_tier_package_pricing'), 10, 1);
|
||||
add_filter('woocommerce_cart_item_price', array($this, 'display_cart_item_price'), 10, 3);
|
||||
add_filter('woocommerce_cart_item_subtotal', array($this, 'display_cart_item_subtotal'), 10, 3);
|
||||
}
|
||||
|
||||
public function apply_tier_package_pricing($cart) {
|
||||
if (is_admin() && !defined('DOING_AJAX')) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Prevent infinite loops
|
||||
if (did_action('woocommerce_before_calculate_totals') >= 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if cart object is valid
|
||||
if (!$cart || !is_a($cart, 'WC_Cart')) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($cart->get_cart() as $cart_item_key => $cart_item) {
|
||||
$product_id = $cart_item['product_id'];
|
||||
$quantity = $cart_item['quantity'];
|
||||
$product = $cart_item['data'];
|
||||
|
||||
// Validate product object
|
||||
if (!$product || !is_a($product, 'WC_Product')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check for exact package match first
|
||||
$package_price = null;
|
||||
if (get_option('wc_tpp_enable_package_pricing') === 'yes') {
|
||||
$package_price = WC_TPP_Frontend::get_package_price($product_id, $quantity);
|
||||
}
|
||||
|
||||
if ($package_price !== null) {
|
||||
// Apply package pricing (total price divided by quantity)
|
||||
$unit_price = $package_price / $quantity;
|
||||
$product->set_price($unit_price);
|
||||
// Store pricing information in cart item for display
|
||||
WC()->cart->cart_contents[$cart_item_key]['wc_tpp_pricing_type'] = 'package';
|
||||
WC()->cart->cart_contents[$cart_item_key]['wc_tpp_total_price'] = $package_price;
|
||||
} else {
|
||||
// Apply tier pricing if no package match
|
||||
if (get_option('wc_tpp_enable_tier_pricing') === 'yes') {
|
||||
$tier_price = WC_TPP_Frontend::get_tier_price($product_id, $quantity);
|
||||
if ($tier_price !== null) {
|
||||
$product->set_price($tier_price);
|
||||
// Store pricing information in cart item for display
|
||||
WC()->cart->cart_contents[$cart_item_key]['wc_tpp_pricing_type'] = 'tier';
|
||||
WC()->cart->cart_contents[$cart_item_key]['wc_tpp_unit_price'] = $tier_price;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function display_cart_item_price($price, $cart_item, $cart_item_key) {
|
||||
if (isset($cart_item['wc_tpp_pricing_type'])) {
|
||||
if ($cart_item['wc_tpp_pricing_type'] === 'package') {
|
||||
$total_price = isset($cart_item['wc_tpp_total_price']) ? $cart_item['wc_tpp_total_price'] : $cart_item['line_total'];
|
||||
$unit_price = $total_price / $cart_item['quantity'];
|
||||
return wc_price($unit_price) . ' <small class="wc-tpp-notice">(' . __('Package price', 'wc-tier-package-prices') . ')</small>';
|
||||
} elseif ($cart_item['wc_tpp_pricing_type'] === 'tier') {
|
||||
$unit_price = isset($cart_item['wc_tpp_unit_price']) ? $cart_item['wc_tpp_unit_price'] : $cart_item['data']->get_price();
|
||||
return wc_price($unit_price) . ' <small class="wc-tpp-notice">(' . __('Volume discount', 'wc-tier-package-prices') . ')</small>';
|
||||
}
|
||||
}
|
||||
return $price;
|
||||
}
|
||||
|
||||
public function display_cart_item_subtotal($subtotal, $cart_item, $cart_item_key) {
|
||||
if (isset($cart_item['wc_tpp_pricing_type']) && $cart_item['wc_tpp_pricing_type'] === 'package') {
|
||||
$total_price = isset($cart_item['wc_tpp_total_price']) ? $cart_item['wc_tpp_total_price'] : $cart_item['line_total'];
|
||||
return wc_price($total_price);
|
||||
}
|
||||
return $subtotal;
|
||||
}
|
||||
}
|
||||
|
||||
new WC_TPP_Cart();
|
||||
Reference in New Issue
Block a user