From 1e24f43ee800a33ddf280e1914890294ab50314c Mon Sep 17 00:00:00 2001 From: vendidero Date: Wed, 4 Dec 2024 11:30:24 +0100 Subject: [PATCH] Prefer using the newly introduced gtin option from Woo core, in case exists. Add fallback compatibility for Woo core gtin in case only germanized gtin exists. --- .../abstracts/abstract-wc-gzd-product.php | 15 ++++++++- woocommerce-germanized.php | 31 ++++++++++++++++--- 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/includes/abstracts/abstract-wc-gzd-product.php b/includes/abstracts/abstract-wc-gzd-product.php index beb7bd64..02783913 100644 --- a/includes/abstracts/abstract-wc-gzd-product.php +++ b/includes/abstracts/abstract-wc-gzd-product.php @@ -116,7 +116,20 @@ public function get_warranty_attachment_id( $context = 'view' ) { } public function get_gtin( $context = 'view' ) { - return $this->get_prop( 'ts_gtin', $context ); + $gtin = $this->get_prop( 'ts_gtin', $context ); + + /** + * Prefer using the newly introduced GTIN option from the Woo Core. + */ + if ( 'view' === $context && is_callable( array( $this->child, 'get_global_unique_id' ) ) ) { + $wc_core_gtin = $this->child->get_global_unique_id(); + + if ( ! empty( $wc_gtin ) ) { + $gtin = $wc_core_gtin; + } + } + + return $gtin; } public function get_mpn( $context = 'view' ) { diff --git a/woocommerce-germanized.php b/woocommerce-germanized.php index 50768342..b521a374 100644 --- a/woocommerce-germanized.php +++ b/woocommerce-germanized.php @@ -319,6 +319,8 @@ public function init() { add_filter( 'woocommerce_locate_core_template', array( $this, 'email_templates' ), 0, 3 ); add_filter( 'woocommerce_structured_data_product', array( $this, 'add_structured_product_data' ), 10, 2 ); + add_filter( 'woocommerce_product_get_global_unique_id', array( $this, 'add_gtin_fallback' ), 10, 2 ); + add_filter( 'woocommerce_product_variation_get_global_unique_id', array( $this, 'add_gtin_fallback' ), 10, 2 ); // Payment gateways add_filter( 'woocommerce_payment_gateways', array( $this, 'register_gateways' ) ); @@ -1550,13 +1552,32 @@ public function register_gateways( $gateways ) { return $gateways; } + /** + * @param string $gtin + * @param WC_Product $product + * + * @return string + */ + public function add_gtin_fallback( $gtin, $product ) { + if ( empty( $gtin ) ) { + $gtin = wc_gzd_get_gzd_product( $product )->get_gtin( 'edit' ); + } + + return $gtin; + } + public function add_structured_product_data( $markup, $product ) { if ( $gzd_product = wc_gzd_get_gzd_product( $product ) ) { - if ( $gtin = $gzd_product->get_gtin() ) { - $markup['gtin'] = $gtin; - } - if ( $mpn = $gzd_product->get_mpn() ) { - $markup['mpn'] = $mpn; + if ( ! isset( $markup['gtin'] ) || empty( $markup['gtin'] ) ) { + if ( $gtin = $gzd_product->get_gtin() ) { + $markup['gtin'] = $gtin; + } + } + + if ( ! isset( $markup['mpn'] ) || empty( $markup['mpn'] ) ) { + if ( $mpn = $gzd_product->get_mpn() ) { + $markup['mpn'] = $mpn; + } } }