array( 'apikey' => $apiKey ) ); $response = wp_remote_get( $url, $args ); if(wp_remote_retrieve_response_code( $response ) === 200){ $body = wp_remote_retrieve_body($response); } return $body; } function abacus_menu_load_custom_styles(){ if(get_option( 'layoutStyle')=='light') wp_enqueue_style( 'abacus', plugin_dir_url( __FILE__) . 'public/abacus.css', array(), NULL, 'all' ); } function abacus_menu_register_settings() { $args = array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => NULL, ); $argsStyle = array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => 'none', ); register_setting( 'abacusmenu_options_group', 'storeId', $args ); register_setting( 'abacusmenu_options_group', 'apiKey', $args ); register_setting( 'abacusmenu_options_group', 'layoutStyle', $argsStyle ); register_setting('abacusmenu_options_init_group', 'abacusInitialized',''); } function abacus_menu_admin_notice(){ if(get_transient( 'abacus-menu-admin-notice-success-update' )){ echo '

Menu synced successfully.

'; delete_transient( 'abacus-menu-admin-notice-success-update' ); } } function abacus_menu_admin_error(){ if(get_transient( 'abacus-menu-admin-notice-error-update' )){ echo '

Menu syncing failed!

'; delete_transient( 'abacus-menu-admin-notice-error-update' ); } } function abacus_menu_options_page_setting() { add_options_page('WooCommerce Abacus Menu Sync Settings', 'WC Abacus Menu', 'manage_options', 'abacus_menu', 'abacus_menu_options_page_form'); } function abacus_menu_remove_data(){ global $wpdb; $sql_1 = "DELETE FROM ".$wpdb->prefix."term_relationships WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'product');"; $sql_2 = "DELETE FROM ".$wpdb->prefix."postmeta WHERE post_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type = 'product');"; $sql_3 = "DELETE FROM ".$wpdb->prefix."posts WHERE post_type = 'product';"; $sql_4 = "DELETE relations.*, taxes.*, terms.* FROM ".$wpdb->prefix."term_relationships AS relations INNER JOIN ".$wpdb->prefix."term_taxonomy AS taxes ON relations.term_taxonomy_id=taxes.term_taxonomy_id INNER JOIN ".$wpdb->prefix."terms AS terms ON taxes.term_id=terms.term_id WHERE object_id IN (SELECT ID FROM ".$wpdb->prefix."posts WHERE post_type='product');"; try{ $wpdb->query($sql_1); $wpdb->query($sql_2); $wpdb->query($sql_3); $wpdb->query($sql_4); }catch(Exception $e){} $wpdb->query("DELETE a,c FROM wp_terms AS a LEFT JOIN wp_term_taxonomy AS c ON a.term_id = c.term_id LEFT JOIN wp_term_relationships AS b ON b.term_taxonomy_id = c.term_taxonomy_id WHERE c.taxonomy = 'product_cat'"); } function abacus_menu_load_menu() { require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); $user_id = get_current_user_id(); $apiKey= get_option('apiKey'); $storeId=get_option('storeId'); if(current_user_can('manage_options')){ if($apiKey && $storeId){ $url = "http://pouria-eval-test.apigee.net/getstarted?storeID=" . $storeId; $response = abacus_menu_curl_get_contents($url, $apiKey); if($response){ $json= json_decode($response); $menu= $json->{'Menus'}; abacus_menu_remove_data(); foreach($menu as $mnu) { $doc= $mnu->{'Doc'}; $Categories= $doc->{'Categories'}; $Products= $doc->{'Products'}; foreach($Categories as $cat) { $cid= $cat->{'ID'}; $cname=$cat->{'Name'}; $cDescription = $cat->{'Description'}; wp_insert_term( $cname, 'product_cat', array( 'description'=> $cDescription, 'slug' => $cname, 'parent' => "0" ) ); $subItems= $cat->{'SubMenuItems'}; foreach($subItems as $sub) { $productCode=$sub->{'ProductCode'}; $pid=$sub->{'ID'}; $price= $sub->{'Price'}; $productID= $sub->{'ProductID'}; $thumb_url= $Products->{$productID}->{'PhotoUrl'}; $post = array( 'post_author' => $user_id, 'post_status' => "publish", 'post_title' => $sub->{'Name'}, 'post_parent' => '', 'post_type' => "product", ); $post_id = wp_insert_post( $post ); if($post_id){ wp_set_object_terms($post_id, $cname, 'product_cat' ); wp_set_object_terms($post_id, 'simple', 'product_type'); update_post_meta( $post_id, '_price', $sub->{'Price'}); if($thumb_url){ preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $thumb_url, $matches); if(!empty($matches)){ $imagename = 'p_'. $pid . '.' . $matches[1]; $new = $image_path = ABACUS_MENU_FOLDER . '/'. $imagename; if(!file_exists($image_path)){ $tmp_file = download_url( $thumb_url ); if (!is_wp_error($tmp_file)) { $tmppath = pathinfo( $tmp_file ); $new = $tmppath['dirname'] . "/". $imagename; rename($tmp_file, $new); $file_array['tmp_name'] = $new; $file_array['name'] = $imagename; $post_data['post_title'] = $imagename; $thumbid = media_handle_sideload( $file_array, $post_id, 'gallery desc',$post_data ); set_post_thumbnail($post_id, $thumbid); } } } } } } } } return true; }else{ return false; } }else{ return false; } } return false; } function abacus_menu_options_page_form() { ?>

WooCommerce Abacus Menu Sync

Abacus API Server Info:

'abacus-menu-save-settings' ); submit_button( __( 'Save Settings', 'textdomain' ), 'primary', 'abacus-menu-save-settings', true, $other_attributes ); ?>

Data Initialization:

Synchronize the menu to initialize categories and products.

'abacus-menu-sync' ); submit_button( __( 'Sync', 'textdomain' ), 'primary', 'abacus-menu-sync', true, $syc_attributes ); ?>