* @copyright Copyright (c) 2018 Ekomi ltd (http://www.ekomi.de) * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * @link http://www.ekomi.de */ namespace ekomi_integration; if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Prepares the eKomi Floating Widget output. * * @return string|null The widget HTML output, or null if widget cannot be rendered. */ function ei_prepare_floating_widget_output() { $floating_enable = get_option( FLOATING_OPTION_ENABLE, '0' ); if ( ENABLE_VALUE !== (string) $floating_enable ) { return null; } $floating_code = get_option( FLOATING_OPTION_CODE, '' ); if ( empty( $floating_code ) ) { return null; } $default_lang = ei_get_default_lang_code(); $config = ei_get_options_data( $default_lang ); $shop_id = isset( $config['ee_shop_id'] ) ? $config['ee_shop_id'] : ''; if ( empty( $shop_id ) ) { return null; } global $product; if ( ! $product ) { return null; } $product_identifier_type = isset( $config['ee_product_identifier'] ) ? $config['ee_product_identifier'] : PRODUCT_IDENTIFIER_ID; if ( PRODUCT_IDENTIFIER_SKU === $product_identifier_type ) { $product_id = $product->get_sku(); } else { $product_id = $product->get_id(); } if ( empty( $product_id ) || empty( $shop_id ) ) { return null; } $widget_output = str_replace( 'productIDwillbeHere', esc_js( $product_id ), $floating_code ); $widget_output = preg_replace( "/(w\['_customerId'\]\s*=\s*)\d+/", '${1}' . esc_js( $shop_id ), $widget_output ); return $widget_output; } /** * Renders the floating widget on single product pages. * * @return void */ function ei_render_floating_widget_single() { $output = ei_prepare_floating_widget_output(); if ( $output ) { echo '
'; } } add_action( 'woocommerce_single_product_summary', __NAMESPACE__ . '\\ei_render_floating_widget_single', 6 );