| @@ -113,9 +113,11 @@ | ||
| 113 | 113 | if ( ! empty( $post_data['nodes'] ) ) { |
| 114 | 114 | delete_post_meta( $post_id, 'sellkit_steps' ); |
| 115 | 115 | unset( $post_data['sellkit_steps'] ); |
| 116 | 116 | |
| 117 | - $post_data['nodes'] = (object) unserialize( $post_data['nodes'] ); // phpcs:ignore WordPress.PHP | |
| 117 | + $new_node = $this->check_product_status( $post_data['nodes'] ); | |
| 118 | + | |
| 119 | + $post_data['nodes'] = $new_node; | |
| 118 | 120 | } |
| 119 | 121 | |
| 120 | 122 | $post_data['data_is_converted'] = false; |
| 121 | 123 | |
| @@ -134,13 +136,17 @@ | ||
| 134 | 136 | $post_data['sellkit_post_slug'] = get_post_field( 'post_name', $post_id ); |
| 135 | 137 | $post_data['sellkit_post_status'] = get_post_status( $post_id ); |
| 136 | 138 | |
| 137 | 139 | // Unset sales-page when user exploring global checkout page. |
| 138 | - if ( 'checkout' === $page ) { | |
| 140 | + if ( 'checkout' === $page && ! empty( $post_data['nodes'] ) ) { | |
| 139 | 141 | foreach ( $post_data['nodes'] as $key => $step ) { |
| 140 | - if ( 'sales-page' === $step['type']->key ) { | |
| 142 | + if ( is_array( $step['type'] ) && 'sales-page' === $step['type']['key'] ) { | |
| 141 | 143 | unset( $post_data['nodes']->$key ); |
| 142 | 144 | } |
| 145 | + | |
| 146 | + if ( is_object( $step['type'] ) && 'sales-page' === $step['type']->key ) { | |
| 147 | + unset( $post_data['nodes']->$key ); | |
| 148 | + } | |
| 143 | 149 | } |
| 144 | 150 | } |
| 145 | 151 | |
| 146 | 152 | wp_send_json_success( [ 'post_data' => $post_data ] ); |
| @@ -155,9 +161,11 @@ | ||
| 155 | 161 | public static function convert_steps_to_nodes( $steps ) { |
| 156 | 162 | $nodes = []; |
| 157 | 163 | $final_steps = $steps; |
| 158 | 164 | |
| 159 | - if ( empty( $steps[0]['targets'] ) ) { | |
| 165 | + $first_key = array_key_first( $steps ); | |
| 166 | + | |
| 167 | + if ( empty( $steps[ $first_key ]['targets'] ) ) { | |
| 160 | 168 | $final_steps = self::add_decision_node_before_upsells( $steps ); |
| 161 | 169 | } |
| 162 | 170 | |
| 163 | 171 | foreach ( $final_steps as $key => $step ) { |
| @@ -213,9 +221,9 @@ | ||
| 213 | 221 | $upsell_num = 0; |
| 214 | 222 | |
| 215 | 223 | foreach ( $steps as $key => $step ) { |
| 216 | 224 | $step['type'] = (object) $step['type']; |
| 217 | - $step['data'] = (object) $step['data']; | |
| 225 | + $step['data'] = ! empty( $step['data'] ) ? (object) $step['data'] : null; | |
| 218 | 226 | |
| 219 | 227 | if ( ! empty( $step['type']->key ) && 'upsell' === $step['type']->key ) { |
| 220 | 228 | $new_node_id = wp_insert_post( [ |
| 221 | 229 | 'post_type' => Sellkit_Admin_Steps::SELLKIT_STEP_POST_TYPE, |
| @@ -313,8 +321,12 @@ | ||
| 313 | 321 | $title = sanitize_text_field( $data['sellkit_post_title'] ); |
| 314 | 322 | $slug = ! empty( $data['sellkit_post_slug'] ) ? sanitize_text_field( $data['sellkit_post_slug'] ) : ''; |
| 315 | 323 | $post_status = 'publish' === $data['sellkit_post_status'] ? 'publish' : 'draft'; |
| 316 | 324 | |
| 325 | + if ( isset( $data['sellkit_global_checkout_header_footer_templates'] ) && 'checkout' === $this->page ) { | |
| 326 | + unset( $data['sellkit_global_checkout_header_footer_templates'] ); | |
| 327 | + } | |
| 328 | + | |
| 317 | 329 | $post_args = [ |
| 318 | 330 | 'post_title' => $title, |
| 319 | 331 | 'post_name' => $slug ?: sanitize_title( $title ), |
| 320 | 332 | 'post_type' => $post_type, |
| @@ -421,8 +433,59 @@ | ||
| 421 | 433 | return; |
| 422 | 434 | } |
| 423 | 435 | |
| 424 | 436 | wp_send_json_error( $errors ); |
| 437 | + } | |
| 438 | + | |
| 439 | + /** | |
| 440 | + * Check product status | |
| 441 | + * | |
| 442 | + * @param mixed $nodes List of node data. | |
| 443 | + * @since 1.7.9 | |
| 444 | + * @return array | |
| 445 | + */ | |
| 446 | + public function check_product_status( $nodes ) { | |
| 447 | + $nodes = (object) unserialize( $nodes ); // phpcs:ignore WordPress.PHP | |
| 448 | + $new_node = []; | |
| 449 | + | |
| 450 | + if ( ! class_exists( 'WooCommerce' ) ) { | |
| 451 | + return $new_node; | |
| 452 | + } | |
| 453 | + | |
| 454 | + foreach ( $nodes as $index => $node ) { | |
| 455 | + if ( ! empty( $node['page_id'] ) ) { | |
| 456 | + $node['link'] = get_the_permalink( $node['page_id'] ); | |
| 457 | + } | |
| 458 | + | |
| 459 | + if ( 'checkout' !== $node['type']['key'] ) { | |
| 460 | + $new_node[ $index ] = $node; | |
| 461 | + continue; | |
| 462 | + } | |
| 463 | + | |
| 464 | + if ( ! empty( $node['data']['products'] ) && ! empty( $node['data']['products']['list'] ) ) { | |
| 465 | + foreach ( $node['data']['products']['list'] as $key => $product ) { | |
| 466 | + $product_obj = wc_get_product( $key ); | |
| 467 | + | |
| 468 | + if ( empty( $product_obj ) ) { | |
| 469 | + unset( $node['data']['products']['list'][ $key ] ); | |
| 470 | + } | |
| 471 | + | |
| 472 | + $data = ! empty( $product_obj ) ? $product_obj->get_data() : []; | |
| 473 | + | |
| 474 | + if ( ! empty( $data['status'] ) && 'trash' === $data['status'] ) { | |
| 475 | + unset( $node['data']['products']['list'][ $key ] ); | |
| 476 | + } | |
| 477 | + } | |
| 478 | + } | |
| 479 | + | |
| 480 | + $new_node[ $index ] = $node; | |
| 481 | + } | |
| 482 | + | |
| 483 | + if ( empty( $new_node ) ) { | |
| 484 | + return $node; | |
| 485 | + } | |
| 486 | + | |
| 487 | + return $new_node; | |
| 425 | 488 | } |
| 426 | 489 | } |
| 427 | 490 | |
| 428 | 491 | Sellkit_Admin_Posts::get_instance(); |