$square_id, 'square_version' => $square_version, ); self::update_map( $map ); return $map; } /** * Imports or updates local category data for a remote CatalogObject. * * @since 2.0.0 * * @param \Square\Models\CatalogObject $catalog_object the catalog object * @return int|null the category ID, if found */ public static function import_or_update( $catalog_object ) { $id = $catalog_object->getId(); $version = $catalog_object->getVersion(); $name = $catalog_object->getCategoryData()->getName(); // look for category ID by the square ID $category_id = self::get_category_id_by_square_id( $id ); // if not found, search for the category by name if ( ! $category_id ) { if ( $category = get_term_by( 'name', $name, 'product_cat', ARRAY_A ) ) { $category_id = isset( $category['term_id'] ) ? absint( $category['term_id'] ) : null; } } // if still not found, create a new category if ( ! $category_id ) { $inserted_term = wp_insert_term( $name, 'product_cat' ); $category_id = isset( $inserted_term['term_id'] ) ? $inserted_term['term_id'] : null; } if ( $category_id ) { wp_update_term( $category_id, 'product_cat', array( 'name' => $name ) ); self::update_square_meta( $category_id, $id, $version ); } return $category_id; } /** * Updates a category's Square metadata. * * @since 2.0.0 * * @param int $category_id the category ID * @param string $square_id the square ID * @param string $square_version the square version */ public static function update_square_meta( $category_id, $square_id, $square_version ) { update_term_meta( $category_id, self::SQUARE_ID_META_KEY, $square_id ); update_term_meta( $category_id, self::SQUARE_VERSION_META_KEY, $square_version ); } /** * Gets a category ID from a known square ID. * * @since 2.0.0 * * @param string $square_id the square ID * @return int|null */ public static function get_category_id_by_square_id( $square_id ) { global $wpdb; return $wpdb->get_var( $wpdb->prepare( " SELECT t.term_id FROM {$wpdb->prefix}terms AS t LEFT JOIN {$wpdb->prefix}term_taxonomy AS tt ON t.term_id = tt.term_id LEFT JOIN {$wpdb->prefix}termmeta AS tm ON t.term_id = tm.term_id WHERE tt.taxonomy = 'product_cat' AND tm.meta_key = %s AND tm.meta_value = %s ", self::SQUARE_ID_META_KEY, $square_id ) ); } /** * Get category ID from ITEM catalog object. * * @param \Square\Models\CatalogItem $catalog_item the catalog item object * @return string|null */ public static function get_square_category_id( $catalog_item ) { $catalog_category_id = null; // Try to get the category from the reporting category first. $catalog_category = $catalog_item->getReportingCategory(); // If no reporting category, try to get the first category from the categories list. if ( empty( $catalog_category ) ) { $catalog_categories = $catalog_item->getCategories(); if ( ! empty( $catalog_categories ) ) { $catalog_category = $catalog_categories[0]; } } // If we have a category, get the ID. if ( ! empty( $catalog_category ) ) { $catalog_category_id = $catalog_category->getId(); } return $catalog_category_id; } }