| @@ -1,54 +1,71 @@ | ||
| 1 | 1 | <?php namespace TierPricingTable\Integrations\Plugins; |
| 2 | 2 | |
| 3 | 3 | use TierPricingTable\PriceManager; |
| 4 | +use TierPricingTable\PricingRule; | |
| 4 | 5 | |
| 5 | 6 | class WCCS extends PluginIntegrationAbstract { |
| 6 | - | |
| 7 | + | |
| 7 | 8 | public function run() { |
| 8 | - | |
| 9 | + | |
| 9 | 10 | add_filter( 'tiered_pricing_table/price/price_by_rules', |
| 10 | - function ( $product_price, $quantity, $product_id, $context ) { | |
| 11 | - | |
| 11 | + function ( $product_price, $quantity, $product_id, $context, $place, PricingRule $pricingRule ) { | |
| 12 | + | |
| 12 | 13 | global $WCCS; |
| 13 | - | |
| 14 | - if ( $WCCS ) { | |
| 15 | - | |
| 14 | + | |
| 15 | + // Percentage tiers are derived from the product's 'view' price, which WCCS has already converted. | |
| 16 | + // Converting again here would double-convert the price. | |
| 17 | + if ( $pricingRule->isPercentage() ) { | |
| 18 | + return $product_price; | |
| 19 | + } | |
| 20 | + | |
| 21 | + if ( $WCCS && $product_price ) { | |
| 22 | + | |
| 16 | 23 | $product = wc_get_product( $product_id ); |
| 17 | - | |
| 24 | + | |
| 18 | 25 | if ( 'view' === $context && $product ) { |
| 19 | 26 | return $WCCS->wccs_custom_price( $product_price, $product ); |
| 20 | 27 | } |
| 21 | 28 | } |
| 22 | - | |
| 29 | + | |
| 23 | 30 | return $product_price; |
| 24 | - | |
| 31 | + | |
| 25 | 32 | }, 10, 10 ); |
| 26 | - | |
| 33 | + | |
| 27 | 34 | add_filter( 'tiered_pricing_table/cart/product_cart_price', |
| 28 | 35 | function ( $price, $cartItem, $cartItemKey, $totalQuantity ) { |
| 29 | - | |
| 36 | + | |
| 30 | 37 | global $WCCS; |
| 31 | - | |
| 38 | + | |
| 32 | 39 | if ( ! $WCCS ) { |
| 33 | 40 | return $price; |
| 34 | 41 | } |
| 35 | - | |
| 42 | + | |
| 36 | 43 | if ( $price ) { |
| 37 | 44 | return PriceManager::getPriceByRules( $totalQuantity, $cartItem['data']->get_id(), 'edit', 'cart', |
| 38 | 45 | false ); |
| 39 | 46 | } |
| 40 | - | |
| 47 | + | |
| 41 | 48 | return $price; |
| 42 | 49 | }, 10, 4 ); |
| 50 | + | |
| 51 | + add_filter( 'tiered_pricing_table/cart/recalculate_cart_item_subtotal', function ( $state ) { | |
| 52 | + global $WCCS; | |
| 53 | + | |
| 54 | + if ( $WCCS ) { | |
| 55 | + return false; | |
| 56 | + } | |
| 57 | + | |
| 58 | + return $state; | |
| 59 | + } ); | |
| 43 | 60 | } |
| 44 | 61 | |
| 45 | 62 | public function getTitle(): string { |
| 46 | - return __( 'WooCommerce Currency Switcher by WP Experts (WCCS)', 'tier-pricing-table' ); | |
| 63 | + return 'WooCommerce Currency Switcher by WP Experts (WCCS)'; | |
| 47 | 64 | } |
| 48 | 65 | |
| 49 | 66 | public function getDescription(): string { |
| 50 | - return __( 'Make the tiered pricing properly work with multiple currencies.', 'tier-pricing-table' ); | |
| 67 | + return __( 'Convert and display tiered pricing correctly when using the WooCommerce Currency Switcher by WP Experts.', 'tier-pricing-table' ); | |
| 51 | 68 | } |
| 52 | 69 | |
| 53 | 70 | public function getIconURL(): string { |
| 54 | 71 | return $this->getContainer()->getFileManager()->locateAsset( 'admin/integrations/wccs-icon.png' ); |