From 21238119fb3182a19da602371c53dd171a83378e Mon Sep 17 00:00:00 2001 From: Soares e Neves Date: Wed, 5 Jun 2019 11:55:42 -0300 Subject: [PATCH] =?UTF-8?q?Adiciona=20plano=20da=20Vindi=20nas=20Assinatur?= =?UTF-8?q?as=20Vari=C3=A1veis=20(#118)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adicionado plano da Vindi nas variações * Adicionado plano padrão da Vindi na aba geral para assinaturas variáveis * Alterado nome do campo do plano de variação * Corrigido bug que não trocava de 12 meses para 1 ano nas variações * Adicionado novo hook para Assinatura Simples * Assinaturas simples salvam o valor conforme o plano da vindi. * Versão 5.5.0 --- CHANGELOG.md | 6 + assets/js/simple-subscription-fields.js | 17 +- includes/class-vindi-payment.php | 10 +- readme.txt | 9 +- ...mple-product-subscription-fields.html.php} | 14 +- ...iable-product-subscription-fields.html.php | 16 ++ vindi-woocommerce-subscriptions.php | 180 ++++++++++++------ 7 files changed, 179 insertions(+), 73 deletions(-) rename templates/{admin-product-subscription-fields.html.php => admin-simple-product-subscription-fields.html.php} (66%) create mode 100644 templates/admin-variable-product-subscription-fields.html.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 1966881..577bf70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Notas das versões +## [5.5.0 - 04/06/2019](https://github.com/vindi/vindi-woocommerce-subscriptions/releases/tag/5.5.0) + +### Adicionado +- Adiciona plano da Vindi para assinaturas do tipo variável + + ## [5.4.2 - 13/03/2019](https://github.com/vindi/vindi-woocommerce-subscriptions/releases/tag/5.4.3) ### Ajustado diff --git a/assets/js/simple-subscription-fields.js b/assets/js/simple-subscription-fields.js index 6015d84..7d6e191 100755 --- a/assets/js/simple-subscription-fields.js +++ b/assets/js/simple-subscription-fields.js @@ -8,11 +8,18 @@ var id = $(this).val(); var plan = plan_infos[id]; - console.log($(".wc_input_subscription_period_interval")); - - $(".wc_input_subscription_period_interval").val(plan.interval_count); - $(".wc_input_subscription_period").val(plan.interval.toString().replace(/s/g, '')); - $(".wc_input_subscription_length").val(plan.billing_cycles || 0); + $("#_subscription_period_interval").val(plan.interval_count); + $("#_subscription_period").val(plan.interval.toString().replace(/s/g, '')); + $("#_subscription_length").val(plan.billing_cycles || 0); + }); + + $(document).on("change", ".variable_vindi_subscription_plan", function(){ + var id = $(this).val(); + var plan = plan_infos[id]; + + $(this).parents(".data").find(".wc_input_subscription_period_interval").val(plan.interval_count); + $(this).parents(".data").find(".wc_input_subscription_period").val(plan.interval.toString().replace(/s/g, '')); + $(this).parents(".data").find(".wc_input_subscription_length").val(plan.billing_cycles || 0); }); }); }(jQuery) diff --git a/includes/class-vindi-payment.php b/includes/class-vindi-payment.php index 4215305..930e686 100755 --- a/includes/class-vindi-payment.php +++ b/includes/class-vindi-payment.php @@ -68,7 +68,15 @@ public function get_plan() foreach($items as $item) { $product = $this->order->get_product_from_item($item); - $vindi_plan = get_post_meta($product->id, 'vindi_subscription_plan', true); + + if (isset($item['variation_id']) && $item['variation_id'] != 0) { + $vindi_plan = get_post_meta($item['variation_id'], 'vindi_variable_subscription_plan', true); + if (empty($vindi_plan) || !is_numeric($vindi_plan) || is_null($vindi_plan) || $vindi_plan == 0){ + $vindi_plan = get_post_meta($product->id, 'vindi_subscription_plan', true); + } + } + else + $vindi_plan = get_post_meta($product->id, 'vindi_subscription_plan', true); if ($this->is_subscription_type($product) AND !empty($vindi_plan)) return $vindi_plan; diff --git a/readme.txt b/readme.txt index e510937..dd82afd 100644 --- a/readme.txt +++ b/readme.txt @@ -3,10 +3,10 @@ Contributors: erico.pedroso, tales.galvao.vindi, wnarde, lyoncesar, laertejr, rt Website Link: https://www.vindi.com.br Tags: vindi, subscriptions, pagamento-recorrente, cobranca-recorrente, cobrança-recorrente, recurring, site-de-assinatura, assinaturas, faturamento-recorrente, recorrencia, assinatura, woocommerce-subscriptions, vindi-woocommerce Requires at least: 4.4 -Tested up to: 4.9.8 +Tested up to: 5.2.1 WC requires at least: 3.0.0 -WC tested up to: 3.4.5 -Stable Tag: 5.4.2 +WC tested up to: 3.6.4 +Stable Tag: 5.5.0 License: GPLv3 License URI: http://www.gnu.org/licenses/gpl-3.0.html @@ -63,6 +63,9 @@ Caso necessite de informações sobre a plataforma ou API por favor siga atravé == Changelog == += 5.5.0 - 04/06/2019 = +- Adiciona plano da Vindi para assinaturas do tipo variável + = 5.4.2 - 13/03/2019 = - Ajusta validação dos dados da empresa durante o checkout diff --git a/templates/admin-product-subscription-fields.html.php b/templates/admin-simple-product-subscription-fields.html.php similarity index 66% rename from templates/admin-product-subscription-fields.html.php rename to templates/admin-simple-product-subscription-fields.html.php index 4340faf..67f5485 100644 --- a/templates/admin-product-subscription-fields.html.php +++ b/templates/admin-simple-product-subscription-fields.html.php @@ -15,15 +15,23 @@ display: none !important; } -
'vindi_subscription_plan', - 'label' => __('Plano da Vindi', VINDI_IDENTIFIER), + 'label' => $label, 'options' => $plans['names'], - 'description' => __('Selecione o plano da Vindi que deseja relacionar a esse produto', VINDI_IDENTIFIER), + 'description' => $description, 'desc_tip' => true, 'value' => $selected_plan, 'custom_attributes' => array( diff --git a/templates/admin-variable-product-subscription-fields.html.php b/templates/admin-variable-product-subscription-fields.html.php new file mode 100644 index 0000000..bc08c15 --- /dev/null +++ b/templates/admin-variable-product-subscription-fields.html.php @@ -0,0 +1,16 @@ + +
+ + 'vindi_variable_subscription_plan[' . $loop . ']', + 'label' => __('Plano da Vindi', VINDI_IDENTIFIER), + 'options' => $plans['names'], + 'description' => __('Selecione o plano da Vindi que deseja relacionar a esse produto', VINDI_IDENTIFIER), + 'desc_tip' => true, + 'value' => $selected_plan, + 'class' => 'select short variable_vindi_subscription_plan' + )); +?> +
+
\ No newline at end of file diff --git a/vindi-woocommerce-subscriptions.php b/vindi-woocommerce-subscriptions.php index adc61fc..256dbc6 100755 --- a/vindi-woocommerce-subscriptions.php +++ b/vindi-woocommerce-subscriptions.php @@ -3,13 +3,13 @@ * Plugin Name: Vindi Woocommerce * Plugin URI: * Description: Adiciona o gateway de pagamentos da Vindi para o WooCommerce. - * Version: 5.4.2 + * Version: 5.5.0 * Author: Vindi * Author URI: https://www.vindi.com.br * Requires at least: 4.4 - * Tested up to: 4.9.8 + * Tested up to: 5.2.1 * WC requires at least: 3.0.0 - * WC tested up to: 3.4.5 + * WC tested up to: 3.6.4 * * Text Domain: vindi-woocommerce-subscriptions * Domain Path: /languages/ @@ -39,7 +39,7 @@ class Vindi_WooCommerce_Subscriptions /** * @var string */ - const VERSION = '5.4.2'; + const VERSION = '5.5.0'; /** * @var string @@ -113,40 +113,52 @@ public function __construct() &$this, 'user_related_orders_actions' ), 100, 2); - add_filter('woocommerce_subscription_period_interval_strings', - function ($intervals) { - foreach ([7, 8, 9, 10, 11, 12, 13] as $new_interval) { - array_push($intervals, $new_interval); - } - - return $intervals; - } + add_filter('woocommerce_subscription_period_interval_strings', array( + &$this, 'set_supported_intervals') ); add_action('woocommerce_customer_save_address', array( &$this, 'sync_vindi_user_information' ), 1, 2 ); - if(is_admin()) { - + if (is_admin()) { add_action('admin_enqueue_scripts', array( &$this, 'add_admin_scripts' )); add_action('woocommerce_product_options_general_product_data', - array(&$this, 'subscription_custom_fields') + array(&$this, 'simple_subscription_custom_fields') ); + add_action('woocommerce_product_after_variable_attributes', + array(&$this, 'variable_subscription_custom_fields') + , 10, 3); + add_action('woocommerce_process_product_meta', array(&$this, 'save_subscription_meta') , 20); - add_action('woocommerce_ajax_save_product_variations', - array(&$this, 'save_ajax_subscription_meta') - , 10); + add_action('save_post', + array(&$this, 'save_subscription_meta') + , 20); + + add_action('woocommerce_save_product_variation', + array(&$this, 'save_subscription_variation_meta') + , 20, 2); } } + /** + * Set supported intervals number for subscription plans + */ + public function set_supported_intervals($current_intervals) + { + foreach (range(7, 60) as $new_interval) { + array_push($current_intervals, $new_interval); + } + return $current_intervals; + } + /** * Update user informations from My Account form */ @@ -173,25 +185,49 @@ public function sync_vindi_user_information($user_id, $address_type) } /** - * Show pricing fields at admin's product page. - */ - public function subscription_custom_fields() + * Show pricing fields at admin's product page. + */ + public function simple_subscription_custom_fields() + { + global $post; + + $plans = $this->settings->api->get_plans(); + $selected_plan = get_post_meta($post->ID, 'vindi_subscription_plan', true); + $wc_product = wc_get_product($post->ID); + $product_type = $wc_product->get_type(); + $plans['names'] = array(__('-- Selecione --', VINDI_IDENTIFIER)) + $plans['names']; + + $this->settings->get_template( + 'admin-simple-product-subscription-fields.html.php', + compact( + 'plans', + 'selected_plan', + 'product_type' + ) + ); + } + + /** + * Show pricing fields at admin's variable product page. + */ + public function variable_subscription_custom_fields($loop, $variation_data, $variation) { global $post; $plans = $this->settings->api->get_plans(); - $selected_plan = get_post_meta($post->ID, 'vindi_subscription_plan', true); + $selected_plan = get_post_meta($variation->ID, 'vindi_variable_subscription_plan', true); - $plans['names'] = array(__('-- Selecione --', VINDI_IDENTIFIER)) + $plans['names']; + $plans['names'] = array(__('-- Plano padrão da Vindi --', VINDI_IDENTIFIER)) + $plans['names']; $this->settings->get_template( - 'admin-product-subscription-fields.html.php', + 'admin-variable-product-subscription-fields.html.php', compact( 'plans', - 'selected_plan' + 'selected_plan', + 'loop' ) ); - } + } /** * @param int $post_id @@ -208,16 +244,22 @@ public function save_subscription_meta($post_id) $subscription_period = wc_clean($_POST['_subscription_period']); $subscription_length = wc_clean($_POST['_subscription_length']); - if(empty($subscription_period_interval)) { + if (empty($subscription_period_interval)) { return; } - if($subscription_period_interval % 12 == 0) { + if ($this->is_yearly_plan($subscription_period_interval, $subscription_period)) { $years_interval = (int) $subscription_period_interval / 12; update_post_meta($post_id, '_subscription_period_interval', $years_interval); update_post_meta($post_id, '_subscription_period', 'year'); update_post_meta($post_id, 'vindi_subscription_period_interval', $years_interval); update_post_meta($post_id, 'vindi_subscription_period', 'year'); + } else if ($this->is_weekly_plan($subscription_period_interval, $subscription_period)) { + $weeks_interval = (int) $subscription_period_interval / 7; + update_post_meta($post_id, '_subscription_period_interval', $weeks_interval); + update_post_meta($post_id, '_subscription_period', 'week'); + update_post_meta($post_id, 'vindi_subscription_period_interval', $weeks_interval); + update_post_meta($post_id, 'vindi_subscription_period', 'week'); } else { update_post_meta($post_id, '_subscription_period_interval', $subscription_period_interval); update_post_meta($post_id, '_subscription_period', $subscription_period); @@ -226,43 +268,39 @@ public function save_subscription_meta($post_id) } update_post_meta($post_id, 'vindi_subscription_plan', $subscription_plan); - - - if(preg_match('/variable-subscription/', $wc_product->get_type())) { - foreach ($wc_product->get_children() as $child) { - update_post_meta($child, '_subscription_length', $subscription_length); - if($subscription_period_interval % 12 == 0) { - update_post_meta($child, '_subscription_period_interval', $years_interval); - update_post_meta($child, '_subscription_period', 'year'); - } else { - update_post_meta($child, '_subscription_period_interval', $subscription_period_interval); - update_post_meta($child, '_subscription_period', $subscription_period); - } - } - } } - public function save_ajax_subscription_meta($post_id) + /** + * @param int $variation_id + */ + public function save_subscription_variation_meta($variation_id, $i) { - if (false === $this->is_product_type_from_post(['variable-subscription'])) { - return; + $child_subscription_plan = wc_clean($_POST['vindi_variable_subscription_plan'][$i]); + $child_subscription_period_interval = wc_clean($_POST['variable_subscription_period_interval'][$i]); + $child_subscription_period = wc_clean($_POST['variable_subscription_period'][$i]); + $child_subscription_length = wc_clean($_POST['variable_subscription_length'][$i]); + + update_post_meta($variation_id, '_subscription_length', $child_subscription_length); + if ($this->is_yearly_plan($child_subscription_period_interval, $child_subscription_period)) { + $child_years_interval = (int) $child_subscription_period_interval / 12; + update_post_meta($variation_id, '_subscription_period_interval', $child_years_interval); + update_post_meta($variation_id, '_subscription_period', 'year'); + update_post_meta($variation_id, 'vindi_subscription_period_interval', $child_years_interval); + update_post_meta($variation_id, 'vindi_subscription_period', 'year'); + } else if ($this->is_weekly_plan($child_subscription_period_interval, $child_subscription_period)) { + $child_weeks_interval = (int) $child_subscription_period_interval / 7; + update_post_meta($variation_id, '_subscription_period_interval', $child_weeks_interval); + update_post_meta($variation_id, '_subscription_period', 'week'); + update_post_meta($variation_id, 'vindi_subscription_period_interval', $child_weeks_interval); + update_post_meta($variation_id, 'vindi_subscription_period', 'week'); + } else { + update_post_meta($variation_id, '_subscription_period_interval', $child_subscription_period_interval); + update_post_meta($variation_id, '_subscription_period', $child_subscription_period); + update_post_meta($variation_id, 'vindi_subscription_period_interval', $child_subscription_period_interval); + update_post_meta($variation_id, 'vindi_subscription_period', $child_subscription_period); } - $subscription_period_interval = get_post_meta($post_id, 'vindi_subscription_period_interval', true); - $subscription_period = get_post_meta($post_id, 'vindi_subscription_period', true); - $subscription_length = get_post_meta($post_id, 'vindi_subscription_length', true); - - update_post_meta($post_id, '_subscription_length', $subscription_length); - update_post_meta($post_id, '_subscription_period_interval', $subscription_period_interval); - update_post_meta($post_id, '_subscription_period', $subscription_period); - - $wc_product = wc_get_product($post_id); - - foreach ($wc_product->get_children() as $child) { - update_post_meta($child, '_subscription_length', $subscription_length); - update_post_meta($child, '_subscription_period_interval', $subscription_period_interval); - update_post_meta($child, '_subscription_period', $subscription_period); - } + update_post_meta($variation_id, 'vindi_variable_subscription_plan', $child_subscription_plan); } private function is_product_type_from_post($allow_types) @@ -270,7 +308,7 @@ private function is_product_type_from_post($allow_types) return in_array( wc_clean($_POST['product-type']), $allow_types - ); + ) && 'product' !== wc_clean($_POST['post-type']); } /** @@ -286,6 +324,26 @@ public static function get_instance() return self::$instance; } + /** + * Returns if it is an weekly periodicity + * @return bool + */ + public function is_weekly_plan($period_interval, $subscription_period) + { + return $period_interval % 7 == 0 + && $period_interval <= 28 + && $subscription_period == 'day'; + } + + /** + * Returns if it is an annual periodicity + * @return bool + */ + public function is_yearly_plan($period_interval, $subscription_period) + { + return $period_interval % 12 == 0 && $subscription_period == 'month'; + } + /** * Include the dependents classes * @param array $classes