Channel_Synchronizer::sync()

Summary

No summary available.

Return

(void)


Source

File: src/BigCommerce/Taxonomies/Channel/Channel_Synchronizer.php

	public function sync() {
		try {
			$channels = $this->fetch_channels_from_api();
		} catch ( ApiException $e ) {
			do_action( 'bigcommerce/log', Error_Log::ERROR, __( 'Unable to import channels', 'bigcommerce' ), $e->getMessage() );
			do_action( 'bigcommerce/log', Error_Log::DEBUG, $e->getTraceAsString(), [] );

			return;
		}

		/** @var \WP_Term[] $terms */
		$terms          = get_terms( [
			'taxonomy'   => Channel::NAME,
			'hide_empty' => false,
		] );
		$known_channels = [];

		// Synchronize previously imported channels
		foreach ( $terms as $term ) {
			$channel_id = get_term_meta( $term->term_id, Channel::CHANNEL_ID, true );
			if ( empty( $channel_id ) || ! array_key_exists( $channel_id, $channels ) ) {
				$this->mark_orphan( $term );
				continue;
			}

			$this->update_term( $term, $channels[ $channel_id ] );
			$known_channels[] = $channel_id;
		}

		// Add WP terms for all other channels
		foreach ( $channels as $channel_id => $channel ) {
			if ( in_array( $channel_id, $known_channels ) ) {
				continue; // already have a WP term
			}
			$this->insert_term( $channel );
		}
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.