I am getting null response on the below query. I am trying to get sum of the products subtotal and a custom cost field for products. I am able to get the data fine without the where clause but getting NULL with the where clause. The where clause is for getting the orders that have the custom meta 'dropshipper_name' in it with a specific meta_value.
$this->report_data->order_totals = (array) $this->get_order_report_data(
array(
'data' => array(
'_cost_total' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => '_alg_wc_cog_cost',
),
'_profit_total' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => '_line_subtotal',
),
),
'where_meta' => array(
array(
'meta_key' => 'dropshipper_name',
'meta_value' => 'alex',
'operator' => '=',
),
),
'filter_range' => true,
'order_status' => array( 'completed', 'processing', 'on-hold', 'pending' ),
)
);
include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 );
function my_custom_woocommerce_admin_reports( $reports ) {
$sales_by_ebay_payment = array(
'sales_by_ebay_payment' => array(
'title' => 'Sales By Ebay Payment',
'description' => '',
'hide_title' => true,
'callback' => 'sales_by_ebay_payment_callback',
),
);
// This can be: orders, customers, stock or taxes, based on where we want to insert our new reports page
$reports['orders']['reports'] = array_merge( $reports['orders']['reports'], $sales_by_ebay_payment);
return $reports;
}
function sales_by_ebay_payment_callback() {
$report = new WC_Report_Sales_By_Ebay_Payment();
$report->output_report();
}
class WC_Report_Sales_By_Ebay_Payment extends WC_Admin_Report {
/**
* Output the report.
*/
public function output_report() {
$ranges = array(
'year' => __( 'Year', 'woocommerce' ),
'last_month' => __( 'Last month', 'woocommerce' ),
'month' => __( 'This month', 'woocommerce' ),
);
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month';
if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) {
$current_range = 'month';
}
$this->check_current_range_nonce( $current_range );
$this->calculate_current_range( $current_range );
$hide_sidebar = true;
include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
}
/**
* Get the main chart.
*/
public function get_main_chart() {
global $wpdb;
$where_meta = array();
$query_data = array(
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders',
'distinct' => true,
),
'_payment_method' => array(
'type' => 'meta',
'function' => '',
'name' => 'Ebay'
),
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'order_total'
),
);
$where_meta[] = array(
'meta_key' => '_payment_method',
'meta_value' =>'ebay_managed_payment',
'operator' => '=',
'type' => 'meta'
);
$sales_by_country_orders = $this->get_order_report_data( array(
'data' => $query_data,
'query_type' => 'get_results',
'group_by' => 'Ebay',
'where_meta' => $where_meta,
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed' ),
'parent_order_status' => false,
) );
//echo "<pre>";
//print_r($sales_by_country_orders);
?>
<table class="widefat">
<thead>
<tr>
<th><strong>Payment Method</strong></th>
<th><strong>Number Of Orders</strong></th>
<th><strong>Sales</strong></th>
</tr>
</thead>
<tbody>
<?php foreach( $sales_by_country_orders as $order ) {
?>
<tr>
<td><?php echo $order->Ebay; ?></td>
<td><?php echo $order->total_orders; ?></td>
<td><?php echo wc_price($order->order_total); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
}
Related
I am sorry if the title isn't correct, but I have no idea about those coding stuff I don't know how to explain it.
I am using WordPress and my theme has a build-in mega menu.
The problem is that the mega menu settings in admin panel appear twice while inside the file just one.
Example in screenshot below:
Here is the settings file:
<?php if ( ! defined( 'ABSPATH' ) ) { die; }
if(!class_exists('Veera_MegaMenu_Init')){
class Veera_MegaMenu_Init{
protected $fields = array();
protected $default_metakey = '';
public function __construct() {
$query_args = array(
'post_type' => 'la_block',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => 20
);
$this->default_metakey = '_mm_meta';
$this->fields = array(
'icon' => array(
'id' => 'icon',
'type' => 'icon',
'title' => esc_html__('Custom Icon','veera')
),
'nolink' => array(
'id' => 'nolink',
'type' => 'switcher',
'title' => esc_html__("Don't link",'veera')
),
'only_icon' => array(
'id' => 'only_icon',
'type' => 'switcher',
'title' => esc_html__("Show Only Icon",'veera')
),
'hide' => array(
'id' => 'hide',
'type' => 'switcher',
'title' => esc_html__("Don't show a link",'veera')
),
'menu_type' => array(
'id' => 'menu_type',
'type' => 'select',
'title' => esc_html__('Menu Type','veera'),
'options' => array(
'narrow' => esc_html__('Narrow','veera'),
'wide' => esc_html__('Wide','veera')
),
'default' => 'narrow'
),
'submenu_position' => array(
'id' => 'submenu_position',
'type' => 'select',
'title' => esc_html__('SubMenu Position','veera'),
'info' => esc_html__('Apply for parent with "Menu Type" is "narrow"','veera'),
'options' => array(
'right' => esc_html__('Right','veera'),
'left' => esc_html__('Left','veera'),
),
'default' => 'right'
),
'force_full_width' => array(
'id' => 'force_full_width',
'type' => 'switcher',
'title' => esc_html__('Force Full Width','veera'),
'info' => esc_html__('Set 100% window width for popup','veera')
),
'popup_max_width' => array(
'id' => 'popup_max_width',
'type' => 'number',
'title' => esc_html__('Popup Max Width','veera'),
'after' => 'px',
'wrap_class' => 'la-element-popup-max-width'
),
'popup_column' => array(
'id' => 'popup_column',
'type' => 'select',
'title' => esc_html__('Popup Columns','veera'),
'options' => array(
'1' => esc_html__('1 Column','veera'),
'2' => esc_html__('2 Columns','veera'),
'3' => esc_html__('3 Columns','veera'),
'4' => esc_html__('4 Columns','veera'),
'5' => esc_html__('5 Columns','veera'),
'6' => esc_html__('6 Columns','veera')
),
'default' => '4'
),
'item_column' => array(
'id' => 'item_column',
'type' => 'text',
'title' => esc_html__('Columns','veera'),
'desc' => esc_html__('will occupy x columns of parent popup columns', 'veera')
),
'block' => array(
'id' => 'block',
'type' => 'select',
'title' => esc_html__('Custom Block Before Menu Item','veera'),
'options' => 'posts',
'query_args' => $query_args,
'default_option' => esc_html__('Select a block','veera')
),
'block2' => array(
'id' => 'block2',
'type' => 'select',
'title' => esc_html__('Custom Block After Menu Item','veera'),
'options' => 'posts',
'query_args' => $query_args,
'default_option' => esc_html__('Select a block','veera')
),
'popup_background' => array(
'id' => 'popup_background',
'type' => 'background',
'title' => esc_html__('Popup Background','veera')
),
'tip_label' => array(
'id' => 'tip_label',
'type' => 'text',
'title' => esc_html__('Tip Label','veera')
),
'tip_color' => array(
'id' => 'tip_color',
'type' => 'color_picker',
'title' => esc_html__('Tip Color','veera')
),
'tip_background_color' => array(
'id' => 'tip_background_color',
'type' => 'color_picker',
'title' => esc_html__('Tip Background','veera')
)
);
$this->load_hooks();
}
private function load_hooks(){
add_action( 'wp_loaded', array( $this, 'load_walker_edit' ), 9);
add_filter( 'wp_setup_nav_menu_item', array( $this, 'setup_nav_menu_item' ));
add_action( 'wp_nav_menu_item_custom_fields', array( $this, 'add_megamu_field_to_menu_item' ), 10, 4);
add_action( 'wp_update_nav_menu_item', array( $this, 'update_nav_menu_item' ), 10, 3);
add_filter( 'nav_menu_item_title', array( $this, 'add_icon_to_menu_item' ),10, 4);
add_filter( 'nav_menu_css_class', array( $this, 'nav_menu_css_class' ),10, 4);
}
public function load_walker_edit() {
add_filter( 'wp_edit_nav_menu_walker', array( $this, 'detect_edit_nav_menu_walker' ), 99 );
}
public function detect_edit_nav_menu_walker( $walker ) {
$walker = 'Veera_MegaMenu_Walker_Edit';
return $walker;
}
public function setup_nav_menu_item($menu_item){
$meta_value = Veera()->settings()->get_post_meta($menu_item->ID, '', $this->default_metakey, true);
foreach ( $this->fields as $key => $value ){
$menu_item->$key = isset($meta_value[$key]) ? $meta_value[$key] : '';
}
return $menu_item;
}
public function update_nav_menu_item( $menu_id, $menu_item_db_id, $menu_item_args ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
$key = $this->default_metakey;
if ( ! empty( $_POST[$key][$menu_item_db_id] ) ) {
$value = $_POST[$key][$menu_item_db_id];
}
else {
$value = null;
}
if(!empty($value)){
update_post_meta( $menu_item_db_id, $key, $value );
}
else {
delete_post_meta( $menu_item_db_id, $key );
}
}
public function add_megamu_field_to_menu_item( $id, $item, $depth, $args ) {
if(function_exists('la_fw_add_element')){
?>
<div class="lastudio-megamenu-settings description-wide la-content">
<h3><?php esc_html_e('MegaMenu Settings','veera');?></h3>
<div class="lastudio-megamenu-custom-fields">
<?php
foreach ( $this->fields as $key => $field ) {
$unique = $this->default_metakey . '['.$item->ID.']';
$default = ( isset( $field['default'] ) ) ? $field['default'] : '';
$elem_id = ( isset( $field['id'] ) ) ? $field['id'] : '';
$field['name'] = $unique. '[' . $elem_id . ']';
$field['id'] = $elem_id . '_' . $item->ID;
$elem_value = isset($item->$key) ? $item->$key : $default;
echo la_fw_add_element( $field, $elem_value, $unique );
}
?>
</div>
</div>
<?php
}
}
public function add_icon_to_menu_item($output, $item, $args, $depth){
if ( !is_a( $args->walker, 'Veera_MegaMenu_Walker' ) && $item->icon){
$icon_class = 'mm-icon ' . $item->icon;
$icon = "<i class=\"".esc_attr($icon_class)."\"></i>";
$output = $icon . $output;
}
return $output;
}
public function nav_menu_css_class( $classes, $item, $args, $depth ){
if(!is_a( $args->walker, 'Veera_MegaMenu_Walker' )){
if ( $item->hide ) {
$classes[] = "mm-item-hide";
}
if ( $item->nolink ) {
$classes[] = "mm-item-nolink";
}
if ( $item->only_icon ) {
$classes[] = "mm-item-onlyicon";
}
}
return $classes;
}
}
}
Can you please help me fix that? (Note: I am really newbie with those stuff, please be detailed)
I am sorry and thanks!
I would like to show order_item_id by get_order_report_data()
I can use the array to show product id and order id :
array(
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'order_id' => array(
'type' => 'order_item',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'order_id'
)
)
But not for order item id.
Any suggestion? Deeply thx.
You can achieve your desired result by using this code.
<?php
$sold_products = $wc_report->get_order_report_data(array(
'data' => array(
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'_qty' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'quantity'
),
'_line_subtotal' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'gross'
),
'_line_total' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => 'SUM',
'name' => 'gross_after_discount'
)
),
'query_type' => 'get_results',
'group_by' => 'product_id',
'where_meta' => '',
'order_by' => 'quantity DESC',
'order_types' => wc_get_order_types('order_count'),
'filter_range' => TRUE,
'order_status' => array('completed'),
));
add_filter( 'woocommerce_admin_reports', 'my_custom_woocommerce_admin_reports', 10, 1 );
function my_custom_woocommerce_admin_reports( $reports ) {
$sales_by_ebay_payment = array(
'sales_by_ebay_payment' => array(
'title' => 'Sales By Ebay Payment',
'description' => '',
'hide_title' => true,
'callback' => 'sales_by_ebay_payment_callback',
),
);
// This can be: orders, customers, stock or taxes, based on where we want to insert our new reports page
$reports['orders']['reports'] = array_merge( $reports['orders']['reports'], $sales_by_ebay_payment);
return $reports;
}
function sales_by_ebay_payment_callback() {
$report = new WC_Report_Sales_By_Ebay_Payment();
$report->output_report();
}
class WC_Report_Sales_By_Ebay_Payment extends WC_Admin_Report {
/**
* Output the report.
*/
public function output_report() {
$ranges = array(
'year' => __( 'Year', 'woocommerce' ),
'last_month' => __( 'Last month', 'woocommerce' ),
'month' => __( 'This month', 'woocommerce' ),
);
$current_range = ! empty( $_GET['range'] ) ? sanitize_text_field( $_GET['range'] ) : 'month';
if ( ! in_array( $current_range, array( 'custom', 'year', 'last_month', '7day' ) ) ) {
$current_range = 'month';
}
$this->check_current_range_nonce( $current_range );
$this->calculate_current_range( $current_range );
$hide_sidebar = true;
include( WC()->plugin_path() . '/includes/admin/views/html-report-by-date.php' );
}
/**
* Get the main chart.
*/
public function get_main_chart() {
global $wpdb;
$where_meta = array();
$query_data = array(
'ID' => array(
'type' => 'post_data',
'function' => 'COUNT',
'name' => 'total_orders',
'distinct' => true,
),
'_payment_method' => array(
'type' => 'meta',
'function' => '',
'name' => 'Ebay'
),
'_order_total' => array(
'type' => 'meta',
'function' => 'SUM',
'name' => 'order_total'
),
);
$where_meta[] = array(
'meta_key' => '_payment_method',
'meta_value' =>'ebay_managed_payment',
'operator' => '=',
'type' => 'meta'
);
$sales_by_country_orders = $this->get_order_report_data( array(
'data' => $query_data,
'query_type' => 'get_results',
'group_by' => 'Ebay',
'where_meta' => $where_meta,
'filter_range' => true,
'order_types' => wc_get_order_types( 'sales-reports' ),
'order_status' => array( 'completed' ),
'parent_order_status' => false,
) );
?>
<table class="widefat">
<thead>
<tr>
<th><strong>Payment Method</strong></th>
<th><strong>Number Of Orders</strong></th>
<th><strong>Sales</strong></th>
</tr>
</thead>
<tbody>
<?php foreach( $sales_by_country_orders as $order ) {
?>
<tr>
<td><?php echo $order->Ebay; ?></td>
<td><?php echo $order->total_orders; ?></td>
<td><?php echo wc_price($order->order_total); ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
}
}
GitHub Repo: https://github.com/CodeBradley/contact-form-7-rest-api/blob/master/lib/endpoints/class-wp-rest-contact-form-7-controller.php
I'm working on an extension to the WordPress Rest API for Contact Form 7 but I'm not sure how to structure their mail objects in my JSON Schema. For anyone who has used the Contact Form 7 plugin, you may have noticed that they have the regular mail object (which is where you add form values, recipients, etc) and they have a mail_2 object for people who want to send a copy of the email to the end user.
Both of the objects mail and mail_2 offer the same properties as you can see in the schema below:
public function get_item_schema() {
$schema = array(
'$schema' => 'http://json-schema.org/draft-04/schema#',
'title' => 'form',
'type' => 'object',
'properties' => array(
'id' => array(
'description' => __( 'Unique identifier for the form.' ),
'type' => 'integer',
'context' => array( 'embed', 'view' ),
'readonly' => true,
),
'title' => array(
'description' => __( 'Display name for the form.' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'name' => array(
'description' => __( 'An alphanumeric identifier for the object unique to its type.' ),
'type' => 'string',
'context' => array( 'embed', 'view' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'locale' => array(
'description' => __( 'The locale setting of the form' ),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
'arg_options' => array(
'sanitize_callback' => 'sanitize_text_field',
),
),
'properties' => array(
'description' => __('The properties of the form'),
'type' => 'object',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'form' => array(
'description' => __('The content for the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail' => array(
'type' => 'object',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'mail-subject' => array(
'description' => __('Subject of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-sender' => array(
'description' => __('Sending email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-recipient' => array(
'description' => __('Recipient email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-additional-headers' => array(
'description' => __('Additional form headers'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-attachment' => array(
'description' => __('Attachments to include with form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-use-html' => array(
'description' => __('Send emails using HTML formatting'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-exclude-blank' => array(
'description' => __('Exclude lines with blank mail-tags from output'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
),
),
'mail-2' => array(
'type' => 'object',
'context' => array( 'embed', 'view', 'edit' ),
'properties' => array(
'mail-2-subject' => array(
'description' => __('Subject of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-sender' => array(
'description' => __('Sending email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-recipient' => array(
'description' => __('Recipient email address of the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-additional-headers' => array(
'description' => __('Additional form headers'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-attachment' => array(
'description' => __('Files to attach to the form'),
'type' => 'string',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-use-html' => array(
'description' => __('Send emails using HTML formatting'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
'mail-2-exclude-blank' => array(
'description' => __('Exclude lines with blank mail-tags from output'),
'type' => 'boolean',
'context' => array( 'embed', 'view', 'edit' ),
),
),
),
),
),
)
);
Currently I'm appending mail properties with mail- and mail-2 properties with mail-2 so that the post values don't get overridden when trying to edit either object. Is this the best way to handle this situation?
Code that currently uses the schema:
prepare_item_for_response() code:
(The returned $data['properties'] key values do not currently map to the defined schema, I'm assuming this need to be fixed? $data['properties'] houses the mail objects)
/**
* The returned $data['properties'] values do not currently conform to the defined schema
*/
public function prepare_item_for_response($form, $request)
{
$data = array();
$data['id'] = $form->id();
$data['name'] = $form->name();
$data['title'] = $form->title();
$data['locale'] = $form->locale;
$data['properties'] = $form->get_properties();
$data = $this->add_additional_fields_to_object($data, $request);
$response = rest_ensure_response($data);
$response = apply_filters('rest_api_prepare_wpcf7', $response, $form, $request);
return $response;
}
prepare_item_for_database() code:
protected function prepare_item_for_database($request) {
$id = $request->get_param('id');
$prepared_form = WPCF7_ContactForm::get_instance( $id );
/**
* update_item ensures the form ID exists before calling this method to prevent non-existant forms from being created accidentally.
*/
if(empty($prepared_form)) {
$prepared_form = WPCF7_ContactForm::get_template();
}
if ( isset( $request['post-title'] ) ) {
$prepared_form->set_title( $request['post-title'] );
}
if ( isset( $request['locale'] ) ) {
$locale = trim( $request['locale'] );
if ( wpcf7_is_valid_locale( $locale ) ) {
$prepared_form->set_locale($locale);
}
}
$properties = $prepared_form->get_properties();
if ( isset( $request['form'] ) ) {
$properties['form'] = trim( $request['form'] );
}
$mail = $properties['mail'];
if ( isset( $request['mail-subject'] ) ) {
$mail['subject'] = trim( $request['mail-subject'] );
}
if ( isset( $request['mail-sender'] ) ) {
$mail['sender'] = trim( $request['mail-sender'] );
}
if ( isset( $request['mail-body'] ) ) {
$mail['body'] = trim( $request['mail-body'] );
}
if ( isset( $request['mail-recipient'] ) ) {
$mail['recipient'] = trim( $request['mail-recipient'] );
}
if ( isset( $request['mail-additional-headers'] ) ) {
$headers = '';
$tempheaders = str_replace(
"\r\n", "\n", $request['mail-additional-headers'] );
$tempheaders = explode( "\n", $tempheaders );
foreach ( $tempheaders as $header ) {
$header = trim( $header );
if ( '' !== $header ) {
$headers .= $header . "\n";
}
}
$mail['additional_headers'] = trim( $headers );
}
if ( isset( $request['mail-attachments'] ) ) {
$mail['attachments'] = trim( $request['mail-attachments'] );
}
$mail['use_html'] = ! empty( $request['mail-use-html'] );
$mail['exclude_blank'] = ! empty( $request['mail-exclude-blank'] );
$properties['mail'] = $mail;
$mail_2 = $properties['mail_2'];
$mail_2['active'] = ! empty( $request['mail-2-active'] );
if ( isset( $request['mail-2-subject'] ) ) {
$mail_2['subject'] = trim( $request['mail-2-subject'] );
}
if ( isset( $request['mail-2-sender'] ) ) {
$mail_2['sender'] = trim( $request['mail-2-sender'] );
}
if ( isset( $request['mail-2-body'] ) ) {
$mail_2['body'] = trim( $request['mail-2-body'] );
}
if ( isset( $request['mail-2-recipient'] ) ) {
$mail_2['recipient'] = trim( $request['mail-2-recipient'] );
}
if ( isset( $request['mail-2-additional-headers'] ) ) {
$headers = '';
$tempheaders = str_replace(
"\r\n", "\n", $request['mail-2-additional-headers'] );
$tempheaders = explode( "\n", $tempheaders );
foreach ( $tempheaders as $header ) {
$header = trim( $header );
if ( '' !== $header ) {
$headers .= $header . "\n";
}
}
$mail_2['additional_headers'] = trim( $headers );
}
if ( isset( $request['mail-2-attachments'] ) ) {
$mail_2['attachments'] = trim( $request['mail-2-attachments'] );
}
$mail_2['use_html'] = ! empty( $request['mail-2-use-html'] );
$mail_2['exclude_blank'] = ! empty( $request['mail-2-exclude-blank'] );
$properties['mail_2'] = $mail_2;
// For setting/updating confirmation/error messages
foreach ( wpcf7_messages() as $key => $arr ) {
$field_name = 'message-' . strtr( $key, '_', '-' );
if ( isset( $request[$field_name] ) ) {
$properties['messages'][$key] = trim( $request[$field_name] );
}
}
if ( isset( $request['additional-settings'] ) ) {
$properties['additional_settings'] = trim( $request['additional-settings'] );
}
$prepared_form->set_properties( $properties );
//Preceded original with rest_api_
do_action( 'rest_api_wpcf7_save_contact_form', $prepared_form );
if ( wpcf7_validate_configuration() ) {
$config_validator = new WPCF7_ConfigValidator( $prepared_form );
$config_validator->validate();
}
return $prepared_form;
}
I'm using this code to get data from text type metabox.. This will create an array
$args = array('post_type' => 'locations_add');
$prefix = 'nilgiris_';
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
while ($the_query->have_posts()):
$the_query->the_post();
$l_names[] = rwmb_meta($prefix . 'l_name');
$lo_name = array();
foreach( $l_names as $l_namea ){
$lo_name[] = array(
$l_namea => $l_namea
);
}
endwhile;
endif;
My problem is that when using the wp_query function along with metabox the metabox will not load..
Hers is the entire code
<?php
$args = array('post_type' => 'locations_add');
$prefix = 'nilgiris_';
$the_query = new WP_Query($args);
if ($the_query->have_posts()):
while ($the_query->have_posts()):
$the_query->the_post();
$l_names[] = rwmb_meta($prefix . 'l_name');
$lo_name = array();
foreach( $l_names as $l_namea ){
$lo_name[] = array(
$l_namea => $l_namea
);
}
endwhile;
endif;
add_filter('rwmb_meta_boxes', 'website_register_meta_boxes');
function website_register_meta_boxes($meta_boxes) {
$prefix = 'nilgiris_';
$meta_boxes[] = array(
'id' => 'location_types',
'title' => __('location name', $prefix),
'post_types' => array('locations_add'),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
// List of meta fields
'fields' => array(
array(
'name' => __('Enter Location Name', $prefix),
'id' => "{$prefix}l_name",
'desc' => __('Enter name', $prefix),
'type' => 'text',
//'clone' => true,
),
),
);
$meta_boxes[] = array(
'id' => 'location_types',
'title' => __('location name', $prefix),
'post_types' => array('Hotels'),
'context' => 'normal',
'priority' => 'high',
'autosave' => true,
'fields' => array(
array(
'name' => __('Enter Location Name', $prefix),
'id' => "{$prefix}H_name",
'desc' => __('Enter name', $prefix),
'type' => 'text',
),
array(
'name' => __('Checkbox list', '$prefix'),
'id' => "{$prefix}H_list",
'type' => 'select',
**'options' => $lo_name,**
),
),
);
return $meta_boxes;
}
I am trying to add a category option in my wordpress theme using option tree. I am using this code in theme-option.php
<?php
$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
add_action( 'admin_init', 'custom_theme_options', 1 );
function custom_theme_options() {
$saved_settings = get_option( 'option_tree_settings', array() );
$custom_settings = array(
'sections' => array(
array(
'id' => 'general',
'title' => 'Home Page Settings'
)
),
'settings' => array(
array(
'id' => 'Great-Product',
'label' => 'Great Product',
'desc' => 'select great Product category',
'type' => 'select',
'section' => 'general',
'std' => 'Choose a category',
'options' => $wp_cats
)
)
);
if ( $saved_settings !== $custom_settings ) {
update_option( 'option_tree_settings', $custom_settings );
}
}
?>
But it is not showing any category. Where is my fault? Please tell me.
You need to define $wp_cats variable within your function, or use global to bring it in.
Option 1 (global variable)
<?php
$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
add_action( 'admin_init', 'custom_theme_options', 1 );
function custom_theme_options() {
global $wp_cats; /** GLOBAL!! */
$saved_settings = get_option( 'option_tree_settings', array() );
$custom_settings = array(
'sections' => array(
array(
'id' => 'general',
'title' => 'Home Page Settings'
)
),
'settings' => array(
array(
'id' => 'Great-Product',
'label' => 'Great Product',
'desc' => 'select great Product category',
'type' => 'select',
'section' => 'general',
'std' => 'Choose a category',
'options' => $wp_cats
)
)
);
if ( $saved_settings !== $custom_settings ) {
update_option( 'option_tree_settings', $custom_settings );
}
}
?>
Option 2 (build variable inside function)
<?php
add_action( 'admin_init', 'custom_theme_options', 1 );
function custom_theme_options() {
$categories = get_categories('hide_empty=0&orderby=name');
$wp_cats = array();
foreach ($categories as $category_list ) {
$wp_cats[$category_list->cat_ID] = $category_list->cat_name;
}
$saved_settings = get_option( 'option_tree_settings', array() );
$custom_settings = array(
'sections' => array(
array(
'id' => 'general',
'title' => 'Home Page Settings'
)
),
'settings' => array(
array(
'id' => 'Great-Product',
'label' => 'Great Product',
'desc' => 'select great Product category',
'type' => 'select',
'section' => 'general',
'std' => 'Choose a category',
'options' => $wp_cats
)
)
);
if ( $saved_settings !== $custom_settings ) {
update_option( 'option_tree_settings', $custom_settings );
}
}
?>
I have found the solution. Just need to write
'type' => 'category-select',
insted of
'type' => 'select',