Skip to content

Commit

Permalink
Prefer using the newly introduced gtin option from Woo core, in case …
Browse files Browse the repository at this point in the history
…exists. Add fallback compatibility for Woo core gtin in case only germanized gtin exists.
  • Loading branch information
dennisnissle committed Dec 4, 2024
1 parent e4c01ac commit 1e24f43
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
15 changes: 14 additions & 1 deletion includes/abstracts/abstract-wc-gzd-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) {
Expand Down
31 changes: 26 additions & 5 deletions woocommerce-germanized.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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;
}
}
}

Expand Down

0 comments on commit 1e24f43

Please sign in to comment.