post_id ) || ! empty( $json_ld->post->ID ) ) ) { $product_id = ( ! empty( $json_ld->post_id ) ? $json_ld->post_id : $json_ld->post->ID ); $product = wc_get_product( $product_id ); if ( $product ) { $_data = $this->add_ean_to_product_structured_data( array(), $product ); if ( ! empty( $_data ) ) { $data['richSnippet'] = array_merge( $data['richSnippet'], $_data ); } } } return $data; } /** * product_columns_style. * * @version 3.0.0 * @since 3.0.0 * * @todo (dev) make this optional? (same for barcodes) * @todo (dev) load only on `edit.php?post_type=product` etc.? (same for barcodes) */ function product_columns_style() { ?>is_main_query() && ( $orderby = $query->get( 'orderby' ) ) && 'alg_ean' === $orderby && isset( $query->query['post_type'] ) && in_array( 'product', (array) $query->query['post_type'] ) && isset( $query->is_admin ) && 1 == $query->is_admin ) { $do_exclude_empty_lines = false; $key = alg_wc_ean()->core->ean_key; if ( $do_exclude_empty_lines ) { $query->set( 'meta_key', $key ); } else { $query->set( 'meta_query', array( 'relation' => 'OR', array( 'key' => $key, 'compare' => 'NOT EXISTS' ), array( 'key' => $key, 'compare' => 'EXISTS' ), ) ); } $query->set( 'orderby', 'meta_value ID' ); } } /** * add_product_columns. * * @version 2.2.7 * @since 1.0.0 * * @todo (dev) `__( 'EAN', 'ean-for-woocommerce' )` -> `'EAN'` (everywhere) (i.e., no translation)? */ function add_product_columns( $columns ) { $is_added = false; $_columns = array(); foreach ( $columns as $column_key => $column_title ) { $_columns[ $column_key ] = $column_title; if ( 'sku' === $column_key ) { $_columns['ean'] = get_option( 'alg_wc_ean_title', __( 'EAN', 'ean-for-woocommerce' ) ); $is_added = true; } } if ( ! $is_added ) { // Fallback $_columns['ean'] = get_option( 'alg_wc_ean_title', __( 'EAN', 'ean-for-woocommerce' ) ); } return $_columns; } /** * render_product_column_ean. * * @version 2.4.0 * @since 1.0.1 */ function render_product_column_ean( $do_validate, $ean, $product_id = false ) { return ( $do_validate && ! alg_wc_ean()->core->is_valid_ean( $ean, $product_id ) ? '' . $ean . '' : $ean ); } /** * render_product_columns. * * @version 2.4.0 * @since 1.0.0 */ function render_product_columns( $column, $product_id ) { if ( 'ean' === $column ) { $product = wc_get_product( $product_id ); $values = array(); $do_validate = ( 'yes' === get_option( 'alg_wc_ean_backend_column_validate', 'no' ) ); if ( '' != ( $value = alg_wc_ean()->core->get_ean( $product_id ) ) ) { $values[] = $this->render_product_column_ean( $do_validate, $value, $product_id ); } if ( $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $child_id ) { if ( '' != ( $value = alg_wc_ean()->core->get_ean( $child_id ) ) ) { $values[] = $this->render_product_column_ean( $do_validate, $value, $product_id ); } } } if ( ! empty( $values ) ) { echo implode( ', ', $values ); } } } /** * add_ean_to_product_structured_data. * * @version 4.7.5 * @since 1.0.0 * * @see https://schema.org/Product * @see https://github.com/woocommerce/woocommerce/blob/6.3.1/plugins/woocommerce/includes/class-wc-structured-data.php#L328 * * @todo (dev) what to do if there is no markup data? see: https://github.com/woocommerce/woocommerce/blob/6.3.1/plugins/woocommerce/includes/class-wc-structured-data.php#L324 * @todo (dev) maybe always use `gtin` (... all-numeric string of either 8, 12, 13 or 14 digits...) * @todo (dev) `default` (`C128`): maybe no markup then? */ function add_ean_to_product_structured_data( $markup, $product ) { // Get & filter product EAN $value = alg_wc_ean()->core->get_ean( $product->get_id() ); $value = apply_filters( 'alg_wc_ean_product_structured_data_value', $value, $product ); // Add EAN to the markup if ( '' !== $value || apply_filters( 'alg_wc_ean_product_structured_data_allow_empty_value', false, $product ) ) { // Get key if ( 'yes' === get_option( 'alg_wc_ean_frontend_product_structured_data_key_auto', 'yes' ) ) { $type = alg_wc_ean()->core->get_type( $value, false, $product->get_id() ); switch ( $type ) { case 'EAN8': $key = 'gtin8'; break; case 'UPCA': $key = 'gtin12'; break; case 'EAN13': case 'ISBN13': case 'JAN': $key = 'gtin13'; break; case 'EAN14': $key = 'gtin14'; break; default: // e.g., `AUTO`, `C128` $key = apply_filters( 'alg_wc_ean_product_structured_data_markup_key', 'gtin', $type, $product ); } } else { $key = get_option( 'alg_wc_ean_frontend_product_structured_data_key', 'gtin' ); $key = apply_filters( 'alg_wc_ean_product_structured_data_markup_key', $key, false, $product ); } // Filter & add $value = apply_filters( 'alg_wc_ean_product_structured_data_markup_value', $value, $product ); if ( '' !== $value ) { $markup[ $key ] = $value; } } return $markup; } /** * variations_enqueue_scripts. * * @version 4.4.0 * @since 1.0.0 */ function variations_enqueue_scripts() { if ( 'product_meta' === get_option( 'alg_wc_ean_frontend_variation_position', 'product_meta' ) ) { wp_enqueue_script( 'alg-wc-ean-variations', alg_wc_ean()->plugin_url() . '/includes/js/alg-wc-ean-variations' . ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ? '' : '.min' ) . '.js', array( 'jquery' ), alg_wc_ean()->version, true ); wp_localize_script( 'alg-wc-ean-variations', 'alg_wc_ean_variations_obj', array( 'variations_form' => get_option( 'alg_wc_ean_js_variations_form', '.variations_form' ), 'variations_form_closest' => get_option( 'alg_wc_ean_js_variations_form_closest', '.summary' ), ) ); } } /** * variations_add_params. * * @version 4.6.0 * @since 1.0.0 */ function variations_add_params( $args, $product = false, $variation = false ) { if ( $variation ) { $key = alg_wc_ean()->core->ean_key; if ( 'product_meta' === get_option( 'alg_wc_ean_frontend_variation_position', 'product_meta' ) ) { $args['ean'] = $variation->get_meta( $key ); } else { $args['variation_description'] .= str_replace( '%ean%', $variation->get_meta( $key ), get_option( 'alg_wc_ean_template', alg_wc_ean()->core->get_default_template() ) ); } } return $args; } /** * get_ean_output_data. * * @version 2.2.5 * @since 1.1.0 * * @todo (dev) better solution for variable products */ function get_ean_output_data() { $result = array( 'do_output' => false, 'style' => '', 'value' => alg_wc_ean()->core->get_ean() ); if ( '' !== $result['value'] ) { $result['do_output'] = true; } else { global $product; if ( $product && $product->is_type( 'variable' ) ) { $result['do_output'] = true; $result['style'] = ' style="display:none;"'; } } return $result; } /** * add_ean. * * @version 4.6.0 * @since 1.0.0 * * @todo (dev) template: shortcode vs placeholder? * @todo (dev) customizable wrapping HTML (same for all frontend/backend options) - `ean` class must be present though (for the variations' JS) * @todo (dev) `esc_html__( 'N/A', 'ean-for-woocommerce' )` */ function add_ean( $template, $single_or_loop ) { $output_data = $this->get_ean_output_data(); if ( $output_data['do_output'] ) { global $product; $template = alg_wc_ean()->core->shortcodes->do_shortcode( $template, array( 'product_id' => ( $product ? $product->get_id() : false ) ) ); $ean_html = '' . $output_data['value'] . ''; $template = str_replace( '%ean%', $ean_html, $template ); $output = '' . $template . ''; echo apply_filters( 'alg_wc_ean_display', $output, $output_data['value'], $output_data['style'], $template, $single_or_loop ); } } /** * add_ean_single. * * @version 4.6.0 * @since 2.1.0 */ function add_ean_single() { $this->add_ean( get_option( 'alg_wc_ean_template', alg_wc_ean()->core->get_default_template() ), 'single' ); } /** * add_ean_loop. * * @version 4.6.0 * @since 2.0.0 * * @todo (feature) customizable position(s)? * @todo (dev) variable: implode variations' EANs? */ function add_ean_loop() { $this->add_ean( get_option( 'alg_wc_ean_template_loop', alg_wc_ean()->core->get_default_template() ), 'loop' ); } /** * add_ean_cart. * * @version 2.0.0 * @since 2.0.0 * * @todo (dev) use `$this->add_ean()`? */ function add_ean_cart( $cart_item ) { $product_id = ( ! empty( $cart_item['variation_id'] ) ? $cart_item['variation_id'] : $cart_item['product_id'] ); if ( $ean = alg_wc_ean()->core->get_ean( $product_id ) ) { echo '