qr-code.php
152 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Elementor; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | class ShopEngine_Qr_Code extends \ShopEngine\Base\Widget |
| 8 | { |
| 9 | |
| 10 | public function config() |
| 11 | { |
| 12 | return new ShopEngine_Qr_Code_Config(); |
| 13 | } |
| 14 | |
| 15 | protected function register_controls() |
| 16 | { |
| 17 | |
| 18 | $this->start_controls_section( |
| 19 | 'shopengine-qrcode-conent', |
| 20 | [ |
| 21 | 'label' => __('QR Code', 'shopengine'), |
| 22 | ] |
| 23 | ); |
| 24 | |
| 25 | $this->add_control( |
| 26 | 'shopengine_size', |
| 27 | [ |
| 28 | 'label' => __('Size', 'shopengine'), |
| 29 | 'type' => Controls_Manager::NUMBER, |
| 30 | 'min' => 0, |
| 31 | 'max' => 1000, |
| 32 | 'step' => 1, |
| 33 | 'default' => 150, |
| 34 | ] |
| 35 | ); |
| 36 | |
| 37 | $this->add_control( |
| 38 | 'shopengine_add_cart_url', |
| 39 | [ |
| 40 | 'label' => __('Scan to Add to Cart URL', 'shopengine'), |
| 41 | 'description' => __('If enabled, the QR code will generate a link that adds the product to the cart when scanned.', 'shopengine'), |
| 42 | 'type' => Controls_Manager::SWITCHER, |
| 43 | 'return_value' => 'yes', |
| 44 | 'default' => 'no', |
| 45 | ] |
| 46 | ); |
| 47 | |
| 48 | $this->add_control( |
| 49 | 'shopengine_direct_payment_url', |
| 50 | [ |
| 51 | 'label' => __('Scan to Direct Payment URL', 'shopengine'), |
| 52 | 'description' => __('If enabled, the QR code will generate a link that adds the product to cart and redirects directly to checkout.', 'shopengine'), |
| 53 | 'type' => Controls_Manager::SWITCHER, |
| 54 | 'return_value' => 'yes', |
| 55 | 'default' => 'no', |
| 56 | ] |
| 57 | ); |
| 58 | |
| 59 | $this->add_control( |
| 60 | 'shopengine_quantity', |
| 61 | [ |
| 62 | 'label' => esc_html__('Quantity', 'shopengine'), |
| 63 | 'type' => Controls_Manager::NUMBER, |
| 64 | 'min' => 0, |
| 65 | 'max' => 500, |
| 66 | 'step' => 1, |
| 67 | 'default' => 1, |
| 68 | 'conditions' => [ |
| 69 | 'relation' => 'or', |
| 70 | 'terms' => [ |
| 71 | [ |
| 72 | 'name' => 'shopengine_add_cart_url', |
| 73 | 'operator' => '===', |
| 74 | 'value' => 'yes', |
| 75 | ], |
| 76 | [ |
| 77 | 'name' => 'shopengine_direct_payment_url', |
| 78 | 'operator' => '===', |
| 79 | 'value' => 'yes', |
| 80 | ], |
| 81 | ], |
| 82 | ], |
| 83 | ] |
| 84 | ); |
| 85 | |
| 86 | $this->add_responsive_control( |
| 87 | 'shopengine_code_align', |
| 88 | [ |
| 89 | 'label' => esc_html__('Alignment', 'shopengine'), |
| 90 | 'type' => Controls_Manager::CHOOSE, |
| 91 | 'options' => [ |
| 92 | 'left' => [ |
| 93 | 'title' => esc_html__('Left', 'shopengine'), |
| 94 | 'icon' => 'eicon-text-align-left', |
| 95 | ], |
| 96 | 'center' => [ |
| 97 | 'title' => esc_html__('Center', 'shopengine'), |
| 98 | 'icon' => 'eicon-text-align-center', |
| 99 | ], |
| 100 | 'right' => [ |
| 101 | 'title' => esc_html__('Right', 'shopengine'), |
| 102 | 'icon' => 'eicon-text-align-right', |
| 103 | ], |
| 104 | ], |
| 105 | 'selectors' => [ |
| 106 | '{{WRAPPER}} .shopengine-qrcode' => 'text-align: {{VALUE}};', |
| 107 | ], |
| 108 | 'separator' => 'before', |
| 109 | ] |
| 110 | ); |
| 111 | |
| 112 | $this->end_controls_section(); |
| 113 | } |
| 114 | |
| 115 | protected function screen() |
| 116 | { |
| 117 | |
| 118 | $post_type = get_post_type(); |
| 119 | $product = \ShopEngine\Widgets\Products::instance()->get_product($post_type); |
| 120 | $settings = $this->get_settings_for_display(); |
| 121 | $product_id = $product->get_id(); |
| 122 | $quantity = ( !empty($settings['shopengine_quantity'] ) ? $settings['shopengine_quantity'] : 1 ); |
| 123 | |
| 124 | if ($settings['shopengine_add_cart_url'] == 'yes') { |
| 125 | |
| 126 | $url = get_the_permalink( $product_id) . sprintf('?add-to-cart=%s&quantity=%s', $product_id, $quantity ); |
| 127 | |
| 128 | } elseif ($settings['shopengine_direct_payment_url'] == 'yes') { |
| 129 | |
| 130 | $checkout_url = wc_get_checkout_url(); |
| 131 | $url = $checkout_url . sprintf('?add-to-cart=%s&quantity=%s', $product_id, $quantity ); |
| 132 | |
| 133 | } else { |
| 134 | |
| 135 | $url = get_the_permalink( $product_id ); |
| 136 | } |
| 137 | |
| 138 | $title = get_the_title( $product_id ); |
| 139 | $product_url = urlencode($url); |
| 140 | $size = ( !empty($settings['shopengine_size']) ? $settings['shopengine_size'] : 150 ); |
| 141 | $size = absint( $size ); |
| 142 | $dimension = esc_attr($size . 'x' . $size); |
| 143 | $image_url = sprintf( 'https://api.qrserver.com/v1/create-qr-code/?size=%s&ecc=L&qzone=1&data=%s', $dimension, $product_url); |
| 144 | |
| 145 | ?> |
| 146 | <div class="shopengine-qrcode"> |
| 147 | <img src="<?php echo esc_url( $image_url ) ?>" alt="<?php echo esc_attr( $title ); ?>"> |
| 148 | </div> |
| 149 | <?php |
| 150 | } |
| 151 | } |
| 152 |