| 1 |
<?php |
| 2 |
/** |
| 3 |
* Update to 1.6.1 |
| 4 |
* Changing POS Only products to use settings rather than postmeta. |
| 5 |
* |
| 6 |
* @package WCPOS\WooCommercePOS |
| 7 |
*/ |
| 8 |
|
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* Change POS Only products to use settings rather than postmeta. |
| 15 |
* NOTE: we will leave the postmeta in place for now, but this will be removed in a future update. |
| 16 |
*/ |
| 17 |
function woocommerce_pos_update_pos_visibility_settings_to_1_6_1() { |
| 18 |
global $wpdb; |
| 19 |
|
| 20 |
$option_key = 'woocommerce_pos_settings_visibility'; |
| 21 |
|
| 22 |
// Check if the option already exists. |
| 23 |
$visibility_settings = get_option( $option_key ); |
| 24 |
|
| 25 |
if ( false !== $visibility_settings ) { |
| 26 |
// The option already exists, no need to update. |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// Initialize arrays for product and variation IDs. |
| 31 |
$pos_only_product_ids = array(); |
| 32 |
$online_only_product_ids = array(); |
| 33 |
$pos_only_variation_ids = array(); |
| 34 |
$online_only_variation_ids = array(); |
| 35 |
|
| 36 |
// Query to get all products with _pos_visibility = 'pos_only'. |
| 37 |
$pos_only_product_ids = $wpdb->get_col( |
| 38 |
" |
| 39 |
SELECT post_id |
| 40 |
FROM {$wpdb->postmeta} |
| 41 |
WHERE meta_key = '_pos_visibility' AND meta_value = 'pos_only' AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product') |
| 42 |
" |
| 43 |
); |
| 44 |
|
| 45 |
// Query to get all products with _pos_visibility = 'online_only'. |
| 46 |
$online_only_product_ids = $wpdb->get_col( |
| 47 |
" |
| 48 |
SELECT post_id |
| 49 |
FROM {$wpdb->postmeta} |
| 50 |
WHERE meta_key = '_pos_visibility' AND meta_value = 'online_only' AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product') |
| 51 |
" |
| 52 |
); |
| 53 |
|
| 54 |
// Query to get all variations with _pos_visibility = 'pos_only'. |
| 55 |
$pos_only_variation_ids = $wpdb->get_col( |
| 56 |
" |
| 57 |
SELECT post_id |
| 58 |
FROM {$wpdb->postmeta} |
| 59 |
WHERE meta_key = '_pos_visibility' AND meta_value = 'pos_only' AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product_variation') |
| 60 |
" |
| 61 |
); |
| 62 |
|
| 63 |
// Query to get all variations with _pos_visibility = 'online_only'. |
| 64 |
$online_only_variation_ids = $wpdb->get_col( |
| 65 |
" |
| 66 |
SELECT post_id |
| 67 |
FROM {$wpdb->postmeta} |
| 68 |
WHERE meta_key = '_pos_visibility' AND meta_value = 'online_only' AND post_id IN (SELECT ID FROM {$wpdb->posts} WHERE post_type = 'product_variation') |
| 69 |
" |
| 70 |
); |
| 71 |
|
| 72 |
// Prepare the new visibility settings. |
| 73 |
$new_visibility_settings = array( |
| 74 |
'products' => array( |
| 75 |
'default' => array( |
| 76 |
'pos_only' => array( |
| 77 |
'ids' => array_map( 'intval', $pos_only_product_ids ), |
| 78 |
), |
| 79 |
'online_only' => array( |
| 80 |
'ids' => array_map( 'intval', $online_only_product_ids ), |
| 81 |
), |
| 82 |
), |
| 83 |
), |
| 84 |
'variations' => array( |
| 85 |
'default' => array( |
| 86 |
'pos_only' => array( |
| 87 |
'ids' => array_map( 'intval', $pos_only_variation_ids ), |
| 88 |
), |
| 89 |
'online_only' => array( |
| 90 |
'ids' => array_map( 'intval', $online_only_variation_ids ), |
| 91 |
), |
| 92 |
), |
| 93 |
), |
| 94 |
); |
| 95 |
|
| 96 |
// Update the option in the database. |
| 97 |
update_option( $option_key, $new_visibility_settings, false ); |
| 98 |
} |
| 99 |
|
| 100 |
// Run the update script. |
| 101 |
woocommerce_pos_update_pos_visibility_settings_to_1_6_1(); |
| 102 |
|