product.php
1006 lines
| 1 | <?php |
| 2 | defined( 'ABSPATH' ) || exit; |
| 3 | |
| 4 | if ( ! class_exists( 'VI_WOO_BOPO_BUNDLE_Product' ) ) { |
| 5 | class VI_WOO_BOPO_BUNDLE_Product { |
| 6 | protected $settings; |
| 7 | protected static $_instance = null; |
| 8 | protected static $_types = array( |
| 9 | 'bundle', |
| 10 | 'bopobb', |
| 11 | 'composite', |
| 12 | 'grouped', |
| 13 | 'external' |
| 14 | ); |
| 15 | |
| 16 | public static function instance() { |
| 17 | if ( is_null( self::$_instance ) ) { |
| 18 | self::$_instance = new self(); |
| 19 | } |
| 20 | |
| 21 | return self::$_instance; |
| 22 | } |
| 23 | |
| 24 | function __construct() { |
| 25 | $this->settings = VI_WOO_BOPO_BUNDLE_DATA::get_instance(); |
| 26 | // Enqueue backend scripts |
| 27 | add_action( 'admin_enqueue_scripts', array( $this, 'bopobb_admin_enqueue_scripts' ) ); |
| 28 | add_action( 'admin_print_scripts', array( $this, 'bopobb_custom_script' ) ); |
| 29 | |
| 30 | // Rule search |
| 31 | add_action( 'wp_ajax_bopobb_search_cat', array( $this, 'bopobb_search_cat' ) ); |
| 32 | add_action( 'wp_ajax_bopobb_search_tag', array( $this, 'bopobb_search_tag' ) ); |
| 33 | add_action( 'wp_ajax_bopobb_search_product', array( $this, 'bopobb_search_product' ) ); |
| 34 | add_action( 'wp_ajax_bopobb_default_product', array( $this, 'bopobb_default_product' ) ); |
| 35 | |
| 36 | // Add to selector |
| 37 | add_filter( 'product_type_selector', array( $this, 'bopobb_product_type_selector' ) ); |
| 38 | |
| 39 | // Product data tabs |
| 40 | add_filter( 'woocommerce_product_data_tabs', array( $this, 'bopobb_product_data_tabs' ), 10, 1 ); |
| 41 | |
| 42 | // Product filters |
| 43 | add_filter( 'woocommerce_product_filters', array( $this, 'bopobb_product_filters' ) ); |
| 44 | |
| 45 | // Product data panels |
| 46 | add_action( 'woocommerce_product_data_panels', array( $this, 'bopobb_product_data_panels' ) ); |
| 47 | add_action( 'woocommerce_process_product_meta', array( $this, 'bopobb_delete_option_fields' ) ); |
| 48 | add_action( 'woocommerce_process_product_meta_bopobb', array( $this, 'bopobb_save_option_fields' ) ); |
| 49 | |
| 50 | // Product type |
| 51 | add_filter( 'woocommerce_product_class', array( $this, 'bopobb_product_class' ), 10, 2 ); |
| 52 | |
| 53 | add_action( 'edit_form_after_title', array( $this, 'bopobb_shortcode_after_title_detail_filter_menu' ) ); |
| 54 | } |
| 55 | |
| 56 | public function bopobb_product_class( $classname, $product_type ) { |
| 57 | if ( $product_type == 'bopobb' ) { |
| 58 | $classname = 'VI_WOO_BOPO_Type'; |
| 59 | } |
| 60 | |
| 61 | return $classname; |
| 62 | } |
| 63 | |
| 64 | public function bopobb_search_cat() { |
| 65 | check_ajax_referer( 'bopobb_nonce', 'nonce' ); |
| 66 | if ( ! current_user_can( 'manage_options' ) ) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | ob_start(); |
| 71 | |
| 72 | $keyword = isset( $_GET['keyword'] ) ? sanitize_text_field( wp_unslash( $_GET['keyword'] ) ) : ''; |
| 73 | if ( ! $keyword ) { |
| 74 | $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : ''; |
| 75 | } |
| 76 | if ( empty( $keyword ) ) { |
| 77 | die(); |
| 78 | } |
| 79 | $categories = get_terms( |
| 80 | array( |
| 81 | 'taxonomy' => 'product_cat', |
| 82 | 'orderby' => 'name', |
| 83 | 'order' => 'ASC', |
| 84 | 'search' => $keyword, |
| 85 | 'number' => 100 |
| 86 | ) |
| 87 | ); |
| 88 | $items = array(); |
| 89 | if ( count( $categories ) ) { |
| 90 | foreach ( $categories as $category ) { |
| 91 | $item = array( |
| 92 | 'id' => $category->term_id, |
| 93 | 'text' => $category->name |
| 94 | ); |
| 95 | $items[] = $item; |
| 96 | } |
| 97 | } |
| 98 | wp_send_json( $items ); |
| 99 | } |
| 100 | |
| 101 | public function bopobb_search_tag() { |
| 102 | check_ajax_referer( 'bopobb_nonce', 'nonce' ); |
| 103 | if ( ! current_user_can( 'manage_options' ) ) { |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | ob_start(); |
| 108 | |
| 109 | $keyword = isset( $_GET['keyword'] ) ? sanitize_text_field( wp_unslash( $_GET['keyword'] ) ) : ''; |
| 110 | if ( ! $keyword ) { |
| 111 | $keyword = isset( $_POST['keyword'] ) ? sanitize_text_field( wp_unslash( $_POST['keyword'] ) ) : ''; |
| 112 | } |
| 113 | if ( empty( $keyword ) ) { |
| 114 | die(); |
| 115 | } |
| 116 | $categories = get_terms( |
| 117 | array( |
| 118 | 'taxonomy' => 'product_tag', |
| 119 | 'orderby' => 'name', |
| 120 | 'order' => 'ASC', |
| 121 | 'search' => $keyword, |
| 122 | 'number' => 100 |
| 123 | ) |
| 124 | ); |
| 125 | $items = array(); |
| 126 | if ( count( $categories ) ) { |
| 127 | foreach ( $categories as $category ) { |
| 128 | $item = array( |
| 129 | 'id' => $category->term_id, |
| 130 | 'text' => $category->name |
| 131 | ); |
| 132 | $items[] = $item; |
| 133 | } |
| 134 | } |
| 135 | wp_send_json( $items ); |
| 136 | } |
| 137 | |
| 138 | function bopobb_search_product() { |
| 139 | check_ajax_referer( 'bopobb_nonce', 'nonce' ); |
| 140 | if ( ! current_user_can( 'manage_options' ) ) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | ob_start(); |
| 145 | |
| 146 | $keyword = isset( $_GET['keyword'] ) ? sanitize_text_field( wp_unslash( $_GET['keyword'] ) ) : ''; |
| 147 | |
| 148 | if ( empty( $keyword ) ) { |
| 149 | die(); |
| 150 | } |
| 151 | $arg = array( |
| 152 | 'post_status' => 'publish', |
| 153 | 'post_type' => 'product', |
| 154 | 'posts_per_page' => 50, |
| 155 | 'no_found_rows' => true, |
| 156 | 's' => $keyword |
| 157 | |
| 158 | ); |
| 159 | $the_query = new WP_Query( $arg ); |
| 160 | $found_products = array(); |
| 161 | if ( $the_query->have_posts() ) { |
| 162 | while ( $the_query->have_posts() ) { |
| 163 | $the_query->the_post(); |
| 164 | $product_id = get_the_ID(); |
| 165 | $product_title = get_the_title(); |
| 166 | |
| 167 | $the_product = wc_get_product( $product_id ); |
| 168 | $product_type = $the_product->get_type(); |
| 169 | if ( ! in_array( $product_type, $this->settings->get_params( 'bopobb_type_include' ) ) ) { |
| 170 | continue; |
| 171 | } |
| 172 | $product_prefix = ' (' . $product_type . ') '; |
| 173 | if ( ! $the_product->is_in_stock() ) { |
| 174 | continue; |
| 175 | } |
| 176 | $product = array( |
| 177 | 'id' => $product_id, |
| 178 | 'text' => '#' . $product_id . $product_prefix . $product_title |
| 179 | ); |
| 180 | $found_products[] = $product; |
| 181 | |
| 182 | } |
| 183 | // wp_reset_postdata(); |
| 184 | } |
| 185 | wp_send_json( $found_products ); |
| 186 | } |
| 187 | |
| 188 | function bopobb_default_product() { |
| 189 | check_ajax_referer( 'bopobb_nonce', 'nonce' ); |
| 190 | if ( ! current_user_can( 'manage_options' ) ) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | ob_start(); |
| 195 | |
| 196 | $keyword = isset( $_GET['keyword'] ) ? sanitize_text_field( wp_unslash( $_GET['keyword'] ) ) : ''; |
| 197 | $cat_search = isset( $_GET['cat'] ) ? array_map('sanitize_text_field', $_GET['cat'] ) : []; |
| 198 | $ex_cat_search = isset( $_GET['ex_cat'] ) ? array_map('sanitize_text_field', $_GET['ex_cat'] ) : []; |
| 199 | $tag_search = isset( $_GET['tag'] ) ? array_map('sanitize_text_field', $_GET['tag'] ) : []; |
| 200 | $ex_tag_search = isset( $_GET['ex_tag'] ) ? array_map('sanitize_text_field', $_GET['ex_tag'] ) : []; |
| 201 | $prod_search = isset( $_GET['prod'] ) ? array_map('sanitize_text_field', $_GET['prod'] ) : []; |
| 202 | $ex_prod_search = isset( $_GET['ex_prod'] ) ? array_map('sanitize_text_field', $_GET['ex_prod'] ) : []; |
| 203 | |
| 204 | if ( empty( $keyword ) ) { |
| 205 | die(); |
| 206 | } |
| 207 | $arg = array( |
| 208 | 'post_status' => 'publish', |
| 209 | 'post_type' => 'product', |
| 210 | 'posts_per_page' => 10, |
| 211 | 'no_found_rows' => true, |
| 212 | 's' => $keyword |
| 213 | ); |
| 214 | $tax_arr = []; |
| 215 | if ( ! empty( $cat_search ) ) { |
| 216 | $tax_arr[] = [ |
| 217 | 'taxonomy' => 'product_cat', |
| 218 | 'field' => 'term_id', |
| 219 | 'terms' => $cat_search, |
| 220 | ]; |
| 221 | } |
| 222 | if ( ! empty( $ex_cat_search ) ) { |
| 223 | $tax_arr[] = [ |
| 224 | 'taxonomy' => 'product_cat', |
| 225 | 'field' => 'term_id', |
| 226 | 'terms' => $ex_cat_search, |
| 227 | 'operator' => 'NOT IN', |
| 228 | ]; |
| 229 | } |
| 230 | if ( ! empty( $tag_search ) ) { |
| 231 | $tax_arr[] = [ |
| 232 | 'taxonomy' => 'product_tag', |
| 233 | 'field' => 'term_id', |
| 234 | 'terms' => $tag_search, |
| 235 | ]; |
| 236 | } |
| 237 | if ( ! empty( $ex_tag_search ) ) { |
| 238 | $tax_arr[] = [ |
| 239 | 'taxonomy' => 'product_tag', |
| 240 | 'field' => 'term_id', |
| 241 | 'terms' => $ex_tag_search, |
| 242 | 'operator' => 'NOT IN', |
| 243 | ]; |
| 244 | } |
| 245 | if ( ! empty( $tax_arr ) ) { |
| 246 | $arg['tax_query'] = $tax_arr;// phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_tax_query |
| 247 | } |
| 248 | if ( ! empty( $prod_search ) ) { |
| 249 | $arg['post__in'] = $prod_search; |
| 250 | } |
| 251 | $the_query = new WP_Query( $arg ); |
| 252 | $found_products = array(); |
| 253 | if ( $the_query->have_posts() ) { |
| 254 | while ( $the_query->have_posts() ) { |
| 255 | $the_query->the_post(); |
| 256 | $prd = wc_get_product( get_the_ID() ); |
| 257 | |
| 258 | if ( ! empty( $ex_prod_search ) ) { |
| 259 | if ( in_array( strval( $prd->get_id() ), $ex_prod_search, true ) ) { |
| 260 | continue; |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | if ( in_array( $prd->get_type(), self::$_types, true ) ) { |
| 265 | continue; |
| 266 | } |
| 267 | |
| 268 | if ( $prd->has_child() && $prd->is_type( 'variable' ) ) { |
| 269 | |
| 270 | $product_children = $prd->get_children(); |
| 271 | $variation_arr = []; |
| 272 | |
| 273 | if ( count( $product_children ) ) { |
| 274 | foreach ( $product_children as $product_child ) { |
| 275 | $child_wc = wc_get_product( $product_child ); |
| 276 | $product_type = $child_wc->get_type(); |
| 277 | $product_prefix = ' (' . $product_type . ') '; |
| 278 | $variation_any = false; |
| 279 | $get_atts = $child_wc->get_variation_attributes(); |
| 280 | |
| 281 | if ( $child_wc->get_manage_stock() ) { |
| 282 | if ( ! $child_wc->is_in_stock() ) { |
| 283 | continue; |
| 284 | } else { |
| 285 | $child_wc_stock = ''; |
| 286 | } |
| 287 | } else { |
| 288 | $child_wc_stock = ''; |
| 289 | }; |
| 290 | |
| 291 | if ( ! $child_wc->is_in_stock() ) { |
| 292 | continue; |
| 293 | } else { |
| 294 | $child_wc_stock = ''; |
| 295 | } |
| 296 | foreach ( $get_atts as $attr_k => $attr_v ) { |
| 297 | if ( empty( $attr_v ) ) { |
| 298 | $variation_any = true; |
| 299 | } |
| 300 | } |
| 301 | if ( $variation_any ) { |
| 302 | $attr_name = ''; |
| 303 | $any_variations = VI_WOO_BOPO_BUNDLE_Helper::bopobb_get_variations( $prd, $product_child, $get_atts, $variation_arr ); |
| 304 | |
| 305 | if ( ! empty( $any_variations ) ) { |
| 306 | $any_to_all = VI_WOO_BOPO_BUNDLE_Helper::bopobb_set_array( $any_variations, $product_child ); |
| 307 | $variation_arr = array_merge( $variation_arr, $any_to_all ); |
| 308 | foreach ( $any_variations as $any_v ) { |
| 309 | $key_arr = array_keys( $get_atts ); |
| 310 | if ( is_array( $any_v ) || is_object( $any_v ) ) { |
| 311 | if ( count( $key_arr ) == count( $any_v ) ) { |
| 312 | $set_arr = array_combine( $key_arr, $any_v ); |
| 313 | $product_variation = array( |
| 314 | 'id' => $product_child . '/' . VI_WOO_BOPO_BUNDLE_Helper::bopobb_build_variations( $set_arr ), |
| 315 | 'text' => '#' . $product_child . $product_prefix . get_the_title() . VI_WOO_BOPO_BUNDLE_Helper::bopobb_build_title( $set_arr ) . $child_wc_stock |
| 316 | ); |
| 317 | } |
| 318 | } else { |
| 319 | $any_v = [ $any_v ]; |
| 320 | if ( count( $key_arr ) == count( $any_v ) ) { |
| 321 | $set_arr = array_combine( $key_arr, $any_v ); |
| 322 | $product_variation = array( |
| 323 | 'id' => $product_child . '/' . VI_WOO_BOPO_BUNDLE_Helper::bopobb_build_variations( $set_arr ), |
| 324 | 'text' => '#' . $product_child . $product_prefix . get_the_title() . VI_WOO_BOPO_BUNDLE_Helper::bopobb_build_title( $set_arr ) . $child_wc_stock |
| 325 | ); |
| 326 | } |
| 327 | } |
| 328 | $found_products[] = $product_variation; |
| 329 | } |
| 330 | } |
| 331 | } else { |
| 332 | $attr_name = ''; |
| 333 | $achieve_arr = []; |
| 334 | $achieve_arr['id'] = $product_child; |
| 335 | $is_variation_valid = VI_WOO_BOPO_BUNDLE_Helper::bopobb_is_variation_allow( $prd, $product_child, $get_atts, $variation_arr ); |
| 336 | if ( ! $is_variation_valid ) { |
| 337 | continue; |
| 338 | } |
| 339 | foreach ( $get_atts as $att_k => $att_v ) { |
| 340 | $cur_key = substr( $att_k, 10 ); |
| 341 | $cur_term = get_term_by( 'slug', $att_v, $cur_key ); |
| 342 | $achieve_arr[] = $att_v; |
| 343 | if ( ! empty( $cur_term ) ) { |
| 344 | $attr_name .= ' - ' . $cur_term->name; |
| 345 | } |
| 346 | } |
| 347 | $variation_arr = array_merge( $variation_arr, [ $achieve_arr ] ); |
| 348 | $product_variation = array( |
| 349 | 'id' => $product_child . '/' . VI_WOO_BOPO_BUNDLE_Helper::bopobb_build_variations( $get_atts ), |
| 350 | 'text' => '#' . $product_child . $product_prefix . get_the_title() . $attr_name . $child_wc_stock |
| 351 | ); |
| 352 | $found_products[] = $product_variation; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | continue; |
| 357 | } |
| 358 | $product_id = get_the_ID(); |
| 359 | $product_title = get_the_title(); |
| 360 | $the_product = wc_get_product( $product_id ); |
| 361 | $product_type = $the_product->get_type(); |
| 362 | $product_prefix = ' (' . $product_type . ') '; |
| 363 | if ( ! $the_product->is_in_stock() ) { |
| 364 | continue; |
| 365 | } |
| 366 | $product_name = array( |
| 367 | 'id' => $product_id, |
| 368 | 'text' => '#' . $product_id . $product_prefix . $product_title |
| 369 | ); |
| 370 | $found_products[] = $product_name; |
| 371 | } |
| 372 | } |
| 373 | wp_send_json( $found_products ); |
| 374 | } |
| 375 | |
| 376 | function bopobb_product_type_selector( $types ) { |
| 377 | $types['bopobb'] = esc_html__( 'Bopo bundle', 'woo-bopo-bundle' ); |
| 378 | |
| 379 | return $types; |
| 380 | } |
| 381 | |
| 382 | function bopobb_product_data_tabs( $tabs ) { |
| 383 | $tabs['bopobb'] = array( |
| 384 | 'label' => esc_html__( 'Bopo Bundle', 'woo-bopo-bundle' ), |
| 385 | 'target' => 'bopobb-settings', |
| 386 | 'class' => array( 'show_if_bopobb' ), |
| 387 | ); |
| 388 | |
| 389 | return $tabs; |
| 390 | } |
| 391 | |
| 392 | function bopobb_product_filters( $filters ) { |
| 393 | $filters = str_replace( 'Bopobb', esc_html__( 'Bopo bundle', 'woo-bopo-bundle' ), $filters ); |
| 394 | |
| 395 | return $filters; |
| 396 | } |
| 397 | |
| 398 | function bopobb_product_data_panels() { |
| 399 | global $post; |
| 400 | $post_id = $post->ID; |
| 401 | $ids = ''; |
| 402 | $meta_count = 0; |
| 403 | $meta_title = ''; |
| 404 | $meta_shipping = ''; |
| 405 | $meta_items = []; |
| 406 | |
| 407 | if ( get_post_meta( $post_id, 'bopobb_title', true ) ) { |
| 408 | $meta_title = get_post_meta( $post_id, 'bopobb_title', true ); |
| 409 | } |
| 410 | if ( get_post_meta( $post_id, 'bopobb_shipping_fee', true ) ) { |
| 411 | $meta_shipping = get_post_meta( $post_id, 'bopobb_shipping_fee', true ); |
| 412 | } |
| 413 | |
| 414 | if ( get_post_meta( $post_id, 'bopobb_count', true ) ) { |
| 415 | $meta_count = get_post_meta( $post_id, 'bopobb_count', true ); |
| 416 | for ( $index = 0; $index < $meta_count; $index ++ ) { |
| 417 | if ( get_post_meta( $post_id, 'bopobb_item_' . $index, true ) ) { |
| 418 | $meta_item = get_post_meta( $post_id, 'bopobb_item_' . $index, true ); |
| 419 | array_push( $meta_items, $meta_item ); |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | ?> |
| 424 | <div id='bopobb-settings' class='panel woocommerce_options_panel wc-metaboxes-wrapper bopobb-product-panel'> |
| 425 | <?php wp_nonce_field( 'bopobb_product_nonce', 'bopobb_product_nonce' ); ?> |
| 426 | <div class="bopobb-bundle-settings"> |
| 427 | <div class="bopobb-title-contain"> |
| 428 | <div class="bopobb-title-label"> |
| 429 | <?php |
| 430 | esc_html_e( 'Bundle title:', 'woo-bopo-bundle' ); |
| 431 | ?> |
| 432 | </div> |
| 433 | <div class="bopobb-title"> |
| 434 | <input aria-label="<?php esc_attr_e( 'Custom title', 'woo-bopo-bundle' ); ?>" type="text" |
| 435 | id="bopobb_title" name="bopobb_title" |
| 436 | placeholder="<?php esc_attr_e( 'A bundle by bopo', 'woo-bopo-bundle' ); ?>" |
| 437 | value="<?php echo esc_attr( $meta_title ); ?>"> |
| 438 | </div> |
| 439 | <input type="text" id="bopobb_count" name="bopobb_count" value="2" readonly |
| 440 | placeholder="Add item"> |
| 441 | </div> |
| 442 | <div class="bopobb-shipping-contain"> |
| 443 | <div class="bopobb-shipping-label"> |
| 444 | <?php |
| 445 | esc_html_e( 'Shipping fee:', 'woo-bopo-bundle' ); |
| 446 | ?> |
| 447 | </div> |
| 448 | <div class="bopobb-shipping"> |
| 449 | <select class="bopobb-shipping-fee" id="bopobb_shipping_fee" name="bopobb_shipping_fee"> |
| 450 | <option <?php if ( ! empty( $meta_shipping ) && $meta_shipping == 'each' ) |
| 451 | echo esc_attr( 'selected' ) ?> |
| 452 | value="each"><?php esc_html_e( 'Apply to each bundled product', 'woo-bopo-bundle' ) ?></option> |
| 453 | <option <?php if ( ! empty( $meta_shipping ) && $meta_shipping == 'both' ) |
| 454 | echo esc_attr( 'selected' ) ?> |
| 455 | value="both"><?php esc_html_e( 'Apply to the whole bundle', 'woo-bopo-bundle' ) ?></option> |
| 456 | </select> |
| 457 | </div> |
| 458 | <input type="text" id="bopobb_count" name="bopobb_count" value="2" readonly |
| 459 | placeholder="Add item"> |
| 460 | </div> |
| 461 | <div class="bopobb-pbi-contain wc-metaboxes ui-sortable"> |
| 462 | <?php |
| 463 | if ( $meta_count !== 0 ) { |
| 464 | $this->bopobb_load_item( $meta_items, $meta_count ); |
| 465 | } else { |
| 466 | $this->bopobb_load_item( '' ); |
| 467 | } |
| 468 | ?> |
| 469 | </div> |
| 470 | </div> |
| 471 | </div> |
| 472 | <?php |
| 473 | } |
| 474 | |
| 475 | function bopobb_load_item( $data = '', $count = 2 ) { |
| 476 | for ( $index = 0; $index < $count; $index ++ ) { |
| 477 | ?> |
| 478 | <div class="bopobb-pbi wc-metabox closed ui-sortable-handle"> |
| 479 | <h3 class="bopobb-pbi-anchor"> |
| 480 | <strong class="bopobb-pbi-item-title"><?php echo sprintf( "%1s %2s", |
| 481 | esc_html__( 'Bundle item', 'woo-bopo-bundle' ), esc_html( $index + 1 ) ); ?></strong> |
| 482 | <div class="bopobb-pbi-slide-arrow dashicons dashicons-arrow-down-alt2" |
| 483 | title="Click to toggle"></div> |
| 484 | <input type="text" class="bopobb-pbi-index" id="bopobb_index_<?php echo esc_attr( $index ) ?>" |
| 485 | name="bopobb_index_<?php echo esc_attr( $index ) ?>" |
| 486 | value="<?php echo esc_attr( $index ) ?>" placeholder="Item index"> |
| 487 | </h3> |
| 488 | <div class="bopobb-pbi-data wc-metabox-content" style=""> |
| 489 | <table> |
| 490 | <tbody> |
| 491 | <tr class="bopobb-pbi-tab-label"> |
| 492 | <th><?php esc_html_e( 'Rule', 'woo-bopo-bundle' ); ?></th> |
| 493 | <td></td> |
| 494 | </tr> |
| 495 | <tr class=""> |
| 496 | <th><?php esc_html_e( 'Categories', 'woo-bopo-bundle' ); ?></th> |
| 497 | <td> |
| 498 | <div class="bopobb-pbi-input"> |
| 499 | <select class="bopobb-pbi-category bopobb-pbi-category-search" |
| 500 | multiple="multiple" |
| 501 | name="bopobb_pbi_category_<?php echo esc_attr( $index ) ?>[]" |
| 502 | id="bopobb_pbi_category_<?php echo esc_attr( $index ) ?>" |
| 503 | data-placeholder="<?php esc_html_e( 'Please Fill In Your Category', 'woo-bopo-bundle' ) ?>"> |
| 504 | <?php |
| 505 | if ( ! empty( $data ) && ! empty( $data[ $index ]['bopobb_pbi_category'] ) ) { |
| 506 | $cat_ids = $data[ $index ]['bopobb_pbi_category']; |
| 507 | if ( count( $cat_ids ) ) { |
| 508 | foreach ( $cat_ids as $ps ) { |
| 509 | $term = get_term_by( 'id', $ps, 'product_cat', 'ARRAY_A' ); |
| 510 | if ( $term ) { |
| 511 | ?> |
| 512 | <option selected |
| 513 | value="<?php echo esc_attr( $ps ) ?>"><?php echo esc_attr( $term['name'] ) ?></option> |
| 514 | <?php |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | ?> |
| 520 | </select> |
| 521 | </div> |
| 522 | <span class="woocommerce-help-tip" data-tip="<?php |
| 523 | esc_attr_e( 'Product with categories will be applied to this bundle item.', 'woo-bopo-bundle' ) ?>"> |
| 524 | </span> |
| 525 | </td> |
| 526 | </tr> |
| 527 | <tr class=""> |
| 528 | <th><?php esc_html_e( 'Exclude categories', 'woo-bopo-bundle' ); ?></th> |
| 529 | <td> |
| 530 | <div class="bopobb-pbi-input"> |
| 531 | <select class="bopobb-pbi-category-exclude bopobb-pbi-category-search" |
| 532 | multiple="multiple" |
| 533 | name="bopobb_pbi_category_exclude_<?php echo esc_attr( $index ) ?>[]" |
| 534 | id="bopobb_pbi_category_exclude_<?php echo esc_attr( $index ) ?>" |
| 535 | data-placeholder="<?php esc_html_e( 'Please Fill In Your Category', 'woo-bopo-bundle' ) ?>"> |
| 536 | <?php |
| 537 | if ( ! empty( $data ) && ! empty( $data[ $index ]['bopobb_pbi_category_exclude'] ) ) { |
| 538 | $cat_ids = $data[ $index ]['bopobb_pbi_category_exclude']; |
| 539 | if ( count( $cat_ids ) ) { |
| 540 | foreach ( $cat_ids as $ps ) { |
| 541 | $term = get_term_by( 'id', $ps, 'product_cat', 'ARRAY_A' ); |
| 542 | if ( $term ) { |
| 543 | ?> |
| 544 | <option selected |
| 545 | value="<?php echo esc_attr( $ps ) ?>"><?php echo esc_attr( $term['name'] ) ?></option> |
| 546 | <?php |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | ?> |
| 552 | </select> |
| 553 | </div> |
| 554 | <span class="woocommerce-help-tip" data-tip="<?php |
| 555 | esc_attr_e( 'Product with categories will not be applied to this bundle item.', 'woo-bopo-bundle' ) ?>"> |
| 556 | </span> |
| 557 | </td> |
| 558 | </tr> |
| 559 | <tr class=""> |
| 560 | <th><?php esc_html_e( 'Tags', 'woo-bopo-bundle' ); ?></th> |
| 561 | <td> |
| 562 | <div class="bopobb-pbi-input"> |
| 563 | <select class="bopobb-pbi-tag bopobb-pbi-tag-search" multiple="multiple" |
| 564 | name="bopobb_pbi_tag_<?php echo esc_attr( $index ) ?>[]" |
| 565 | id="bopobb_pbi_tag_<?php echo esc_attr( $index ) ?>" |
| 566 | data-placeholder="<?php esc_html_e( 'Please Fill In Your Tag', 'woo-bopo-bundle' ) ?>"> |
| 567 | <?php |
| 568 | if ( ! empty( $data ) && ! empty( $data[ $index ]['bopobb_pbi_tag'] ) ) { |
| 569 | $tag_ids = $data[ $index ]['bopobb_pbi_tag']; |
| 570 | if ( count( $tag_ids ) ) { |
| 571 | foreach ( $tag_ids as $ps ) { |
| 572 | $term = get_term_by( 'id', $ps, 'product_tag', 'ARRAY_A' ); |
| 573 | if ( $term ) { |
| 574 | ?> |
| 575 | <option selected |
| 576 | value="<?php echo esc_attr( $ps ) ?>"><?php echo esc_attr( $term['name'] ) ?></option> |
| 577 | <?php |
| 578 | } |
| 579 | } |
| 580 | } |
| 581 | } |
| 582 | ?> |
| 583 | </select> |
| 584 | </div> |
| 585 | <span class="woocommerce-help-tip" data-tip="<?php |
| 586 | esc_attr_e( 'Product with tags will be applied to this bundle item.', 'woo-bopo-bundle' ) ?>"> |
| 587 | </span> |
| 588 | </td> |
| 589 | </tr> |
| 590 | <tr class=""> |
| 591 | <th><?php esc_html_e( 'Exclude tags', 'woo-bopo-bundle' ); ?></th> |
| 592 | <td> |
| 593 | <div class="bopobb-pbi-input"> |
| 594 | <select class="bopobb-pbi-tag-exclude bopobb-pbi-tag-search" multiple="multiple" |
| 595 | name="bopobb_pbi_tag_exclude_<?php echo esc_attr( $index ) ?>[]" |
| 596 | id="bopobb_pbi_tag_exclude_<?php echo esc_attr( $index ) ?>" |
| 597 | data-placeholder="<?php esc_html_e( 'Please Fill In Your Tag', 'woo-bopo-bundle' ) ?>"> |
| 598 | <?php |
| 599 | if ( ! empty( $data ) && ! empty( $data[ $index ]['bopobb_pbi_tag_exclude'] ) ) { |
| 600 | $tag_ids = $data[ $index ]['bopobb_pbi_tag_exclude']; |
| 601 | if ( count( $tag_ids ) ) { |
| 602 | foreach ( $tag_ids as $ps ) { |
| 603 | $term = get_term_by( 'id', $ps, 'product_tag', 'ARRAY_A' ); |
| 604 | if ( $term ) { |
| 605 | ?> |
| 606 | <option selected |
| 607 | value="<?php echo esc_attr( $ps ) ?>"><?php echo esc_attr( $term['name'] ) ?></option> |
| 608 | <?php |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 | ?> |
| 614 | </select> |
| 615 | </div> |
| 616 | <span class="woocommerce-help-tip" data-tip="<?php |
| 617 | esc_attr_e( 'Product with tags will not be applied to this bundle item.', 'woo-bopo-bundle' ) ?>"> |
| 618 | </span> |
| 619 | </td> |
| 620 | </tr> |
| 621 | <tr class=""> |
| 622 | <th><?php esc_html_e( 'Product', 'woo-bopo-bundle' ); ?></th> |
| 623 | <td> |
| 624 | <div class="bopobb-pbi-input"> |
| 625 | <select class="bopobb-pbi-title bopobb-pbi-product-search" multiple="multiple" |
| 626 | name="bopobb_pbi_title_<?php echo esc_attr( $index ) ?>[]" |
| 627 | id="bopobb_pbi_title_<?php echo esc_attr( $index ) ?>" |
| 628 | data-placeholder="<?php esc_html_e( 'Please Fill In Your Product Title', 'woo-bopo-bundle' ) ?>"> |
| 629 | <?php |
| 630 | if ( ! empty( $data ) && ! empty( $data[ $index ]['bopobb_pbi_title'] ) ) { |
| 631 | $product_ids = $data[ $index ]['bopobb_pbi_title']; |
| 632 | if ( count( $product_ids ) ) { |
| 633 | foreach ( $product_ids as $ps ) { |
| 634 | $product_in = wc_get_product( $ps ); |
| 635 | // if ( ! $product_in->is_purchasable() || 'publish' !== $product_in->get_status() ) { |
| 636 | // continue; |
| 637 | // } |
| 638 | $product_prefix = ' (' . $product_in->get_type() . ') '; |
| 639 | $product_in_title = '#' . $ps . $product_prefix . $product_in->get_title(); |
| 640 | if ( $product_in ) { |
| 641 | ?> |
| 642 | <option selected |
| 643 | value="<?php echo esc_attr( $ps ) ?>"><?php echo esc_attr( $product_in_title ) ?></option> |
| 644 | <?php |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | } |
| 649 | ?> |
| 650 | </select> |
| 651 | </div> |
| 652 | <span class="woocommerce-help-tip" data-tip="<?php |
| 653 | esc_attr_e( 'Product with title will be applied to this bundle item.', 'woo-bopo-bundle' ) ?>"> |
| 654 | </span> |
| 655 | </td> |
| 656 | </tr> |
| 657 | <tr class=""> |
| 658 | <th><?php esc_html_e( 'Exclude product', 'woo-bopo-bundle' ); ?></th> |
| 659 | <td> |
| 660 | <div class="bopobb-pbi-input"> |
| 661 | <select class="bopobb-pbi-title-exclude bopobb-pbi-product-search" |
| 662 | multiple="multiple" |
| 663 | name="bopobb_pbi_title_exclude_<?php echo esc_attr( $index ) ?>[]" |
| 664 | id="bopobb_pbi_title_exclude_<?php echo esc_attr( $index ) ?>" |
| 665 | data-placeholder="<?php esc_html_e( 'Please Fill In Your Product Title', 'woo-bopo-bundle' ) ?>"> |
| 666 | <?php |
| 667 | if ( ! empty( $data ) && ! empty( $data[ $index ]['bopobb_pbi_title_exclude'] ) ) { |
| 668 | $product_ids = $data[ $index ]['bopobb_pbi_title_exclude']; |
| 669 | if ( count( $product_ids ) ) { |
| 670 | foreach ( $product_ids as $ps ) { |
| 671 | $product_ex = wc_get_product( $ps ); |
| 672 | if ( ! $product_ex->is_purchasable() || 'publish' !== $product_ex->get_status() ) { |
| 673 | continue; |
| 674 | } |
| 675 | $product_prefix = ' (' . $product_ex->get_type() . ') '; |
| 676 | $product_ex_title = '#' . $ps . $product_prefix . $product_ex->get_title(); |
| 677 | if ( $product_ex ) { |
| 678 | ?> |
| 679 | <option selected |
| 680 | value="<?php echo esc_attr( $ps ) ?>"><?php echo esc_attr( $product_ex_title ) ?></option> |
| 681 | <?php |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | ?> |
| 687 | </select> |
| 688 | </div> |
| 689 | <span class="woocommerce-help-tip" data-tip="<?php |
| 690 | esc_attr_e( 'Product with title will not be applied to this bundle item.', 'woo-bopo-bundle' ) ?>"> |
| 691 | </span> |
| 692 | </td> |
| 693 | </tr> |
| 694 | <tr class="bopobb-pbi-tab-label"> |
| 695 | <th><?php esc_html_e( 'Display', 'woo-bopo-bundle' ); ?></th> |
| 696 | <td></td> |
| 697 | </tr> |
| 698 | <tr class=""> |
| 699 | <th><?php esc_html_e( 'Sort', 'woo-bopo-bundle' ); ?></th> |
| 700 | <td> |
| 701 | <div class="bopobb-pbi-input"> |
| 702 | <select class="vi-ui fluid dropdown bopo-pbi-input-part" |
| 703 | id="bopobb_bpi_sort_<?php echo esc_attr( $index ) ?>" |
| 704 | name="bopobb_bpi_sort_<?php echo esc_attr( $index ) ?>"> |
| 705 | <option <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_sort'] == 'price' ) |
| 706 | echo esc_attr( 'selected' ) ?> |
| 707 | value="price"><?php esc_html_e( 'Price', 'woo-bopo-bundle' ) ?></option> |
| 708 | <option <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_sort'] == 'title' ) |
| 709 | echo esc_attr( 'selected' ) ?> |
| 710 | value="title"><?php esc_html_e( 'Title', 'woo-bopo-bundle' ) ?></option> |
| 711 | <option <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_sort'] == 'ratting' ) |
| 712 | echo esc_attr( 'selected' ) ?> |
| 713 | value="ratting"><?php esc_html_e( 'Rating', 'woo-bopo-bundle' ) ?></option> |
| 714 | </select> |
| 715 | <select class="vi-ui fluid dropdown bopo-pbi-input-part" |
| 716 | id="bopobb_bpi_order_<?php echo esc_attr( $index ) ?>" |
| 717 | name="bopobb_bpi_order_<?php echo esc_attr( $index ) ?>"> |
| 718 | <option <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_order'] == 'ASC' ) |
| 719 | echo esc_attr( 'selected' ) ?> |
| 720 | value="ASC"><?php esc_html_e( 'ASC', 'woo-bopo-bundle' ) ?></option> |
| 721 | <option <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_order'] == 'DESC' ) |
| 722 | echo esc_attr( 'selected' ) ?> |
| 723 | value="DESC"><?php esc_html_e( 'DESC', 'woo-bopo-bundle' ) ?></option> |
| 724 | </select> |
| 725 | </div> |
| 726 | <span class="woocommerce-help-tip" data-tip="<?php |
| 727 | esc_attr_e( 'This field allows you to set the order of product in bundle select popup', 'woo-bopo-bundle' ) ?>"> |
| 728 | </span> |
| 729 | </td> |
| 730 | </tr> |
| 731 | <tr class=""> |
| 732 | <th><?php esc_html_e( 'Default', 'woo-bopo-bundle' ); ?></th> |
| 733 | <td> |
| 734 | <div class="bopobb-pbi-input"> |
| 735 | <div class="bopo-pbi-input-checkbox"> |
| 736 | <input class="checkbox" |
| 737 | id="bopobb_bpi_set_default_<?php echo esc_attr( $index ) ?>" |
| 738 | name="bopobb_bpi_set_default_<?php echo esc_attr( $index ) ?>" |
| 739 | type="checkbox" <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_set_default'] ) |
| 740 | echo esc_attr( 'checked=checked' ) ?>> |
| 741 | <span class="woocommerce-help-tip" data-tip="<?php |
| 742 | esc_attr_e( 'Check this field to set the default product of current item', 'woo-bopo-bundle' ) ?>"> |
| 743 | </span> |
| 744 | </div> |
| 745 | <div class="bopobb-bpi-default-product-wrap"> |
| 746 | <select class="bopobb-bpi-default-product bopobb-pbi-default-search" |
| 747 | id="bopobb_bpi_default_product_<?php echo esc_attr( $index ) ?>" |
| 748 | name="bopobb_bpi_default_product_<?php echo esc_attr( $index ) ?>" |
| 749 | data-placeholder="<?php esc_html_e( 'Please Fill In Your Title', 'woo-bopo-bundle' ) ?>"> |
| 750 | <?php |
| 751 | if ( ! empty( $data ) && ! empty( $data[ $index ]['bopobb_bpi_default_product'] ) ) { |
| 752 | $variation_title = ''; |
| 753 | $id_array = explode( '/', $data[ $index ]['bopobb_bpi_default_product'] ); |
| 754 | if ( count( $id_array ) > 1 ) { |
| 755 | $variation_title .= VI_WOO_BOPO_BUNDLE_Helper::bopobb_decode_variations( $id_array[1], 1 ); |
| 756 | } |
| 757 | $product_def = wc_get_product( $id_array[0] ); |
| 758 | $product_prefix = ' (' . $product_def->get_type() . ') '; |
| 759 | if ( ! $product_def->is_purchasable() || 'publish' !== $product_def->get_status() ) { |
| 760 | $product_def = ''; |
| 761 | } else { |
| 762 | $product_title = '#' . $id_array[0] . $product_prefix . $product_def->get_title() . $variation_title; |
| 763 | } |
| 764 | if ( $product_def ) { |
| 765 | ?> |
| 766 | <option selected |
| 767 | value="<?php echo esc_attr( $data[ $index ]['bopobb_bpi_default_product'] ) ?>"><?php echo esc_attr( $product_title ) ?></option> |
| 768 | <?php |
| 769 | } |
| 770 | } |
| 771 | ?> |
| 772 | </select> |
| 773 | </div> |
| 774 | </div> |
| 775 | <span class="woocommerce-help-tip" data-tip="<?php |
| 776 | esc_attr_e( 'Search product to set the default product of current item, if this item not set and checkbox checked, default is the first product in list', 'woo-bopo-bundle' ) ?>"> |
| 777 | </span> |
| 778 | </td> |
| 779 | </tr> |
| 780 | <tr class=""> |
| 781 | <th><?php esc_html_e( 'Quantity', 'woo-bopo-bundle' ); ?></th> |
| 782 | <td> |
| 783 | <div class="bopobb-pbi-input"> |
| 784 | <input type="number" id="bopobb_bpi_quantity_<?php echo esc_attr( $index ) ?>" |
| 785 | min="1" |
| 786 | name="bopobb_bpi_quantity_<?php echo esc_attr( $index ) ?>" |
| 787 | value="<?php echo esc_attr( isset( $data[ $index ]['bopobb_bpi_quantity'] ) ? $data[ $index ]['bopobb_bpi_quantity'] : 1 ) ?>"> |
| 788 | </div> |
| 789 | <span class="woocommerce-help-tip" data-tip="<?php |
| 790 | esc_attr_e( 'This field allows you to set the quantity of product to current item', 'woo-bopo-bundle' ) ?>"> |
| 791 | </span> |
| 792 | </td> |
| 793 | </tr> |
| 794 | <tr class="bopobb-pbi-tab-label"> |
| 795 | <th><?php esc_html_e( 'Discount', 'woo-bopo-bundle' ); ?></th> |
| 796 | <td> |
| 797 | <div class="bopobb-pbi-input"> |
| 798 | <select class="vi-ui fluid dropdown bopo-pbi-input-part" |
| 799 | id="bopobb_bpi_discount_<?php echo esc_attr( $index ) ?>" |
| 800 | name="bopobb_bpi_discount_<?php echo esc_attr( $index ) ?>"> |
| 801 | <option <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_discount'] == '0' ) |
| 802 | echo esc_attr( 'selected' ) ?> |
| 803 | value="0"><?php esc_html_e( 'Percent of total (%)', 'woo-bopo-bundle' ) ?></option> |
| 804 | <option <?php if ( ! empty( $data ) && $data[ $index ]['bopobb_bpi_discount'] == '1' ) |
| 805 | echo esc_attr( 'selected' ) ?> |
| 806 | value="1"><?php echo sprintf( "%1s (%2s)", |
| 807 | esc_html__( 'Fixed price', 'woo-bopo-bundle' ), |
| 808 | esc_html( get_woocommerce_currency_symbol() ) ); ?></option> |
| 809 | </select> |
| 810 | <input type="number" class="bopo-pbi-input-part" |
| 811 | id="bopobb_bpi_discount_number_<?php echo esc_attr( $index ) ?>" min="0" |
| 812 | value="<?php if ( ! empty( $data ) && isset( $data[ $index ]['bopobb_bpi_discount_number'] ) ) |
| 813 | echo esc_attr( $data[ $index ]['bopobb_bpi_discount_number'] ) ?>" |
| 814 | name="bopobb_bpi_discount_number_<?php echo esc_attr( $index ) ?>"> |
| 815 | </div> |
| 816 | <span class="woocommerce-help-tip" data-tip="<?php |
| 817 | esc_attr_e( 'This field allows you to set the discount of current item', 'woo-bopo-bundle' ) ?>"> |
| 818 | </span> |
| 819 | </td> |
| 820 | </tr> |
| 821 | </tbody> |
| 822 | </table> |
| 823 | </div> |
| 824 | </div> |
| 825 | <?php |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | function bopobb_save_option_fields( $post_id ) { |
| 830 | if ( ! current_user_can( "edit_post", $post_id ) ) { |
| 831 | return $post_id; |
| 832 | } |
| 833 | if ( ! isset( $_REQUEST['bopobb_product_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['bopobb_product_nonce'] ) ), 'bopobb_product_nonce' ) ) { |
| 834 | return $post_id; |
| 835 | } |
| 836 | if ( isset( $_POST['product-type'] ) && ( $_POST['product-type'] == 'bopobb' ) ) { |
| 837 | if ( isset( $_POST['bopobb_count'] ) && ! empty( $_POST['bopobb_count'] ) ) { |
| 838 | update_post_meta( $post_id, 'bopobb_count', ( absint( $_POST['bopobb_count'] ) ) ); |
| 839 | if ( isset( $_POST['bopobb_title'] ) && ! empty( $_POST['bopobb_title'] ) ) { |
| 840 | update_post_meta( $post_id, 'bopobb_title', ( sanitize_text_field( wp_unslash( $_POST['bopobb_title'] ) ) ) ); |
| 841 | } else { |
| 842 | update_post_meta( $post_id, 'bopobb_title', '' ); |
| 843 | } |
| 844 | if ( isset( $_POST['bopobb_shipping_fee'] ) && ! empty( $_POST['bopobb_shipping_fee'] ) ) { |
| 845 | update_post_meta( $post_id, 'bopobb_shipping_fee', ( sanitize_text_field( wp_unslash( $_POST['bopobb_shipping_fee'] ) ) ) ); |
| 846 | } |
| 847 | for ( $i = 0; $i < absint( $_POST['bopobb_count'] ); $i ++ ) { |
| 848 | $item_bundle = []; |
| 849 | if ( isset( $_POST[ 'bopobb_index_' . $i ] ) ) { |
| 850 | $item_bundle['bopobb_index'] = absint( $_POST[ 'bopobb_index_' . $i ] ); |
| 851 | } |
| 852 | if ( isset( $_POST[ 'bopobb_pbi_category_' . $i ] ) && ! empty( $_POST[ 'bopobb_pbi_category_' . $i ] ) ) { |
| 853 | $item_bundle['bopobb_pbi_category'] = array_map( 'esc_attr', wc_clean( wp_unslash( $_POST[ 'bopobb_pbi_category_' . $i ] ) ) ); |
| 854 | } |
| 855 | if ( isset( $_POST[ 'bopobb_pbi_category_exclude_' . $i ] ) && ! empty( $_POST[ 'bopobb_pbi_category_exclude_' . $i ] ) ) { |
| 856 | $item_bundle['bopobb_pbi_category_exclude'] = array_map( 'esc_attr', wc_clean( wp_unslash( $_POST[ 'bopobb_pbi_category_exclude_' . $i ] ) ) ); |
| 857 | } |
| 858 | if ( isset( $_POST[ 'bopobb_pbi_tag_' . $i ] ) && ! empty( $_POST[ 'bopobb_pbi_tag_' . $i ] ) ) { |
| 859 | $item_bundle['bopobb_pbi_tag'] = array_map( 'esc_attr', wc_clean( wp_unslash( $_POST[ 'bopobb_pbi_tag_' . $i ] ) ) ); |
| 860 | } |
| 861 | if ( isset( $_POST[ 'bopobb_pbi_tag_exclude_' . $i ] ) && ! empty( $_POST[ 'bopobb_pbi_tag_exclude_' . $i ] ) ) { |
| 862 | $item_bundle['bopobb_pbi_tag_exclude'] = array_map( 'esc_attr', wc_clean( wp_unslash( $_POST[ 'bopobb_pbi_tag_exclude_' . $i ] ) ) ); |
| 863 | } |
| 864 | if ( isset( $_POST[ 'bopobb_pbi_title_' . $i ] ) && ! empty( $_POST[ 'bopobb_pbi_title_' . $i ] ) ) { |
| 865 | $item_bundle['bopobb_pbi_title'] = array_map( 'esc_attr', wc_clean( wp_unslash( $_POST[ 'bopobb_pbi_title_' . $i ] ) ) ); |
| 866 | } |
| 867 | if ( isset( $_POST[ 'bopobb_pbi_title_exclude_' . $i ] ) && ! empty( $_POST[ 'bopobb_pbi_title_exclude_' . $i ] ) ) { |
| 868 | $item_bundle['bopobb_pbi_title_exclude'] = array_map( 'esc_attr', wc_clean( wp_unslash( $_POST[ 'bopobb_pbi_title_exclude_' . $i ] ) ) ); |
| 869 | } |
| 870 | if ( isset( $_POST[ 'bopobb_bpi_sort_' . $i ] ) && ! empty( $_POST[ 'bopobb_bpi_sort_' . $i ] ) ) { |
| 871 | $item_bundle['bopobb_bpi_sort'] = sanitize_text_field( wp_unslash( $_POST[ 'bopobb_bpi_sort_' . $i ] ) ); |
| 872 | } |
| 873 | if ( isset( $_POST[ 'bopobb_bpi_order_' . $i ] ) && ! empty( $_POST[ 'bopobb_bpi_order_' . $i ] ) ) { |
| 874 | $item_bundle['bopobb_bpi_order'] = sanitize_text_field( wp_unslash( $_POST[ 'bopobb_bpi_order_' . $i ] ) ); |
| 875 | } |
| 876 | if ( isset( $_POST[ 'bopobb_bpi_set_default_' . $i ] ) ) { |
| 877 | $item_bundle['bopobb_bpi_set_default'] = 1; |
| 878 | } else { |
| 879 | $item_bundle['bopobb_bpi_set_default'] = 0; |
| 880 | } |
| 881 | if ( isset( $_POST[ 'bopobb_bpi_default_product_' . $i ] ) && ! empty( $_POST[ 'bopobb_bpi_default_product_' . $i ] ) ) { |
| 882 | $item_bundle['bopobb_bpi_default_product'] = sanitize_text_field( wp_unslash( $_POST[ 'bopobb_bpi_default_product_' . $i ] ) ); |
| 883 | } |
| 884 | if ( isset( $_POST[ 'bopobb_bpi_quantity_' . $i ] ) && ! empty( $_POST[ 'bopobb_bpi_quantity_' . $i ] ) ) { |
| 885 | $item_bundle['bopobb_bpi_quantity'] = absint( $_POST[ 'bopobb_bpi_quantity_' . $i ] ); |
| 886 | } else { |
| 887 | $item_bundle['bopobb_bpi_quantity'] = absint( 1 ); |
| 888 | } |
| 889 | if ( isset( $_POST[ 'bopobb_bpi_discount_' . $i ] ) ) { |
| 890 | $item_bundle['bopobb_bpi_discount'] = absint( $_POST[ 'bopobb_bpi_discount_' . $i ] ); |
| 891 | } |
| 892 | if ( isset( $_POST[ 'bopobb_bpi_discount_number_' . $i ] ) && ! empty( $_POST[ 'bopobb_bpi_discount_number_' . $i ] ) ) { |
| 893 | $item_bundle['bopobb_bpi_discount_number'] = floatval( $_POST[ 'bopobb_bpi_discount_number_' . $i ] ); |
| 894 | } else { |
| 895 | $item_bundle['bopobb_bpi_discount_number'] = 0; |
| 896 | } |
| 897 | update_post_meta( $post_id, 'bopobb_item_' . absint( $_POST[ 'bopobb_index_' . $i ] ), ( $item_bundle ) ); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | return $post_id; |
| 903 | } |
| 904 | |
| 905 | function bopobb_delete_option_fields( $post_id ) { |
| 906 | if ( ! isset( $_REQUEST['bopobb_product_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST['bopobb_product_nonce'] ) ), 'bopobb_product_nonce' ) ) { |
| 907 | return; |
| 908 | } |
| 909 | if ( isset( $_POST['product-type'] ) && ( $_POST['product-type'] !== 'bopobb' ) ) { |
| 910 | $ib_count = get_post_meta( $post_id, 'bopobb_count' ); |
| 911 | if ( ! empty( $ib_count ) && isset( $ib_count ) ) { |
| 912 | for ( $i = 0; $i <= intval( $ib_count ); $i ++ ) { |
| 913 | delete_post_meta( $post_id, 'bopobb_item_' . $i ); |
| 914 | } |
| 915 | delete_post_meta( $post_id, 'bopobb_count' ); |
| 916 | delete_post_meta( $post_id, 'bopobb_title' ); |
| 917 | delete_post_meta( $post_id, 'bopobb_shipping_fee' ); |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | public function bopobb_shortcode_after_title_detail_filter_menu() { |
| 923 | global $post; |
| 924 | $post_id = $post->ID; |
| 925 | $_product = wc_get_product( $post_id ); |
| 926 | if ( $post->post_type != 'product' ) { |
| 927 | return; |
| 928 | } |
| 929 | if ( ! $_product->is_type( 'bopobb' ) ) { |
| 930 | return; |
| 931 | } |
| 932 | |
| 933 | ?> |
| 934 | <div class="inside"> |
| 935 | |
| 936 | <div class="bopobb-after-title-shortcode" id="bopobb-after-title-shortcode"> |
| 937 | <div class="vi-ui left labeled icon input fluid"> |
| 938 | <label class="vi-ui label" |
| 939 | for="bopobb_shortcode_show"><?php esc_html_e( 'Shortcode:', 'woo-bopo-bundle' ); ?></label> |
| 940 | <input type="text" id="bopobb_shortcode_show" class="bopobb_shortcode_show" readonly |
| 941 | value="[bopobb_bundle id=<?php echo "'" . esc_attr( $post->ID ) . "'"; ?>]"> |
| 942 | <i class="copy icon"></i> |
| 943 | <span class="bopobb_copy_tooltip" |
| 944 | style=""><?php esc_html_e( 'Copied', 'woo-bopo-bundle' ); ?></span> |
| 945 | </div> |
| 946 | </div> |
| 947 | </div> |
| 948 | <?php |
| 949 | } |
| 950 | |
| 951 | function bopobb_admin_enqueue_scripts() { |
| 952 | $screen = get_current_screen(); |
| 953 | if ( get_post_type() == 'product' && $screen->id == 'product' ) { |
| 954 | wp_enqueue_style( 'dashicons' ); |
| 955 | wp_enqueue_style( 'woo-bopo-bundle-select2-css', VI_WOO_BOPO_BUNDLE_CSS . 'select2.min.css', array(), VI_WOO_BOPO_BUNDLE_VERSION ); |
| 956 | wp_enqueue_script( 'jquery-ui-sortable' ); |
| 957 | if ( WP_DEBUG ) { |
| 958 | wp_enqueue_style( 'woo-bopo-bundle-backend-css', VI_WOO_BOPO_BUNDLE_CSS . 'bopo-settings.css', array(), VI_WOO_BOPO_BUNDLE_VERSION ); |
| 959 | wp_enqueue_script( 'woo-bopo-bundle-product-js', VI_WOO_BOPO_BUNDLE_JS . 'bopo-product.js', array( 'jquery' ), VI_WOO_BOPO_BUNDLE_JS, false ); |
| 960 | } else { |
| 961 | wp_enqueue_style( 'woo-bopo-bundle-backend-css', VI_WOO_BOPO_BUNDLE_CSS . 'bopo-settings.min.css', array(), VI_WOO_BOPO_BUNDLE_VERSION ); |
| 962 | wp_enqueue_script( 'woo-bopo-bundle-product-js', VI_WOO_BOPO_BUNDLE_JS . 'bopo-product.min.js', array( 'jquery' ), VI_WOO_BOPO_BUNDLE_JS, false ); |
| 963 | } |
| 964 | if ( is_plugin_active( 'hub-core/landing-hub-core.php' ) ) { |
| 965 | if ( wp_script_is( 'select2-js' ) ) { |
| 966 | wp_deregister_script( 'select2-js' ); |
| 967 | wp_dequeue_script( 'select2-js' ); |
| 968 | wp_dequeue_style( 'select2-js' ); |
| 969 | } |
| 970 | } |
| 971 | wp_enqueue_script( 'woo-bopo-bundle-select2-js', VI_WOO_BOPO_BUNDLE_JS . 'select2.js', array( 'jquery' ), VI_WOO_BOPO_BUNDLE_VERSION, false ); |
| 972 | wp_localize_script( 'woo-bopo-bundle-product-js', 'bopobbProductVars', array( |
| 973 | 'bopobb_nonce' => wp_create_nonce( 'bopobb_nonce' ), |
| 974 | ) |
| 975 | ); |
| 976 | } |
| 977 | } |
| 978 | |
| 979 | function bopobb_custom_script() { |
| 980 | $screen = get_current_screen(); |
| 981 | |
| 982 | if ( ! $screen ) { |
| 983 | return; |
| 984 | } |
| 985 | switch ( $screen->id ) { |
| 986 | case 'product': |
| 987 | $this->bopobb_create_product_tutorial(); |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | public function bopobb_create_product_tutorial() { |
| 993 | if ( isset( $_GET['product_type'] ) && current_user_can( 'manage_options' ) ) {// phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 994 | $get_p_type = sanitize_text_field( wp_unslash( $_GET['product_type'] ) ); |
| 995 | $get_p_type = $get_p_type != 'bopobb' ? 'other' : $get_p_type; |
| 996 | $script = 'var bopobb_get_type = "' . $get_p_type . '"';// phpcs:ignore WordPress.Security.NonceVerification.Recommended ?> |
| 997 | <script type="text/javascript" data-cfasync="false"> |
| 998 | <?php echo esc_js( $script );// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 999 | </script> |
| 1000 | <?php |
| 1001 | } |
| 1002 | |
| 1003 | return; |
| 1004 | } |
| 1005 | } |
| 1006 | } |