tf( 'wc-block-mini-cart wp-block-woocommerce-mini-cart %s', $classes_styles['classes'] ); if ( ! empty( $attributes['className'] ) ) { $wrapper_classes .= ' ' . $attributes['className']; } $wrapper_styles = $classes_styles['styles']; $icon_color = array_key_exists( 'iconColorValue', $attributes ) ? $attributes['iconColorValue'] : 'currentColor'; $product_count_color = array_key_exists( 'productCountColorValue', $attributes ) ? $attributes['productCountColorValue'] : ''; // Default "Cart" icon. $icon = ' '; if ( isset( $attributes['miniCartIcon'] ) ) { if ( 'bag' === $attributes['miniCartIcon'] ) { $icon = ' '; } elseif ( 'bag-alt' === $attributes['miniCartIcon'] ) { $icon = ' '; } } $button_html = $this->get_cart_price_markup( $attributes ) . ' ' . $icon . ' '; if ( is_cart() || is_checkout() ) { if ( $this->should_not_render_mini_cart( $attributes ) ) { return ''; } // It is not necessary to load the Mini-Cart Block on Cart and Checkout page. return ''; } $template_part_contents = ''; // Determine if we need to load the template part from the DB, the theme or WooCommerce in that order. $templates_from_db = BlockTemplateUtils::get_block_templates_from_db( array( 'mini-cart' ), 'wp_template_part' ); if ( count( $templates_from_db ) > 0 ) { $template_slug_to_load = $templates_from_db[0]->theme; } else { $theme_has_mini_cart = BlockTemplateUtils::theme_has_template_part( 'mini-cart' ); $template_slug_to_load = $theme_has_mini_cart ? get_stylesheet() : BlockTemplateUtils::PLUGIN_SLUG; } $template_part = BlockTemplateUtils::get_block_template( $template_slug_to_load . '//mini-cart', 'wp_template_part' ); if ( $template_part && ! empty( $template_part->content ) ) { $template_part_contents = do_blocks( $template_part->content ); } if ( '' === $template_part_contents ) { $template_part_contents = do_blocks( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents file_get_contents( Package::get_path() . 'templates/' . BlockTemplateUtils::DIRECTORY_NAMES['TEMPLATE_PARTS'] . '/mini-cart.html' ) ); } return '
'; } /** * Return the main instance of WC_Cart class. * * @return \WC_Cart CartController class instance. */ protected function get_cart_instance() { $cart = WC()->cart; if ( $cart && $cart instanceof \WC_Cart ) { return $cart; } return null; } /** * Get array with data for handle the tax label. * the entire logic of this function is was taken from: * https://github.com/woocommerce/woocommerce/blob/e730f7463c25b50258e97bf56e31e9d7d3bc7ae7/includes/class-wc-cart.php#L1582 * * @return array; */ protected function get_tax_label() { $cart = $this->get_cart_instance(); if ( $cart && $cart->display_prices_including_tax() ) { if ( ! wc_prices_include_tax() ) { $tax_label = WC()->countries->inc_tax_or_vat(); $display_cart_prices_including_tax = true; return array( 'tax_label' => $tax_label, 'display_cart_prices_including_tax' => $display_cart_prices_including_tax, ); } return array( 'tax_label' => '', 'display_cart_prices_including_tax' => true, ); } if ( wc_prices_include_tax() ) { $tax_label = WC()->countries->ex_tax_or_vat(); return array( 'tax_label' => $tax_label, 'display_cart_prices_including_tax' => false, ); }; return array( 'tax_label' => '', 'display_cart_prices_including_tax' => false, ); } /** * Prepare translations for inner blocks and dependencies. */ protected function get_inner_blocks_translations() { $wp_scripts = wp_scripts(); $translations = array(); $chunks = $this->get_chunks_paths( $this->chunks_folder ); $vendor_chunks = $this->get_chunks_paths( 'vendors--mini-cart-contents-block' ); $shared_chunks = [ 'cart-blocks/cart-line-items--mini-cart-contents-block/products-table-frontend' ]; foreach ( array_merge( $chunks, $vendor_chunks, $shared_chunks ) as $chunk ) { $handle = 'wc-blocks-' . $chunk . '-chunk'; $this->asset_api->register_script( $handle, $this->asset_api->get_block_asset_build_path( $chunk ), [], true ); $translations[] = $wp_scripts->print_translations( $handle, false ); wp_deregister_script( $handle ); } $translations = array_filter( $translations ); return implode( '', $translations ); } /** * Register block pattern for Empty Cart Message to make it translatable. */ public function register_empty_cart_message_block_pattern() { register_block_pattern( 'woocommerce/mini-cart-empty-cart-message', array( 'title' => __( 'Empty Mini-Cart Message', 'woocommerce' ), 'inserter' => false, 'content' => '

' . __( 'Your cart is currently empty!', 'woocommerce' ) . '

', ) ); } /** * Returns whether the Mini-Cart should be rendered or not. * * @param array $attributes Block attributes. * * @return bool */ public function should_not_render_mini_cart( array $attributes ) { return isset( $attributes['cartAndCheckoutRenderStyle'] ) && 'hidden' !== $attributes['cartAndCheckoutRenderStyle']; } }