ProductPageController.php
| 1 | <?php |
| 2 | namespace SureCart\Controllers\Web; |
| 3 | |
| 4 | use SureCart\Models\Form; |
| 5 | |
| 6 | /** |
| 7 | * Handles Product page requests for frontend. |
| 8 | */ |
| 9 | class ProductPageController extends BasePageController { |
| 10 | /** |
| 11 | * Show the product page |
| 12 | * |
| 13 | * @param \SureCartCore\Requests\RequestInterface $request Request. |
| 14 | * @param \SureCartCore\View $view View. |
| 15 | * |
| 16 | * @return function|void |
| 17 | */ |
| 18 | public function show( $request, $view ) { |
| 19 | // get the product from the query var. |
| 20 | $id = get_query_var( 'sc_product_page_id' ); |
| 21 | |
| 22 | // fetch the product by id/slug. |
| 23 | $this->model = \SureCart\Models\Product::with( [ 'prices', 'image', 'variants', 'variant_options' ] )->find( $id ); |
| 24 | |
| 25 | if ( is_wp_error( $this->model ) ) { |
| 26 | return $this->handleError( $this->model ); |
| 27 | } |
| 28 | |
| 29 | // if this product is a draft, check read permissions. |
| 30 | if ( 'draft' === $this->model->status && ! current_user_can( 'read_sc_products' ) ) { |
| 31 | return $this->notFound(); |
| 32 | } |
| 33 | |
| 34 | // slug changed or we are using the id, redirect. |
| 35 | if ( $this->model->slug !== $id ) { |
| 36 | return \SureCart::redirect()->to( $this->model->permalink ); |
| 37 | } |
| 38 | |
| 39 | set_query_var( 'sc_product_page_id', $this->model->id ); |
| 40 | |
| 41 | // add the filters. |
| 42 | $this->filters(); |
| 43 | |
| 44 | // handle block theme. |
| 45 | if ( wp_is_block_theme() ) { |
| 46 | global $_wp_current_template_content; |
| 47 | $_wp_current_template_content = $this->model->template->content ?? ''; |
| 48 | } |
| 49 | |
| 50 | // include the default view. |
| 51 | include $view; |
| 52 | |
| 53 | return \SureCart::response(); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Handle filters. |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | public function filters(): void { |
| 62 | parent::filters(); |
| 63 | |
| 64 | // Add edit product link to admin bar. |
| 65 | add_action( 'admin_bar_menu', [ $this, 'addEditProductLink' ], 99 ); |
| 66 | |
| 67 | // add data needed for product to load. |
| 68 | add_filter( |
| 69 | 'surecart-components/scData', |
| 70 | function( $data ) { |
| 71 | $form = \SureCart::forms()->getDefault(); |
| 72 | |
| 73 | $data['product_data'] = [ |
| 74 | 'product' => $this->model, |
| 75 | 'form' => $form, |
| 76 | 'mode' => Form::getMode( $form->ID ), |
| 77 | 'checkout_link' => \SureCart::pages()->url( 'checkout' ), |
| 78 | ]; |
| 79 | |
| 80 | return $data; |
| 81 | } |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Add edit product link. |
| 87 | * |
| 88 | * @param \WP_Admin_Bar $wp_admin_bar Admin bar. |
| 89 | * |
| 90 | * @return void |
| 91 | */ |
| 92 | public function addEditProductLink( $wp_admin_bar ): void { |
| 93 | $wp_admin_bar->add_node( |
| 94 | [ |
| 95 | 'id' => 'edit-product', |
| 96 | 'title' => __( 'Edit Product', 'surecart' ), |
| 97 | 'href' => esc_url( \SureCart::getUrl()->edit( 'product', $this->model->id ) ), |
| 98 | ] |
| 99 | ); |
| 100 | } |
| 101 | } |
| 102 |