Skip to content

Commit 2898fc2

Browse files
committed
fix missing attribute name issue during product import
1 parent d4393c6 commit 2898fc2

File tree

2 files changed

+75
-10
lines changed

2 files changed

+75
-10
lines changed

includes/API.php

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use WooCommerce\Square\Framework\Api\Base;
2727
use WooCommerce\Square\API\Requests;
2828
use WooCommerce\Square\API\Responses;
29+
use Square\Models\CatalogObject;
2930
use Square\Models\ListCatalogResponse;
3031
use Square\SquareClient;
3132
use Square\Environment;
@@ -610,7 +611,13 @@ public function retrieve_options_data( $cursor = '', $refresh = false ) {
610611

611612
// Stop if transient exists and we don't want to refresh.
612613
if ( $options_data && ! $refresh ) {
613-
return array( '', $options_data, $cursor );
614+
if ( $this->options_transient_needs_refresh( $options_data ) ) {
615+
$refresh = true;
616+
$options_data = array();
617+
delete_transient( 'wc_square_options_data' );
618+
} else {
619+
return array( '', $options_data, $cursor );
620+
}
614621
}
615622

616623
// If transient doesn't exist, initialize the array.
@@ -627,7 +634,8 @@ public function retrieve_options_data( $cursor = '', $refresh = false ) {
627634
$objects = $response->get_data()->getObjects() ? $response->get_data()->getObjects() : array();
628635

629636
foreach ( $objects as $object ) {
630-
$options_data[ $object->getId() ]['name'] = $object->getItemOptionData()->getName();
637+
$option_name = $this->get_item_option_name_from_catalog_object( $object );
638+
$options_data[ $object->getId() ]['name'] = $option_name;
631639

632640
$option_values_object = $object->getItemOptionData()->getValues();
633641
$option_values = array();
@@ -643,7 +651,7 @@ public function retrieve_options_data( $cursor = '', $refresh = false ) {
643651

644652
$cursor = $response->get_data()->getCursor();
645653
if ( ! $cursor ) {
646-
set_transient( 'wc_square_options_data', $options_data );
654+
set_transient( 'wc_square_options_data', $options_data, DAY_IN_SECONDS );
647655
}
648656

649657
return array( $response, $options_data, $cursor );
@@ -751,6 +759,61 @@ public function create_options_and_values( $option_id = false, $attribute_name =
751759
return $option;
752760
}
753761

762+
/**
763+
* Determines the best available name for a catalog option.
764+
*
765+
* @since x.x.x
766+
*
767+
* @param \Square\Models\CatalogObject $catalog_object Catalog option object.
768+
* @return string
769+
*/
770+
public function get_item_option_name_from_catalog_object( ?CatalogObject $catalog_object ) {
771+
772+
if ( ! $catalog_object instanceof CatalogObject || ! $catalog_object->getItemOptionData() ) {
773+
return '';
774+
}
775+
776+
$option_data = $catalog_object->getItemOptionData();
777+
$name = $option_data->getName();
778+
$name = is_string( $name ) ? trim( $name ) : '';
779+
780+
if ( '' === $name && method_exists( $option_data, 'getDisplayName' ) ) {
781+
$display_name = $option_data->getDisplayName();
782+
$name = is_string( $display_name ) ? trim( $display_name ) : '';
783+
}
784+
785+
return $name;
786+
}
787+
788+
/**
789+
* Checks if the cached options data is missing names and needs refreshing.
790+
*
791+
* @since x.x.x
792+
*
793+
* @param mixed $options_data Cached options data.
794+
* @return bool
795+
*/
796+
private function options_transient_needs_refresh( $options_data ) {
797+
798+
if ( ! is_array( $options_data ) ) {
799+
return true;
800+
}
801+
802+
foreach ( $options_data as $option_data ) {
803+
$name = '';
804+
805+
if ( is_array( $option_data ) && isset( $option_data['name'] ) ) {
806+
$name = is_string( $option_data['name'] ) ? trim( $option_data['name'] ) : '';
807+
}
808+
809+
if ( '' === $name ) {
810+
return true;
811+
}
812+
}
813+
814+
return false;
815+
}
816+
754817

755818
/** Locations methods *********************************************************************************************/
756819

includes/Sync/Product_Import.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -782,10 +782,11 @@ protected function extract_square_item_variation_data( $variation ) {
782782
$option_matched = $options_data[ $option_id ]['value_ids'][ $option_value_id ];
783783
} else {
784784
// Fetch option data from Square.
785-
$response = wc_square()->get_api()->retrieve_catalog_object( $option_id );
786-
$option_name = $response->get_data()->getObject()->getItemOptionData()->getName();
785+
$response = wc_square()->get_api()->retrieve_catalog_object( $option_id );
786+
$option_object = $response->get_data()->getObject();
787+
$option_name = wc_square()->get_api()->get_item_option_name_from_catalog_object( $option_object );
787788

788-
$option_values_object = $response->get_data()->getObject()->getItemOptionData()->getValues();
789+
$option_values_object = $option_object && $option_object->getItemOptionData() ? $option_object->getItemOptionData()->getValues() : array();
789790
$option_matched = '';
790791
$option_values = array();
791792
$option_value_ids = array();
@@ -807,7 +808,7 @@ protected function extract_square_item_variation_data( $variation ) {
807808
'value_ids' => $option_value_ids,
808809
);
809810

810-
set_transient( 'wc_square_options_data', $options_data );
811+
set_transient( 'wc_square_options_data', $options_data, DAY_IN_SECONDS );
811812
}
812813

813814
$attributes[] = array(
@@ -888,8 +889,9 @@ protected function extract_attributes_from_square_options( $options, $variations
888889
} else {
889890
// Fetch option name from Square.
890891
$response = wc_square()->get_api()->retrieve_catalog_object( $option_id );
891-
$option_name = $response->get_data()->getObject()->getItemOptionData()->getName();
892-
$option_values_object = $response->get_data()->getObject()->getItemOptionData()->getValues();
892+
$option_object = $response->get_data()->getObject();
893+
$option_name = wc_square()->get_api()->get_item_option_name_from_catalog_object( $option_object );
894+
$option_values_object = $option_object && $option_object->getItemOptionData() ? $option_object->getItemOptionData()->getValues() : array();
893895
$option_value_ids = array();
894896
$option_values = array();
895897

@@ -903,7 +905,7 @@ protected function extract_attributes_from_square_options( $options, $variations
903905
'values' => $option_values,
904906
'value_ids' => array_combine( $option_value_ids, $option_values ),
905907
);
906-
set_transient( 'wc_square_options_data', $options_data );
908+
set_transient( 'wc_square_options_data', $options_data, DAY_IN_SECONDS );
907909
}
908910

909911
// Filter option values to only include those actually used by variations.

0 commit comments

Comments
 (0)