Skip to content

Instantly share code, notes, and snippets.

@mboynes
Created December 14, 2021 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mboynes/4536e61fd46e64cc91c227e20a1c7ef9 to your computer and use it in GitHub Desktop.
Save mboynes/4536e61fd46e64cc91c227e20a1c7ef9 to your computer and use it in GitHub Desktop.
Product demo model for Mantle with GraphQL fields
<?php
/**
* Product class file.
*
* @package App\Models
*/
namespace App\Models;
use Mantle\Contracts\Database\Registrable;
use Mantle\Database\Model\Post;
use Mantle\Database\Model\Registration\Register_Post_Type;
/**
* Product Model.
*/
class Product extends Post implements Registrable {
use Register_Post_Type;
/**
* Register the post type and other features.
*/
public static function boot() {
\add_action( 'fm_post_product', [ __CLASS__, 'fieldmanager_fields' ] );
\add_action( 'graphql_register_types', [ __CLASS__, 'register_graphql_fields' ] );
}
public static function fieldmanager_fields() {
$fm = new \Fieldmanager_Group(
[
'name' => 'attributes',
'serialize_data' => false,
'add_to_prefix' => false,
'children' => [
'price' => new \Fieldmanager_TextField(
[
'label' => __( 'Price', 'mantle-demo' ),
'input_type' => 'number',
'description' => __( 'Unit price in cents', 'mantle-demo' ),
]
),
'inventory' => new \Fieldmanager_TextField(
[
'label' => __( 'Inventory', 'mantle-demo' ),
'input_type' => 'number',
]
),
],
]
);
$fm->add_meta_box( __( 'Product Attributes', 'mantle-demo' ), [ 'product' ], 'side' );
}
public static function register_graphql_fields() {
register_graphql_field( 'Product', 'price', [
'type' => 'integer',
'description' => __( 'The price of the product in cents', 'mantle-demo' ),
'resolve' => function( $post ) {
return get_post_meta( $post->ID, 'price', true );
},
] );
register_graphql_field( 'Product', 'availability', [
'type' => 'string',
'description' => __( 'The availability of the product', 'mantle-demo' ),
'resolve' => function( $post ) {
if ( $post->post_status !== 'publish' ) {
return strtoupper( $post->post_status );
}
return (int) get_post_meta( $post->ID, 'inventory', true ) > 0 ? 'AVAILABLE' : 'UNAVAILABLE';
},
] );
}
public static function get_registration_args(): array {
return [
'public' => true,
'rest_base' => static::get_object_name(),
'show_in_rest' => true,
'supports' => [ 'title', 'editor', 'thumbnail', 'excerpt' ],
'show_in_graphql' => true,
'graphql_single_name' => 'product',
'graphql_plural_name' => 'products',
'labels' => [
'name' => __( 'Products', 'mantle-demo' ),
'singular_name' => __( 'Product', 'mantle-demo' ),
'all_items' => __( 'All Products', 'mantle-demo' ),
'archives' => __( 'Product Archives', 'mantle-demo' ),
'attributes' => __( 'Product Attributes', 'mantle-demo' ),
'insert_into_item' => __( 'Insert into product', 'mantle-demo' ),
'uploaded_to_this_item' => __( 'Uploaded to this product', 'mantle-demo' ),
'featured_image' => _x( 'Featured Image', 'Product', 'mantle-demo' ),
'set_featured_image' => _x( 'Set featured image', 'Product', 'mantle-demo' ),
'remove_featured_image' => _x( 'Remove featured image', 'Product', 'mantle-demo' ),
'use_featured_image' => _x( 'Use as featured image', 'Product', 'mantle-demo' ),
'filter_items_list' => __( 'Filter products list', 'mantle-demo' ),
'items_list_navigation' => __( 'Products list navigation', 'mantle-demo' ),
'items_list' => __( 'Products list', 'mantle-demo' ),
'new_item' => __( 'New Product', 'mantle-demo' ),
'add_new' => __( 'Add New', 'mantle-demo' ),
'add_new_item' => __( 'Add New Product', 'mantle-demo' ),
'edit_item' => __( 'Edit Product', 'mantle-demo' ),
'view_item' => __( 'View Product', 'mantle-demo' ),
'view_items' => __( 'View Products', 'mantle-demo' ),
'search_items' => __( 'Search products', 'mantle-demo' ),
'not_found' => __( 'No products found', 'mantle-demo' ),
'not_found_in_trash' => __( 'No products found in trash', 'mantle-demo' ),
'parent_item_colon' => __( 'Parent Product:', 'mantle-demo' ),
'menu_name' => __( 'Products', 'mantle-demo' ),
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment