admin_url('admin-ajax.php'), 'checkout_url' => wc_get_checkout_url(), 'nonce' => wp_create_nonce('ADIBNQC_security_nonce') ]); $this->print_dynamic_css(); } private function print_dynamic_css() { $s = get_option( 'adibnqc_settings', [] ); $css = ""; $gen = function($prefix, $selector) use ($s) { $bg = isset($s[$prefix.'_bg_color']) ? sanitize_hex_color($s[$prefix.'_bg_color']) : ''; $txt = isset($s[$prefix.'_text_color']) ? sanitize_hex_color($s[$prefix.'_text_color']) : ''; if( empty($bg) ) return ""; return "body {$selector} { background-color: {$bg} !important; color: {$txt} !important; }"; }; $css .= $gen('grid_atc', '.adibnqc-grid-atc'); $css .= $gen('grid_bn', '.adibnqc-grid-bn'); wp_add_inline_style( 'adibnqc-frontend', wp_strip_all_tags( $css ) ); } public function sc_single_buttons( $atts ) { $a = shortcode_atts( [ 'atc_text' => '', 'bn_text' => esc_html__( 'Buy Now', 'adi-buy-now-quick-cart' ), ], $atts ); // ১. Buy Now বাটনের জন্য লজিক $buy_now = function() use ($a) { // এই দুটি হিডেন ইনপুট ফিল্ড রিডাইরেক্টের জন্য বাধ্যতামূলক echo ''; echo ''; echo ''; }; // ২. বাটনের টেক্সট ফিল্টার (যদি ইউজার কাস্টম টেক্সট দেয়) if ( ! empty( $a['atc_text'] ) ) { add_filter( 'woocommerce_product_single_add_to_cart_text', function() use ($a) { return esc_html( $a['atc_text'] ); }, 999 ); } add_action( 'woocommerce_after_add_to_cart_button', $buy_now, 10 ); ob_start(); // উকমার্স এর ডিফল্ট ফর্ম কল করা woocommerce_template_single_add_to_cart(); $output = ob_get_clean(); // ফিল্টার এবং অ্যাকশন ক্লিনআপ remove_action( 'woocommerce_after_add_to_cart_button', $buy_now, 10 ); if ( ! empty( $a['atc_text'] ) ) { remove_all_filters( 'woocommerce_product_single_add_to_cart_text' ); } // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped return '
'; } public function handle_buy_now_redirect( $url ) { if ( ! isset( $_REQUEST['adibnqc_buy_now'] ) || '1' !== $_REQUEST['adibnqc_buy_now'] ) { return $url; } if ( ! isset( $_REQUEST['adibnqc_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['adibnqc_nonce'] ) ), 'adibnqc_buy_now_action' ) ) { return $url; } return wc_get_checkout_url(); } public function sc_grid_atc() { $ADIBNQC_product = null; if ( isset( $GLOBALS['product'] ) && is_object( $GLOBALS['product'] ) ) { $ADIBNQC_product = $GLOBALS['product']; } else { $ADIBNQC_product = wc_get_product( get_the_ID() ); } if ( ! $ADIBNQC_product ) { return ''; } $product_id = $ADIBNQC_product->get_id(); $is_var = $ADIBNQC_product->is_type( 'variable' ); $url = $is_var ? get_permalink( $product_id ) : '?add-to-cart=' . $product_id; $cls = $is_var ? 'adibnqc-grid-atc adibnqc-link' : 'adibnqc-grid-atc adibnqc-ajax-btn'; $settings = get_option( 'adibnqc_settings', [] ); $text = ! empty( $settings['grid_atc_text_label'] ) ? $settings['grid_atc_text_label'] : esc_html__( 'Add to Cart', 'adi-buy-now-quick-cart' ); return sprintf( '%s', esc_url( $url ), esc_attr( $cls ), esc_attr( $product_id ), esc_html( $text ) ); } public function sc_grid_bn() { $ADIBNQC_product = null; if ( isset( $GLOBALS['product'] ) && is_object( $GLOBALS['product'] ) ) { $ADIBNQC_product = $GLOBALS['product']; } else { $ADIBNQC_product = wc_get_product( get_the_ID() ); } if ( ! $ADIBNQC_product ) { return ''; } $product_id = $ADIBNQC_product->get_id(); $is_var = $ADIBNQC_product->is_type( 'variable' ); if ( $is_var ) { $url = get_permalink( $product_id ); } else { $url = add_query_arg( [ 'add-to-cart' => $product_id, 'quantity' => 1, 'adibnqc_buy_now' => 1, 'adibnqc_nonce' => wp_create_nonce( 'adibnqc_buy_now_action' ) ], wc_get_checkout_url() ); } $settings = get_option( 'adibnqc_settings', [] ); $text = ! empty( $settings['grid_bn_text_label'] ) ? $settings['grid_bn_text_label'] : esc_html__( 'Buy Now', 'adi-buy-now-quick-cart' ); return sprintf( '%s', esc_url( $url ), esc_html( $text ) ); } public function ajax_atc() { check_ajax_referer( 'ADIBNQC_security_nonce' ); $product_id = isset($_POST['product_id']) ? absint( $_POST['product_id'] ) : 0; if ( $product_id && \WC()->cart->add_to_cart( $product_id, 1 ) ) { \WC_AJAX::get_refreshed_fragments(); } wp_die(); } }