Skip to content

Instantly share code, notes, and snippets.

@artikus11
Last active February 23, 2023 13:29
Show Gist options
  • Save artikus11/246b36f921bae20d5df3ce86d4cf38d5 to your computer and use it in GitHub Desktop.
Save artikus11/246b36f921bae20d5df3ce86d4cf38d5 to your computer and use it in GitHub Desktop.
Функция проверки существования товара на странице. Проверяет на шорткод, блок и шатные страницы архивов
function has_product(): array {
$has_product_page = is_shop() || is_product_category() || is_product_tag() || is_product();
global $post;
if ( ! $post ) {
return [ false, false, false ];
}
$parse_content = parse_blocks( $post->post_content );
$blocks_name = array_filter( wp_list_pluck( $parse_content, 'blockName' ) );
$wc_blocks_name = [];
foreach ( $blocks_name as $block_name ) {
if ( false !== strpos( $block_name, 'woocommerce' ) ) {
$wc_blocks_name[] = $block_name;
}
}
$has_wc_blocks = false;
if ( ! empty( $wc_blocks_name ) ) {
$has_wc_blocks = true;
}
$has_wc_shortcode_products = false;
if ( has_shortcode( $post->post_content, 'products' ) ) {
$has_wc_shortcode_products = true;
}
return [ $has_product_page, $has_wc_blocks, $has_wc_shortcode_products ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment