Factory.php
1 week ago
MulticurrencyHooks.php
1 week ago
SharedHooks.php
1 week ago
class-wcml-wc-subscriptions.php
1 week ago
MulticurrencyHooks.php
212 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\Compatibility\WcSubscriptions; |
| 4 | |
| 5 | use WC_Product; |
| 6 | use WC_Product_Subscription_Variation; |
| 7 | use WC_Product_Variable_Subscription; |
| 8 | use WCML\Orders\Helper as OrdersHelper; |
| 9 | use WCML\Utilities\DB; |
| 10 | use WCML_Custom_Prices_UI; |
| 11 | use woocommerce_wpml; |
| 12 | use wpdb; |
| 13 | use function WPML\FP\tap as tap; |
| 14 | |
| 15 | class MulticurrencyHooks implements \IWPML_Action { |
| 16 | |
| 17 | private $woocommerce_wpml; |
| 18 | |
| 19 | private $wpdb; |
| 20 | |
| 21 | private $newSubscription = false; |
| 22 | |
| 23 | private $proratingPrice = false; |
| 24 | |
| 25 | public function __construct( woocommerce_wpml $woocommerce_wpml, wpdb $wpdb ) { |
| 26 | $this->woocommerce_wpml = $woocommerce_wpml; |
| 27 | $this->wpdb = $wpdb; |
| 28 | } |
| 29 | |
| 30 | public function add_hooks() { |
| 31 | add_action( 'init', [ $this, 'init' ], 9 ); |
| 32 | |
| 33 | add_filter( 'wcml_custom_prices_fields', [ $this, 'set_prices_fields' ], 10, 2 ); |
| 34 | add_filter( 'wcml_custom_prices_strings', [ $this, 'set_labels_for_prices_fields' ], 10, 2 ); |
| 35 | add_filter( 'wcml_custom_prices_fields_labels', [ $this, 'set_labels_for_prices_fields' ], 10, 2 ); |
| 36 | add_filter( 'wcml_update_custom_prices_values', [ $this, 'update_custom_prices_values' ], 10, 3 ); |
| 37 | add_action( 'wcml_after_custom_prices_block', [ $this, 'new_subscription_prices_block' ] ); |
| 38 | |
| 39 | add_filter( 'woocommerce_subscriptions_product_price', [ $this, 'woocommerce_subscription_price_from' ], 10, 2 ); |
| 40 | } |
| 41 | |
| 42 | public function init() { |
| 43 | if ( ! is_admin() ) { |
| 44 | add_filter( 'woocommerce_subscriptions_product_sign_up_fee', [ $this, 'subscriptions_product_sign_up_fee_filter' ], 10, 2 ); |
| 45 | $this->maybe_force_client_currency_for_subscription(); |
| 46 | } |
| 47 | |
| 48 | add_filter( 'wcs_switch_proration_new_price_per_day', tap( [ $this, 'set_prorating_price' ] ) ); |
| 49 | } |
| 50 | |
| 51 | public function set_prorating_price() { |
| 52 | $this->proratingPrice = true; |
| 53 | } |
| 54 | |
| 55 | public function subscriptions_product_sign_up_fee_filter( $subscriptionSignUpFee, $product ) { |
| 56 | if ( is_object( $product ) && ! $this->proratingPrice ) { |
| 57 | $currency = $this->woocommerce_wpml->multi_currency->get_client_currency(); |
| 58 | |
| 59 | if ( wcml_get_woocommerce_currency_option() !== $currency ) { |
| 60 | $productId = $product->get_id(); |
| 61 | |
| 62 | if ( $product instanceof WC_Product_Variable_Subscription ) { |
| 63 | $productId = $product->get_meta( '_min_price_variation_id', true ); |
| 64 | } |
| 65 | |
| 66 | $originalProductId = $this->woocommerce_wpml->products->get_original_product_id( $productId ); |
| 67 | |
| 68 | if ( get_post_meta( $originalProductId, '_wcml_custom_prices_status', true ) ) { |
| 69 | $subscriptionSignUpFee = get_post_meta( $originalProductId, '_subscription_sign_up_fee_' . $currency, true ); |
| 70 | } else { |
| 71 | $subscriptionSignUpFee = apply_filters( 'wcml_raw_price_amount', $subscriptionSignUpFee ); |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return $subscriptionSignUpFee; |
| 77 | } |
| 78 | |
| 79 | public function maybe_force_client_currency_for_subscription() { |
| 80 | $subscriptionId = false; |
| 81 | $getData = wpml_collect( $_GET ); |
| 82 | |
| 83 | if ( $getData->has( 'resubscribe' ) ) { |
| 84 | $subscriptionId = (int) $getData->get( 'resubscribe' ); |
| 85 | } elseif ( $getData->has( 'subscription_renewal_early' ) ) { |
| 86 | $subscriptionId = (int) $getData->get( 'subscription_renewal_early' ); |
| 87 | } elseif ( is_cart() || is_checkout() ) { |
| 88 | $resubscribeCartItem = wcs_cart_contains_resubscribe(); |
| 89 | if ( $resubscribeCartItem ) { |
| 90 | $subscriptionId = $resubscribeCartItem['subscription_resubscribe']['subscription_id']; |
| 91 | } else { |
| 92 | $earlyRenewalCartItem = wcs_cart_contains_early_renewal(); |
| 93 | if ( $earlyRenewalCartItem ) { |
| 94 | $subscriptionId = $earlyRenewalCartItem['subscription_renewal']['subscription_renewal_early']; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if ( $subscriptionId ) { |
| 100 | $subscriptionCurrency = OrdersHelper::getCurrency( $subscriptionId ); |
| 101 | if ( $subscriptionCurrency && $this->woocommerce_wpml->multi_currency->get_client_currency() !== $subscriptionCurrency ) { |
| 102 | $this->woocommerce_wpml->multi_currency->set_client_currency( $subscriptionCurrency ); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public function set_prices_fields( $fields, $productId ) { |
| 108 | if ( $this->isSubscriptionsProduct( $productId ) || $this->newSubscription ) { |
| 109 | $fields[] = '_subscription_sign_up_fee'; |
| 110 | } |
| 111 | |
| 112 | return $fields; |
| 113 | } |
| 114 | |
| 115 | public function set_labels_for_prices_fields( $labels, $productId ) { |
| 116 | if ( $this->isSubscriptionsProduct( $productId ) || $this->newSubscription ) { |
| 117 | $labels['_regular_price'] = __( 'Subscription Price', 'woocommerce-multilingual' ); |
| 118 | $labels['_subscription_sign_up_fee'] = __( 'Sign-up Fee', 'woocommerce-multilingual' ); |
| 119 | } |
| 120 | |
| 121 | return $labels; |
| 122 | } |
| 123 | |
| 124 | public function update_custom_prices_values( $prices, $code, $variationId = false ) { |
| 125 | if ( isset( $_POST['_custom_subscription_sign_up_fee'][ $code ] ) ) { |
| 126 | $prices['_subscription_sign_up_fee'] = wc_format_decimal( $_POST['_custom_subscription_sign_up_fee'][ $code ] ); |
| 127 | } |
| 128 | |
| 129 | if ( $variationId && isset( $_POST['_custom_variation_subscription_sign_up_fee'][ $code ][ $variationId ] ) ) { |
| 130 | $prices['_subscription_sign_up_fee'] = wc_format_decimal( $_POST['_custom_variation_subscription_sign_up_fee'][ $code ][ $variationId ] ); |
| 131 | } |
| 132 | |
| 133 | return $prices; |
| 134 | } |
| 135 | |
| 136 | public function new_subscription_prices_block( $productId ) { |
| 137 | if ( 'new' === $productId ) { |
| 138 | $this->newSubscription = true; |
| 139 | echo '<div class="wcml_prices_if_subscription" style="display: none">'; |
| 140 | $custom_prices_ui = new WCML_Custom_Prices_UI( $this->woocommerce_wpml, 'new' ); |
| 141 | $custom_prices_ui->show(); |
| 142 | echo '</div>'; |
| 143 | ?> |
| 144 | <script> |
| 145 | jQuery(function($) { |
| 146 | jQuery('.wcml_prices_if_subscription .wcml_custom_prices_input').attr('name', '_wcml_custom_prices[new_subscription]').attr( 'id', '_wcml_custom_prices[new_subscription]'); |
| 147 | jQuery('.wcml_prices_if_subscription .wcml_custom_prices_options_block>label').attr('for', '_wcml_custom_prices[new_subscription]'); |
| 148 | jQuery('.wcml_prices_if_subscription .wcml_schedule_input').each( function(){ |
| 149 | jQuery(this).attr('name', jQuery(this).attr('name')+'_subscription'); |
| 150 | }); |
| 151 | |
| 152 | jQuery('.options_group>.wcml_custom_prices_block .wcml_custom_prices_input:first-child').click(); |
| 153 | jQuery('.options_group>.wcml_custom_prices_block .wcml_schedule_options .wcml_schedule_input:first-child').click(); |
| 154 | |
| 155 | jQuery(document).on('change', 'select#product-type', function () { |
| 156 | if (jQuery(this).val() == 'subscription') { |
| 157 | jQuery('.wcml_prices_if_subscription').show(); |
| 158 | jQuery('.options_group>.wcml_custom_prices_block').hide(); |
| 159 | } else if (jQuery(this).val() != 'variable-subscription') { |
| 160 | jQuery('.wcml_prices_if_subscription').hide(); |
| 161 | jQuery('.options_group>.wcml_custom_prices_block').show(); |
| 162 | } |
| 163 | }); |
| 164 | |
| 165 | jQuery(document).on('click', '#publish', function () { |
| 166 | if ( jQuery('.wcml_prices_if_subscription').is( ':visible' ) ) { |
| 167 | jQuery('.options_group>.wcml_custom_prices_block').remove(); |
| 168 | jQuery('.wcml_prices_if_subscription .wcml_custom_prices_input').attr('name', '_wcml_custom_prices[new]'); |
| 169 | jQuery('.wcml_prices_if_subscription .wcml_schedule_input').each( function(){ |
| 170 | jQuery(this).attr('name', jQuery(this).attr('name').replace('_subscription','') ); |
| 171 | }); |
| 172 | }else{ |
| 173 | jQuery('.wcml_prices_if_subscription').remove(); |
| 174 | } |
| 175 | }); |
| 176 | }); |
| 177 | </script> |
| 178 | <?php |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | private function isSubscriptionsProduct( $productId ) { |
| 183 | $variationTermTaxonomyIds = $this->wpdb->get_col( "SELECT tt.term_taxonomy_id FROM {$this->wpdb->terms} AS t LEFT JOIN {$this->wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE t.slug IN ( 'subscription', 'variable-subscription' ) AND tt.taxonomy = 'product_type'" ); |
| 184 | |
| 185 | if ( get_post_type( $productId ) == 'product_variation' ) { |
| 186 | $productId = wp_get_post_parent_id( $productId ); |
| 187 | } |
| 188 | |
| 189 | return (bool) $this->wpdb->get_var( |
| 190 | $this->wpdb->prepare( |
| 191 | "SELECT count(object_id) FROM {$this->wpdb->term_relationships} |
| 192 | WHERE object_id = %d AND term_taxonomy_id IN (" . DB::prepareIn( $variationTermTaxonomyIds, '%d' ) . ')', |
| 193 | $productId |
| 194 | ) |
| 195 | ); |
| 196 | } |
| 197 | |
| 198 | public function woocommerce_subscription_price_from( $price, $product ) { |
| 199 | if ( $product instanceof WC_Product_Subscription_Variation ) { |
| 200 | $customPricesOn = get_post_meta( $product->get_id(), '_wcml_custom_prices_status', true ); |
| 201 | |
| 202 | if ( $customPricesOn ) { |
| 203 | $price = get_post_meta( $product->get_id(), '_price_' . $this->woocommerce_wpml->multi_currency->get_client_currency(), true ); |
| 204 | } else { |
| 205 | $price = apply_filters( 'wcml_raw_price_amount', $price ); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return $price; |
| 210 | } |
| 211 | } |
| 212 |