I use the following function call to get a product variations,
$available_variations = $product->get_available_variations();
And in the returned $available_variations I have all the variations.
I am given the variation_id, and I do not have the knowledge of the name of the attribute, which in this case is country-of-origin, so how do i retrieve the attribute values, which are Hong Kong and Singapore in this case?
Below is the dump of above $available_variations,
[18-Mar-2014 17:04:20 UTC] Array
(
[variation_id] => 2968
[attributes] => Array
(
[attribute_pa_country-of-origin] => hong-kong
)
[image_src] =>
[image_link] =>
[image_title] =>
[image_alt] =>
[price_html] => <span class="price"><span class="amount">SGD$15</span></span>
[availability_html] =>
[sku] =>
[weight] => kg
[dimensions] =>
[min_qty] => 1
[max_qty] => 0
[backorders_allowed] =>
[is_in_stock] => 1
[is_downloadable] =>
[is_virtual] =>
[is_sold_individually] => no
)
[18-Mar-2014 17:04:20 UTC] Array
(
[variation_id] => 2969
[attributes] => Array
(
[attribute_pa_country-of-origin] => singapore
)
[image_src] =>
[image_link] =>
[image_title] =>
[image_alt] =>
[price_html] => <span class="price"><span class="amount">SGD$12</span></span>
[availability_html] =>
[sku] =>
[weight] => kg
[dimensions] =>
[min_qty] => 1
[max_qty] => 0
[backorders_allowed] =>
[is_in_stock] => 1
[is_downloadable] =>
[is_virtual] =>
[is_sold_individually] => no
)
I know this is old but just incase someone needs this in the future and comes across this post, the correct way is.
echo $available_variations[0][attributes][attribute_pa_country-of-origin];
echo $available_variations[1][attributes][attribute_pa_country-of-origin];
Related
Is there any option that I can further filter post list for a custom post type in admin based on user_id if the same has been passed as a query string like http://album.multibaselocal.com/wp-admin/edit.php?post_type=albums&user_id=31 ?
Here is what I want to accomplish:
I have a custom post type called album which is used by site users to upload Albums to the system. By default when I go to the list page all albums come up fine. In user list page, I have added a custom column which shows a count of Published albums by each user which links back to album list page with a query string user_id like above.
I want to filter the albums by the user_id I have passed. Is this possible?
I am trying this:
add_filter( 'parse_query', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$requery = array(
'post_type' => 'albums',
'post_author' => $user_id,
'status' => 'publish',
'posts_per_page' => -1,
);
// What to do here?
}
}
I have lost here completely. Not an experienced WP developer!
If I have not understood it all wrong, because the hook I am using is called parse_query, I have to somehow parse the query again and filter the existing result using user_id. But don't know where and how.
EDIT (Query output):
WP_Query Object
(
[query] => Array
(
[post_type] => albums
[posts_per_page] => 20
)
[query_vars] => Array
(
[post_type] => albums
[posts_per_page] => 20
[error] =>
[m] =>
[p] => 0
[post_parent] =>
[subpost] =>
[subpost_id] =>
[attachment] =>
[attachment_id] => 0
[name] =>
[static] =>
[pagename] =>
[page_id] => 0
[second] =>
[minute] =>
[hour] =>
[day] => 0
[monthnum] => 0
[year] => 0
[w] => 0
[category_name] =>
[tag] =>
[cat] =>
[tag_id] =>
[author] =>
[author_name] =>
[feed] =>
[tb] =>
[paged] => 0
[meta_key] => user_id
[meta_value] => 31
[preview] =>
[s] =>
[sentence] =>
[title] =>
[fields] =>
[menu_order] =>
[embed] =>
[category__in] => Array
(
)
[category__not_in] => Array
(
)
[category__and] => Array
(
)
[post__in] => Array
(
)
[post__not_in] => Array
(
)
[post_name__in] => Array
(
)
[tag__in] => Array
(
)
[tag__not_in] => Array
(
)
[tag__and] => Array
(
)
[tag_slug__in] => Array
(
)
[tag_slug__and] => Array
(
)
[post_parent__in] => Array
(
)
[post_parent__not_in] => Array
(
)
[author__in] => Array
(
)
[author__not_in] => Array
(
)
)
[tax_query] => WP_Tax_Query Object
(
[queries] => Array
(
)
[relation] => AND
[table_aliases:protected] => Array
(
)
[queried_terms] => Array
(
)
[primary_table] =>
[primary_id_column] =>
)
[meta_query] =>
[date_query] =>
[post_count] => 0
[current_post] => -1
[in_the_loop] =>
[comment_count] => 0
[current_comment] => -1
[found_posts] => 0
[max_num_pages] => 0
[max_num_comment_pages] => 0
[is_single] =>
[is_preview] =>
[is_page] =>
[is_archive] => 1
[is_date] =>
[is_year] =>
[is_month] =>
[is_day] =>
[is_time] =>
[is_author] =>
[is_category] =>
[is_tag] =>
[is_tax] =>
[is_search] =>
[is_feed] =>
[is_comment_feed] =>
[is_trackback] =>
[is_home] =>
[is_404] =>
[is_embed] =>
[is_paged] =>
[is_admin] => 1
[is_attachment] =>
[is_singular] =>
[is_robots] =>
[is_posts_page] =>
[is_post_type_archive] => 1
[query_vars_hash:WP_Query:private] => 1c0b485390fc9607d8008a14177bd63d
[query_vars_changed:WP_Query:private] =>
[thumbnails_cached] =>
[stopwords:WP_Query:private] =>
[compat_fields:WP_Query:private] => Array
(
[0] => query_vars_hash
[1] => query_vars_changed
)
[compat_methods:WP_Query:private] => Array
(
[0] => init_query_flags
[1] => parse_tax_query
)
)
EDIT (Function body presently using):
add_action( 'pre_get_posts', 'filter_albums_further_if_user_id_found');
function filter_albums_further_if_user_id_found($query) {
if(!is_admin())
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id']) ) {
$user_id = $_GET['user_id'];
$query->set('meta_key', 'user_id');
$query->set('meta_value', $user_id);
}
echo '<pre>'; print_r($query); echo '</pre>'; die();
}
This reminded me of a similar problem I had a couple weeks ago. I was wanting to adjust the 'orderby' part of the query, but I believe you can accomplish your task with the same action. Try using the 'pre_get_posts' action instead of that filter. I haven't tested this, so let me know if it works!
CORRECT ANSWER (EDITED after user input):
function user_id_filter( $query ) {
if ( ! is_admin() && !$query->is_main_query() )
return;
if( isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
$query->set( 'author', $user_id );
}
//Debug if necessary
//echo '<pre>'; print_r($query); echo '</pre>'; die();
}
add_action( 'pre_get_posts', 'user_id_filter', 500000000 );
I used this as a reference. https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
I'm trying to write PHP code to list top selling items (with quantity) and top selling categories (with total sales amount) in a tabular form in descending order.
Item | Quantity
Category | Sales
I know we can retrieve this from Reports screen in WooCommerce, but I need to get this values so I can display them else where in the WordPress admin panel in a widget. I know how to create dashboard widgets, by the way.
Does anyone know how to implement this? Any help is appreciated.
Thanks.
you should look into woocommerce files and look for WC_Admin_Report. You can see some examples and work your way with what you want.
I'll give you this example I made.
add_action('init', 'reports');
function reports() {
include_once(WC()->plugin_path().'/includes/admin/reports/class-wc-admin-report.php');
$wc_report = new WC_Admin_Report();
$data = $wc_report->get_order_report_data( array(
'data' => array(
'_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'
),
'_product_id' => array(
'type' => 'order_item_meta',
'order_item_type' => 'line_item',
'function' => '',
'name' => 'product_id'
),
'order_item_name' => array(
'type' => 'order_item',
'function' => '',
'name' => 'order_item_name',
),
),
'group_by' => 'product_id',
'order_by' => 'quantity DESC',
'query_type' => 'get_results',
'limit' => 20,
'order_status' => array( 'completed', 'processing' ),
) );
print_r($data);
}
sample output of print_r($data);
Array
(
[0] => stdClass Object
(
[quantity] => 4
[gross] => 140
[product_id] => 53
[order_item_name] => Happy Ninja
)
[1] => stdClass Object
(
[quantity] => 3
[gross] => 36
[product_id] => 70
[order_item_name] => Flying Ninja
)
[2] => stdClass Object
(
[quantity] => 1
[gross] => 10
[product_id] => 50
[order_item_name] => Patient Ninja
)
)
you have the data and that would get you up and running for Item | Quantity.
I have a array of posts and i displayed it like bellow
$types = get_post_types( array(
'public' => true,
'_builtin' => false), 'objects' );
foreach ( $types as $type )
echo '<li>'.'<a href="'.home_url().'/'.$type->name.'" id="'.$type->name.'">
<span class="post-text">'. $type->singular_label.'</span>
<span class="entry-title" style="display:none;">'.$type->description.'</span>
</a></li>';
I have to unset an array from the array which i have got from the above array, means
[news] => stdClass Object
(
[labels] => stdClass Object
(
[name] => News
[singular_name] => News
[add_new] => Add News
[add_new_item] => Add New News
[edit_item] => Edit News
[new_item] => New News
[view_item] => View News
[search_items] => Search News
[not_found] => No News Found
[not_found_in_trash] => No News Found in Trash
[parent_item_colon] => Parent Page:
[all_items] => News
[menu_name] => News
[edit] => Edit
[view] => View News
[parent] => Parent News
[name_admin_bar] => News
)
[description] => This will display all the posts under news post type.
[publicly_queryable] => 1
[exclude_from_search] =>
[capability_type] => post
[map_meta_cap] => 1
[_builtin] =>
[_edit_link] => post.php?post=%d
[hierarchical] => 1
[public] => 1
[rewrite] => Array
(
[slug] => news
[with_front] => 1
[pages] => 1
[feeds] => 1
[ep_mask] => 1
)
[has_archive] => 1
[query_var] => news
[register_meta_box_cb] =>
[taxonomies] => Array
(
[0] => category
)
[show_ui] => 1
[menu_position] =>
[menu_icon] =>
[can_export] => 1
[show_in_nav_menus] => 1
[show_in_menu] => 1
[show_in_admin_bar] => 1
[delete_with_user] =>
[label] => News
[singular_label] => News
[name] => news
[cap] => stdClass Object
(
[edit_post] => edit_post
[read_post] => read_post
[delete_post] => delete_post
[edit_posts] => edit_posts
[edit_others_posts] => edit_others_posts
[publish_posts] => publish_posts
[read_private_posts] => read_private_posts
[read] => read
[delete_posts] => delete_posts
[delete_private_posts] => delete_private_posts
[delete_published_posts] => delete_published_posts
[delete_others_posts] => delete_others_posts
[edit_private_posts] => edit_private_posts
[edit_published_posts] => edit_published_posts
)
)
[wardrobes] => stdClass Object
(
[labels] => stdClass Object
(
[name] => Wardrobes
[singular_name] => Wardrobes
[add_new] => Add Wardrobes
[add_new_item] => Add New Wardrobes
[edit_item] => Edit Wardrobes
[new_item] => New Wardrobes
[view_item] => View Wardrobes
[search_items] => Search Wardrobes
[not_found] => No Wardrobes Found
[not_found_in_trash] => No Wardrobes Found in Trash
[parent_item_colon] => Parent Page:
[all_items] => Wardrobes
[menu_name] => Wardrobes
[edit] => Edit
[view] => View Wardrobes
[parent] => Parent Wardrobes
[name_admin_bar] => Wardrobes
)
[description] => This will show all the posts under Wardrobes post type.
[publicly_queryable] => 1
[exclude_from_search] => 1
[capability_type] => post
[map_meta_cap] => 1
[_builtin] =>
[_edit_link] => post.php?post=%d
[hierarchical] => 1
[public] => 1
[rewrite] => Array
(
[slug] => wardrobes
[with_front] => 1
[pages] => 1
[feeds] => 1
[ep_mask] => 1
)
[has_archive] => 1
[query_var] => wardrobes
[register_meta_box_cb] =>
[taxonomies] => Array
(
[0] => category
)
[show_ui] => 1
[menu_position] =>
[menu_icon] =>
[can_export] => 1
[show_in_nav_menus] => 1
[show_in_menu] => 1
[show_in_admin_bar] => 1
[delete_with_user] =>
[label] => Wardrobes
[singular_label] => Wardrobes
[name] => wardrobes
[cap] => stdClass Object
(
[edit_post] => edit_post
[read_post] => read_post
[delete_post] => delete_post
[edit_posts] => edit_posts
[edit_others_posts] => edit_others_posts
[publish_posts] => publish_posts
[read_private_posts] => read_private_posts
[read] => read
[delete_posts] => delete_posts
[delete_private_posts] => delete_private_posts
[delete_published_posts] => delete_published_posts
[delete_others_posts] => delete_others_posts
[edit_private_posts] => edit_private_posts
[edit_published_posts] => edit_published_posts
)
)
[journal] => stdClass Object
(
[labels] => stdClass Object
(
[name] => Journal
[singular_name] => Journal==
[add_new] => Add Journal==
[add_new_item] => Add New Journal==
[edit_item] => Edit Journal==
[new_item] => New Journal==
[view_item] => View Journal==
[search_items] => Search Journal
[not_found] => No Journal Found
[not_found_in_trash] => No Journal Found in Trash
[parent_item_colon] => Parent Page:
[all_items] => Journal
[menu_name] => Journal
[edit] => Edit
[view] => View Journal==
[parent] => Parent Journal==
[name_admin_bar] => Journal==
)
[description] => This will show all the posts under journal post type.
[publicly_queryable] => 1
[exclude_from_search] =>
[capability_type] => post
[map_meta_cap] => 1
[_builtin] =>
[_edit_link] => post.php?post=%d
[hierarchical] => 1
[public] => 1
[rewrite] => Array
(
[slug] => journal
[with_front] => 1
[pages] => 1
[feeds] => 1
[ep_mask] => 1
)
[has_archive] => 1
[query_var] => journal
[register_meta_box_cb] =>
[taxonomies] => Array
(
[0] => category
[1] => post_tag
)
[show_ui] => 1
[menu_position] =>
[menu_icon] =>
[can_export] => 1
[show_in_nav_menus] => 1
[show_in_menu] => 1
[show_in_admin_bar] => 1
[delete_with_user] =>
[label] => Journal
[singular_label] => Journal==
[name] => journal
[cap] => stdClass Object
(
[edit_post] => edit_post
[read_post] => read_post
[delete_post] => delete_post
[edit_posts] => edit_posts
[edit_others_posts] => edit_others_posts
[publish_posts] => publish_posts
[read_private_posts] => read_private_posts
[read] => read
[delete_posts] => delete_posts
[delete_private_posts] => delete_private_posts
[delete_published_posts] => delete_published_posts
[delete_others_posts] => delete_others_posts
[edit_private_posts] => edit_private_posts
[edit_published_posts] => edit_published_posts
)
)
)
Now i have to remove the "wardrobes" from the array, when i got the the custom post type array.
Is there any function in wordpress or in php. Please help me.
If you had googled php + unset, you'd have found:
unset — Unset a given variable
unset() destroys the specified variables.
The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.
In your example: unset( $types['wardrobes'] );
The reason I need to do this is because we are turning our CMS over to a client and we need them to follow the rules but we can't just change the value outright as there is content in the system that breaks the max length rules for some fields and we don't want them to get chopped. So going ahead we just want to target all new content by targeting the new content form (node-form). I currently have a hook that successfully changes the max value for the title below:
function myForm_form_alter(&$form, &$form_state, $form_id) {
if($form['#id'] == 'node-form') {
$form['title']['#maxlength'] = 30;
}
}
I need to add one for a field named "field_feature_desc". I printed the form info to the screen and here is the info I received for that field:
[field_feature_desc] => Array
(
[field_name] => field_feature_desc
[type_name] => article
[display_settings] => Array
(
[weight] => 11
[parent] =>
[label] => Array
(
[format] => above
)
[teaser] => Array
(
[format] => default
[exclude] => 1
)
[full] => Array
(
[format] => default
[exclude] => 1
)
[4] => Array
(
[format] => default
[exclude] => 1
)
[2] => Array
(
[format] => default
[exclude] => 0
)
[3] => Array
(
[format] => default
[exclude] => 0
)
)
[widget_active] => 1
[type] => text
[required] => 0
[multiple] => 0
[db_storage] => 0
[module] => text
[active] => 1
[locked] => 0
[columns] => Array
(
[value] => Array
(
[type] => text
[size] => big
[not null] =>
[sortable] => 1
[views] => 1
)
)
[text_processing] => 0
[max_length] =>
[allowed_values] =>
[allowed_values_php] =>
[widget] => Array
(
[rows] => 5
[size] => 60
[default_value] => Array
(
[0] => Array
(
[value] =>
[_error_element] => default_value_widget][field_feature_desc][0][value
)
)
[default_value_php] =>
[label] => Feature Description
[weight] => -3
[description] => Description text in the feature area on the homepage, if applicable
[type] => text_textfield
[module] => text
)
)
If you have an idea how i would code for this additional field it would be much appreciated. I've been trying the following:
$form['field_feature_desc']['max_length'] = 70;
$form['field_feature_desc']['columns']['value']['length'] = 70;
Sorry if there is already an answer for this question, I looked all over for one and couldn't locate one.
I discovered an array outside of the one i was working with. So the code that did it was:
$form['#field_info'] = array(
'field_feature_desc' => array(
'max_length' => 70,
),
);
Or a shorter version is:
$form['#field_info']['field_feature_desc']['max_length'] = 70;
When adding custom php code for a conditional action, drupal/ubercart provides two php variables ($order and $account) that hold information about the order and account for the checkout that was just completed.
I can't find anywhere on the internet documentation about how these objects are structured...anyone know where this documentation is or how these objects are setup???
thanks
Here's what the $order object looks like on a fairly standard install (might be a bit different depending on your installation. Of course the products in the order dictate what the items section looks like:
stdClass Object
(
[order_id] => 123
[uid] => 456
[order_status] => payment_received
[order_total] => 100
[product_count] => 1
[primary_email] => test#example.com
[delivery_first_name] => Test
[delivery_last_name] => Customer
[delivery_phone] => 123-123-1234
[delivery_company] => ABC Company, Inc.
[delivery_street1] => 123 Easy St.
[delivery_street2] =>
[delivery_city] => Anytown
[delivery_zone] => 39
[delivery_postal_code] => 12345
[delivery_country] => 840
[billing_first_name] => Test
[billing_last_name] => Customer
[billing_phone] => 123-123-1234
[billing_company] => ABC Company, Inc.
[billing_street1] => 123 Easy St.
[billing_street2] =>
[billing_city] => Anytown
[billing_zone] => 39
[billing_postal_code] => 12345
[billing_country] => 840
[payment_method] => credit
[data] => Array
(
[cc_data] => ***encrypted credit card data***
)
[created] => 1295455508
[modified] => 1295457962
[host] => 127.0.0.1
[products] => Array
(
[0] => stdClass Object
(
[order_product_id] => 245
[order_id] => 123
[nid] => 5
[title] => Test Product
[manufacturer] =>
[model] => TEST-PRODUCT-SKU
[qty] => 1
[cost] => 100.00000
[price] => 100.00000
[weight] => 0
[data] => Array
(
[attributes] => Array
(
)
[shippable] => 1
[module] => uc_product
)
[order_uid] => 456
)
)
[payment_details] =>
[quote] => Array
(
[method] => flatrate_1
[accessorials] => 0
[rate] => 7.00000
[quote_form] =>
)
[line_items] => Array
(
[0] => Array
(
[line_item_id] => subtotal
[type] => subtotal
[title] => Subtotal
[amount] => 100
[weight] => 0
[data] =>
)
[1] => Array
(
[line_item_id] => 194
[type] => shipping
[title] => Flat Rate Shipping
[amount] => 7.00000
[weight] => 1
[data] =>
)
)
)
$account is the user object.
$order is the ubercart order object.
There are some minimum defined values for both of these objects, but they can contain anything really. The reason is, that Drupal will allow modules to expand the user object, while ubercart will allow modules to expand the order object.
The best thing to do in such situations is to inspect the objects to figure out how to get to what you need.
The devel module will allow you to pretty print variables using dsm() or dump the variable t a log file using dd(). Those are two ways to get to the variable info.