class Wayra_Cuotas_Public { private $plugin_name; private $version; public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; // Registrar shortcode add_action( 'init', [ $this, 'wayra_installments_add_short_code' ] ); } public function enqueue_styles() { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/wayra-cuotas-public.css', [], $this->version, 'all' ); } // SHORTCODE public function wayra_installments_render_shortcode( $atts ) { global $product; // Asegurar contexto de producto if ( empty( $product ) || ! $product instanceof WC_Product ) { return ''; } $attributes = shortcode_atts( [ 'image' => 'true', 'store' => 'false' ], (array) $atts ); $config = (array) get_option( 'wayra-cuotas', [] ); if ( empty( $config['installments'] ) || ! is_array( $config['installments'] ) ) { return ''; } // Flags producto / categorías $show_installments_product = ''; if ( ( $config['disable_on_products'] ?? '' ) === 'yes' ) { $show_installments_product = get_post_meta( $product->get_id(), 'wayra_cuotas_product', true ); } $show_installments_category = ''; if ( ( $config['disable_on_categories'] ?? '' ) === 'yes' ) { foreach ( (array) $product->get_category_ids() as $category_id ) { if ( get_term_meta( $category_id, 'wayra_cuotas_category', true ) === 'no' ) { $show_installments_category = 'no'; } } } if ( $show_installments_product === 'no' || $show_installments_category === 'no' ) { return ''; } $product_price = (float) $product->get_price(); if ( $product_price <= 0 ) { return ''; } // Cuotas por producto $product_installments = []; if ( $show_installments_product !== 'no' ) { $product_installments_saved = (array) get_post_meta( $product->get_id(), 'wayra_cuotas_product_installments', true ); foreach ( (array) $config['installments'] as $installment ) { if ( ( $product_installments_saved[ $installment ] ?? '' ) !== 'no' ) { $product_installments[] = $installment; } } } // Cuotas por categoría $category_installments = []; if ( $show_installments_category !== 'no' ) { foreach ( (array) $product->get_category_ids() as $category_id ) { $saved = (array) get_term_meta( $category_id, 'wayra_cuotas_category_installments', true ); foreach ( (array) $config['installments'] as $installment ) { if ( ( $saved[ $installment ] ?? '' ) !== 'no' ) { $category_installments[] = $installment; } } } $category_installments = array_values( array_unique( $category_installments ) ); } // Prioridad if ( ( $config['disable_on_products'] ?? '' ) === 'yes' && ( $config['disable_on_categories'] ?? '' ) === 'yes' ) { switch ( $config['installments_priority'] ?? 'product' ) { case 'category': $installments = $category_installments; sort( $installments ); break; case 'sum_both': $installments = array_values( array_unique( array_merge( $product_installments, $category_installments ) ) ); sort( $installments ); break; case 'product': default: $installments = $product_installments; } } elseif ( ( $config['disable_on_products'] ?? '' ) === 'yes' ) { $installments = $product_installments; } elseif ( ( $config['disable_on_categories'] ?? '' ) === 'yes' ) { $installments = $category_installments; } else { $installments = (array) $config['installments']; } if ( empty( $installments ) ) { return ''; } $product_price = apply_filters( 'wayra_installments_price', $product_price ); $installments_img = ( $attributes['image'] === 'true' ) ? ( $config['installments_img'] ?? '' ) : ''; $installments_percent = (array) ( $config['installments_percent'] ?? [] ); $installments_text = (array) ( $config['installments_text'] ?? [] ); $installments_type = ( $attributes['store'] === 'true' ) ? 'store' : 'product'; $extra_percent = apply_filters( 'wayra_installments_extra_percent', 0 ); if ( ( $config['installments_order'] ?? 'asc' ) === 'desc' ) { $installments = array_reverse( $installments ); } // JS (solo en producto) if ( $installments_type === 'product' ) { $data = []; foreach ( $installments as $i ) { $data[] = [ 'installment' => $i, 'text' => ( $installments_text[ $i ] ?? '' ) . __( ' of ', 'wayra-cuotas' ), 'percent' => (float) ( $installments_percent[ $i ] ?? 0 ), 'extraPercent' => (float) $extra_percent, 'thousandSeparator' => wc_get_price_thousand_separator(), 'decimalSeparator' => wc_get_price_decimal_separator(), ]; } if ( ! function_exists( 'is_plugin_active' ) ) { include_once ABSPATH . 'wp-admin/includes/plugin.php'; } $is_tm = is_plugin_active( 'woocommerce-tm-extra-product-options/tm-woo-extra-product-options.php' ); if ( $is_tm ) { wp_register_script( 'wayra-cuotas-tm-extra-product-options', plugin_dir_url( __FILE__ ) . 'js/wayra-cuotas-tm-extra-product-options.js', [], defined('Wayra_Cuotas_VERSION') ? Wayra_Cuotas_VERSION : $this->version, true ); wp_localize_script( 'wayra-cuotas-tm-extra-product-options', 'installments', $data ); wp_enqueue_script( 'wayra-cuotas-tm-extra-product-options' ); } $is_ppom = is_plugin_active( 'woocommerce-product-addon/woocommerce-product-addon.php' ); if ( $is_ppom ) { wp_register_script( 'wayra-cuotas-ppom-for-woocommerce', plugin_dir_url( __FILE__ ) . 'js/wayra-cuotas-ppom-for-woocommerce.js', [], defined('Wayra_Cuotas_VERSION') ? Wayra_Cuotas_VERSION : $this->version, true ); wp_localize_script( 'wayra-cuotas-ppom-for-woocommerce', 'installments', $data ); wp_enqueue_script( 'wayra-cuotas-ppom-for-woocommerce' ); } if ( ! $is_ppom && $product->is_type( 'variable' ) ) { wp_register_script( 'wayra-cuotas-variable-products', plugin_dir_url( __FILE__ ) . 'js/wayra-cuotas-variable-products.js', [], defined('Wayra_Cuotas_VERSION') ? Wayra_Cuotas_VERSION : $this->version, true ); wp_localize_script( 'wayra-cuotas-variable-products', 'installments', $data ); wp_enqueue_script( 'wayra-cuotas-variable-products' ); } } // Render ob_start(); include __DIR__ . '/partials/wayra-cuotas-public-display.php'; return ob_get_clean(); } public function wayra_installments_on_store() { echo do_shortcode( '[wayra_cuotas store=true]' ); } public function wayra_installments_on_product() { echo do_shortcode( '[wayra_cuotas]' ); } public function wayra_installments_add_short_code() { add_shortcode( 'wayra_cuotas', [ $this, 'wayra_installments_render_shortcode' ] ); } }