Posted on

WooCommerce Demo Store (Hook & Function)

wpJakarta.Com – WooCommerce Demo Store (Hook & Function). Hook dan fungsi WooCommerce Demo Store. Artikel / konten terkait WooCommerce Demo Store Hook. Tips-trik kustomisasi WooCommerce Store Notice. https://wpjakarta.com/woocommerce/woocommerce-store-notice/.

WooCommerce Demo Store Hook

woocommerce_demo_store()

Adds a demo store banner to the site if enabled. (Menambahkan banner informasi demo jika di aktifkan)

woocommerce_demo_store() : mixed

includes/wc-template-functions.php : 1042

if ( ! function_exists( 'woocommerce_demo_store' ) ) {

	/**
	 * Adds a demo store banner to the site if enabled.
	 */
	function woocommerce_demo_store() {
		if ( ! is_store_notice_showing() ) {
			return;
		}

		$notice = get_option( 'woocommerce_demo_store_notice' );

		if ( empty( $notice ) ) {
			$notice = __( 'This is a demo store for testing purposes — no orders shall be fulfilled.', 'woocommerce' );
		}

		$notice_id = md5( $notice );

		// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
		echo apply_filters( 'woocommerce_demo_store', '<p class="woocommerce-store-notice demo_store" data-notice-id="' . esc_attr( $notice_id ) . '" style="display:none;">' . wp_kses_post( $notice ) . ' <a href="#" class="woocommerce-store-notice__dismiss-link">' . esc_html__( 'Dismiss', 'woocommerce' ) . '</a></p>', $notice );
	}
}

Contoh berikut adalah untuk menambahkan hook callback.

function wpjakarta_woocommerce_demo_store() {
	if ( get_option( 'woocommerce_demo_store' ) == 'no' )
		return;

	if (! is_woocommerce () ) {
                echo apply_filters( 'wpjakarta_woocommerce_demo_store', '<p class="demo_store" style="display:none;">' . $notice . '</p>'  );
	} else { 
                echo apply_filters( 'wpjakarta_woocommerce_demo_store', '<p class="demo_store">' . $notice . '</p>'  );
        }
}        
// add the filter 
add_filter( 'woocommerce_demo_store', 'wpjakarta_woocommerce_demo_store', 10, 2 ); 

Untuk meremove hook callback.

// remove filter 
remove_action( 'wp_footer', 'woocommerce_demo_store' );

Contoh lainnya.

function conditional_woocommerce_demo_store() {
    remove_action( 'wp_footer', 'woocommerce_demo_store' );

    // menampilkan info pada kategori tertentu
    if ( is_product_category( array( 'pakaian-pria', 'aksesories-pria' ) ) ) {
        add_action( 'woocommerce_before_shop_loop', 'woocommerce_demo_store' );
    }

    // menampilkan info pada halaman single produk tertentu
    if ( is_single( array( 100, 102 ) ) ) {
        add_action( 'woocommerce_before_single_product', 'woocommerce_demo_store' );
    }
}
add_action( 'template_redirect', 'conditional_woocommerce_demo_store' );