Woocommerce REST API how send custom meta? - wordpress

Woocommerce not send meta with custom plug-ins in format .json.
What should I add to the file functions.php?
How to forcibly compel him to send some meta?
Sorry for such a simple question, but I have not found a solution for spaces google

Send the following payload to the API:
$data = [
'name' => 'Product Name',
'type' => 'simple',
'regular_price' => '19.95',
'description' => 'Simple Product,
'categories' => [
['id' => 1]
],
'virtual' => true,
'reviews_allowed' => false,
'meta_data' => [
[
'key' => '_custom_meta_1',
'value' => 'value1'
],
[
'key' => '_custom_meta_2',
'value' => 'value2'
]
]
];
the "meta_data" field, should be an array, containing arrays with "key" and "value" attributes.

Related

WpGraphQL: Creating Extensions - Having Issues

Maybe I'm in over my head but I'm trying to create an extension for WpGraphQL to just gather the YITH WooCommerce Product Add-Ons info for each product but I've gotten to the point of banging my head against the desk in frustration. Is there a fairly easy way to just output the array into nodes? The following is what I have so far, currently I'm only getting one product with a set ID (later, hopefully it will get the one associated with that query). I can get a simple string output but I can't get it to output the array into nodes? What am I doing wrong? I commented out ideas I had for an output but it just usually results in an error or null. I do see the graphql_debug working and outputting $fields as an array though? Thanks!
register_graphql_object_type('MyNewType', [
'description' => __('Describe the Type and what it represents', 'replace-me'),
'fields' => [
'nodes' => [
'type' => 'String',
'description' => __('Describe what this field should be used for', 'replace-me'),
'fields' => [
'form_type' => [
'type' => 'String',
'description' => __('Describe what this field should be used for', 'replace-me'),
],
],
],
],
]);
register_graphql_field(
'Product',
'YITH_fields',
[
'type' => 'MyNewType',
'description' => __('Example field added to the Post Type', 'replace-with-your-textdomain'),
'resolve' => function (\WPGraphQL\Model\Post $post) {
global $wpdb;
$sql = "SELECT wp_live_yith_wapo_types.type, wp_live_yith_wapo_types.options FROM wp_live_yith_wapo_groups JOIN wp_live_yith_wapo_types on wp_live_yith_wapo_groups.id = wp_live_yith_wapo_types.group_id WHERE FIND_IN_SET(13, products_id)";
$results = $wpdb->get_results($sql);
if ($results) {
$array = array('nodes');
//$array = array();
foreach ($results as $result) {
$type = array('form_type' => $result->type);
$options = maybe_unserialize($result->options);
$result = array_merge($type, $options);
$array[] = $result;
}
//$array = wp_json_encode($array);
$fields = !empty($array) ? $array : null;
} else {
$fields = null;
}
graphql_debug($fields, ['type' => 'ARGS_BREAKPOINT']);
//return $fields['nodes'];
return $fields;
}
]
);
Probably it's good enough to return an array of Info 'records' (without embedding into nodes subfield - it would require some, own 'InfoList' type def.), then you need a type for single Info, like:
register_graphql_object_type('MyInfoItem', [
'description' => __('Describe the Type and what it represents', 'replace-me'),
'fields' => [
'form_type' => [
'type' => 'String',
'description' => __('Describe what this field should be used for', 'replace-me'),
],
// options field - list of some scalar or custom type
'options' => [
'type' => [ 'list_of' => 'String' ],
'description' => __('Describe what this field should be used for', 'replace-me'),
],
],
]);
extend Product with:
register_graphql_field(
'Product',
'YITH_fields',
[
'type' => [ 'list_of' => 'MyInfoItem' ],
'description' => __('Example field added to the Post Type', 'replace-with-your-textdomain'),
'resolve' => function (\WPGraphQL\Model\Post $post) {
// ...
return [
['form_type' => 'some_value'],
[
'form_type' => 'other_value',
'options' => ['opt_1', 'opt_2'],
],
];
Any, not trivial data 'nesting level' requires a separate type definition.
For more complex options 'records' you need to register another type - e.g. ids required for optional option selection.
If you don't want to explicitely express these [unknown/dynamic] structures, you can use custom JSON scalar type.

WP Elementor Choose Control Prefix Class Prefixed %s String Format

I am having trouble with elementor prefix class using with CHOOSE control
Here is code snippet
$this->add_responsive_control(
'course_author_layout',
[
'label' => __('Layout', 'text-domain'),
'type' => Controls_Manager::CHOOSE,
'options' => [
'left' => [
'title' => __('Left', 'text-domain'),
'icon' => 'fa fa-long-arrow-left',
],
'up' => [
'title' => __('Center', 'text-domain'),
'icon' => 'fa fa-long-arrow-up',
],
],
'prefix_class' => 'etlms-author-layout-%s',
'default' => 'left',
]
);
Problem is here in prefix_class. In the HTML Markup prefix class is showing with %s but there should only show value.
Current scenario of class : etlms-author-layout-%sleft
Expected result of class : etlms-author-layout-left
Just remove the %s, then you will get the expected result.

Dynamic query string params in Wordpress Rest API

I cant find any information in the Wordpress rest api documentation about how to pass in a dynamic string as part of a url, I know how to do an ID.
register_rest_route($this->namespace, '/' . $this->rest_base . '/(?P<type>)', [
'args' => [
'type' => [
'description' => __('Type of notification to generate', $this->pluginName),
'type' => 'string',
'enum' => ['product_question']
]
],
[
'methods' => \WP_REST_Server::CREATABLE,
'callback' => [$this, 'create_item'],
'permission_callback' => [$this, 'create_item_permissions_check'],
'args' => $this->get_endpoint_args_for_item_schema(\WP_REST_Server::CREATABLE)
],
'schema' => [$this, 'get_public_item_schema']
]);
The above is the code im using to try and register the route. The route should look something like this /notifications/product_question The last part of the url, would be the dynamic part, and would be one of an array of values

Translations are not saved correctly using A2lix Translation + KnpLabs DoctrineBehaviors

In my Symfony project I use SonataAdminBundle and A2lixTranslationForm + KnpLabsDoctrineBehaviors bundles for multi-language support.
I have Serial and Episode entities which use standard OneToMany and ManyToOne relationship. I display episodes using sonata_type_collection:
->add('episodes', 'sonata_type_collection', [
'by_reference' => false,
],
[
'edit' => 'inline',
'inline' => 'table',
]
)
By default A2lixTranslation bundle displays translatable fields under each other. However, I need to display each translation field of the episode in its own column. In order to achieve this, I created three virtual fields with the same property_path:
->add('episode_title', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'title' => [
'attr' => [
'pattern' => false,
],
'label' => false,
],
],
'exclude_fields' => ['subTitle', 'description'],
'label' => 'Title',
])
->add('episode_subtitle', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'subTitle' => [
'label' => false,
],
],
'exclude_fields' => ['title', 'description'],
'label' => 'Subtitle',
'required' => false,
])
->add('episode_description', 'a2lix_translations', [
'property_path' => 'translations',
'fields' => [
'description' => [
'field_type' => 'textarea',
'label' => false,
],
],
'exclude_fields' => ['title', 'subTitle'],
'label' => 'Description',
'required' => false,
])
As you can see, I display only one field in each a2lix_translations form.
But I faced the following problem: only the last field value (description in this case) is saved when I add new episode using Add new button of the collection form and then clicking Update button of the parent (Serial) form. Wherein already existing episodes are saved correctly if edited.
Could you please point me why is this happening and how to fix the issue. I suppose each translations form values rewrite the previous ones, but why do this work normally when saving existing records?
Or maybe there is another way to display each field in the own column in sonata collection form, this also will be helpful.
Thank you.

Display data attributes in collection field Symfony 2

I have 2 fields in my form. First Name and Email.
$builder->add('firstName', 'text', [
'label' => 'First Name',
'required' => true,
'attr' => [
'data-msg-required' => 'First name is required'
],
'trim' => true])
->add('emails', 'collection', [
'type' => new RegisterEmail,
'required' => true,
'by_reference' => false,
'label' => false,
'options' => [
'attr' => [
'data-msg-required' => 'Email is required'
]
],
]);
However data-msg required is only displayed for first field but not for second one. I know I can add that directly in twig template but is there anyway I can achieve that through form class only.
You should add it to the default value of the RegisterEmail type, with setDefaults()

Resources