2626use WooCommerce \Square \Framework \Api \Base ;
2727use WooCommerce \Square \API \Requests ;
2828use WooCommerce \Square \API \Responses ;
29+ use Square \Models \CatalogObject ;
2930use Square \Models \ListCatalogResponse ;
3031use Square \SquareClient ;
3132use 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
0 commit comments