/** * Sync WooCommerce categories to Square. * * Looks for category names that don't exist in Square, and creates them. */ public function sync_categories() { $wc_category_objects = $this->connect->wc->get_product_categories(); $wc_categories = array(); if (is_wp_error($wc_category_objects) || empty($wc_category_objects['product_categories'])) { return; } foreach ($wc_category_objects['product_categories'] as $wc_category) { if (empty($wc_category['name']) || empty($wc_category['id']) || $wc_category['parent'] !== 0) { continue; } $wc_categories[$wc_category['name']] = $wc_category['id']; } $square_category_objects = $this->connect->get_square_categories(); $square_categories = array(); $processed_categories = array(); foreach ($square_category_objects as $square_category) { // Square list endpoints may return dups so we need to check for that if (in_array($square_category->id, $processed_categories)) { continue; } if (is_object($square_category) && !empty($square_category->name) && !empty($square_category->id)) { $square_categories[$square_category->name] = $square_category->id; $processed_categories[] = $square_category->id; } } foreach ($wc_categories as $wc_cat_name => $wc_cat_id) { $square_cat_id = WC_Square_Utils::get_wc_term_square_id($wc_cat_id); if ($square_cat_id && ($square_cat_name = array_search($square_cat_id, $square_categories))) { // Update a known Square Category whose name has changed in WC. if ($wc_cat_name !== $square_cat_name) { $this->connect->update_square_category($square_cat_id, $wc_cat_name); } } elseif (isset($square_categories[$wc_cat_name])) { // Store the Square Category ID on a WC term that matches. $square_category_id = $square_categories[$wc_cat_name]; WC_Square_Utils::update_wc_term_square_id($wc_cat_id, $square_category_id); } else { // Create a new Square Category for a WC term that doesn't yet exist. $response = $this->connect->create_square_category($wc_cat_name); if (!empty($response->id)) { $square_category_id = $response->id; WC_Square_Utils::update_wc_term_square_id($wc_cat_id, $square_category_id); } } } }
/** * Sync Square categories to WooCommerce. * * Looks for category names that don't exist in WooCommerce, and creates them. */ public function sync_categories() { $square_category_objects = $this->connect->get_square_categories(); $square_categories = array(); $processed_categories = array(); foreach ($square_category_objects as $square_category) { // Square list endpoints may return dups so we must check for that if (in_array($square_category->id, $processed_categories)) { continue; } if (is_object($square_category) && !empty($square_category->name) && !empty($square_category->id)) { $square_categories[$square_category->name] = $square_category->id; $processed_categories[] = $square_category->id; } } if (empty($square_categories)) { WC_Square_Sync_Logger::log('[Square -> WC] No categories found to sync.'); return; } $wc_category_objects = $this->connect->wc->get_product_categories(); $wc_categories = array(); if (is_wp_error($wc_category_objects)) { WC_Square_Sync_Logger::log('[Square -> WC] Error encountered retrieving WC Product Categories: ' . $wc_category_objects->get_error_message()); return; } if (!empty($wc_category_objects['product_categories'])) { foreach ($wc_category_objects['product_categories'] as $wc_category) { if (empty($wc_category['name']) || empty($wc_category['id']) || $wc_category['parent'] !== 0) { continue; } $wc_categories[$wc_category['name']] = $wc_category['id']; } } // Look for previously synced categories and update them with data from Square foreach ($wc_categories as $wc_cat_name => $wc_cat_id) { $wc_square_cat_id = WC_Square_Utils::get_wc_term_square_id($wc_cat_id); // Make sure the associated Square ID still exists on the Square side if ($wc_square_cat_id && ($square_cat_name = array_search($wc_square_cat_id, $square_categories))) { $result = $this->connect->wc->edit_product_category($wc_cat_id, array('product_category' => array('name' => $square_cat_name))); if (is_wp_error($result)) { WC_Square_Sync_Logger::log(sprintf('[Square -> WC] Error updating WC Product Category %d for Square ID %s: %s', $wc_cat_id, $wc_square_cat_id, $result->get_error_message())); continue; } else { if (empty($result['product_category'])) { WC_Square_Sync_Logger::log(sprintf('[Square -> WC] Unexpected empty result updating WC Product Category %d for Square ID %s.', $wc_cat_id, $wc_square_cat_id)); continue; } } // We no longer need to process this Square Category, so remove from list unset($square_categories[$square_cat_name]); } else { WC_Square_Sync_Logger::log(sprintf('[Square -> WC] Cannot sync Square Category ID %s, it no longer exists.', $wc_square_cat_id)); } } /* * Go through the remaining Square Categories and either: * 1) Match them to an existing WC Category * 2) Create a new WC Category */ foreach ($square_categories as $name => $square_id) { if (empty($wc_categories[$name])) { $result = $this->connect->wc->create_product_category(array('product_category' => array('name' => $name))); if (is_wp_error($result)) { WC_Square_Sync_Logger::log(sprintf('[Square -> WC] Error creating WC Product Category for Square ID %s: %s', $wc_square_cat_id, $result->get_error_message())); continue; } else { if (empty($result['product_category'])) { WC_Square_Sync_Logger::log(sprintf('[Square -> WC] Unexpected empty result creating WC Product Category for Square ID %s.', $wc_square_cat_id)); continue; } } $wc_term_id = $result['product_category']['id']; } else { $wc_term_id = $wc_categories[$name]; } WC_Square_Utils::update_wc_term_square_id($wc_term_id, $square_id); } }