1212namespace Magento \Cron \Model \Config \Backend ;
1313
1414use Magento \Framework \Exception \LocalizedException ;
15+ use Magento \Sitemap \Model \Config \Source \GenerationMethod ;
1516
1617/**
1718 * Sitemap configuration
@@ -79,6 +80,9 @@ public function afterSave()
7980 $ this ->_config ->getValue ('sitemap/generate/time ' , $ this ->getScope (), $ this ->getScopeId ()) ?: '0,0,0 '
8081 );
8182 $ frequency = $ this ->getValue ();
83+ $ generationMethod = $ this ->getData ('groups/generate/fields/generation_method/value ' ) ?:
84+ $ this ->_config ->getValue ('sitemap/generate/generation_method ' , $ this ->getScope (), $ this ->getScopeId ())
85+ ?: GenerationMethod::STANDARD ;
8286
8387 $ cronExprArray = [
8488 (int )($ time [1 ] ?? 0 ), //Minute
@@ -91,25 +95,60 @@ public function afterSave()
9195 $ cronExprString = join (' ' , $ cronExprArray );
9296
9397 try {
94- $ this ->_configValueFactory ->create ()->load (
95- self ::CRON_STRING_PATH ,
96- 'path '
97- )->setValue (
98- $ cronExprString
99- )->setPath (
100- self ::CRON_STRING_PATH
101- )->save ();
102- $ this ->_configValueFactory ->create ()->load (
103- self ::CRON_MODEL_PATH ,
104- 'path '
105- )->setValue (
106- $ this ->_runModelPath
107- )->setPath (
108- self ::CRON_MODEL_PATH
109- )->save ();
98+ // Clear old cron configurations first to prevent conflicts
99+ $ this ->clearCronConfiguration (self ::CRON_STRING_PATH );
100+ $ this ->clearCronConfiguration (self ::CRON_MODEL_PATH );
101+
102+ // Configure the selected generation method with correct model class
103+ if ($ generationMethod === GenerationMethod::BATCH ) {
104+ $ this ->setCronConfiguration (self ::CRON_STRING_PATH , $ cronExprString );
105+ $ this ->setCronConfiguration (
106+ self ::CRON_MODEL_PATH ,
107+ 'Magento\Sitemap\Model\Batch\Observer::scheduledGenerateSitemaps '
108+ );
109+ } else {
110+ $ this ->setCronConfiguration (self ::CRON_STRING_PATH , $ cronExprString );
111+ $ this ->setCronConfiguration (
112+ self ::CRON_MODEL_PATH ,
113+ 'Magento\Sitemap\Model\Observer::scheduledGenerateSitemaps '
114+ );
115+ }
110116 } catch (\Exception $ e ) {
111117 throw new LocalizedException (__ ('We can \'t save the cron expression. ' ));
112118 }
113119 return parent ::afterSave ();
114120 }
121+
122+ /**
123+ * Set cron configuration value
124+ *
125+ * @param string $path
126+ * @param string $value
127+ * @return void
128+ */
129+ private function setCronConfiguration (string $ path , string $ value ): void
130+ {
131+ $ this ->_configValueFactory ->create ()->load (
132+ $ path ,
133+ 'path '
134+ )->setValue (
135+ $ value
136+ )->setPath (
137+ $ path
138+ )->save ();
139+ }
140+
141+ /**
142+ * Clear cron configuration value
143+ *
144+ * @param string $path
145+ * @return void
146+ */
147+ private function clearCronConfiguration (string $ path ): void
148+ {
149+ $ configValue = $ this ->_configValueFactory ->create ()->load ($ path , 'path ' );
150+ if ($ configValue ->getId ()) {
151+ $ configValue ->delete ();
152+ }
153+ }
115154}
0 commit comments