I'm using a multiple category structure like:
Each dash (-) represents a subcategory.
Home & Living
-Furniture
--Study room
---Study desk
--Bedroom
---Baby Room
----Baby Chair
-Kitchen
-Dining room
Sport
Garden
...
The code below is on the payment page. If the item to be paid is from the Furniture Category (For example, Study desk or Baby Chair), I have to do some action.
However, I'm not sure how to do this. Each term_id can be retrieved individually. However, this doesn't make much sense as you might guess. Because there are nearly 100 categories.
Actually, I'm looking for the parent of the related product Furniture, but I couldn't find how to do it. I would be very happy if anyone can help
foreach (WC()->cart->get_cart() as $item_id => $item) {
//$product = new WC_Product($item['product_id']);
$product_cats = wp_get_post_terms($item['product_id'], 'product_cat');
foreach ($product_cats as $cat) {
}
If I understand correctly you are just looking for the parent category for each item in which case you can continue with your code and add a check for the parent ie
foreach (WC()->cart->get_cart() as $item_id => $item) {
$product_cats = wp_get_post_terms($item['product_id'], 'product_cat');
$parent_cat = '';
foreach ($product_cats as $cat) {
if ( $cat->parent == 0 ) {
// you have found the parent category so you can end the for loop
$parent_cat = $cat->parent;
break;
}
}
// do the action you needed to with your parent cat $parent_cat
}
Related
I am trying to display the products in my Shop page like that:
A
Asd
Asddd
B
Beer
Bear
and so on. I managed to do this for the categories by overriding and using the woocommerce_output_product_categories action and for them it works, but I want to do this for the products as well(since Woocommerce gives you the option to show products or categories in the shop page). Thank you!
There can be so many possible solutions. But for me this can be done like this:
add_action( 'woocommerce_shop_loop', 'wc_shop_loop', 30 );
function wc_shop_loop() {
global $product, $last_title_first_letter_95845949545454;
$title = $product->get_title();
if ( $last_title_first_letter_95845949545454 !== $title[0] ) {
$last_title_first_letter_95845949545454 = $title[0];
woocommerce_product_loop_end(); // let's close the loop.
echo '<h3>'.$last_title_first_letter_95845949545454. '</h3>'; // add a letter heading.
woocommerce_product_loop_start(); // open a new loop start.
}
}
Tested to work on shop page and product category page.
You will need to work on its css.
Suppose I have parent pages A1 and B1. A1 has child pages A1.1, A1.2,A1.3 and B1 has child pages B1.1, B1.2.
When I am on page A1.1 I shall be able to display A1.2 and A1.3.
Same for A1.2, I shall be able to see A1.1 and A1.3.
If I am on page B1.1, I shall see B1.2 and vice versa.
Note: Every page has an image and a title. I want to get a solution using views.
This thread may be linked to this link if we need the child pages: How to list all child pages of current parent page in drupal 7?
Add a contextual filter nid on both pages, in default select content id from url, then down below in advance section check exclude option.
I managed to do it by creating a view with the following php code in contextual filter
$sibbling = array();
$current = db_query("select menu_name, mlid from {menu_links} where link_path = :node", array(':node' => $_GET['q']));
$current_info = array();
foreach ($current as $value) {
$current_info[] = $value;
}
if($current_info) {
$result = db_query("select mlid, plid, link_path, link_title from {menu_links} where link_path != :node and plid = ( select plid FROM {menu_links} WHERE link_path =:node)", array(':node' => $_GET['q']));
foreach ($result as $row) {
$sibbling[] = $row;
}
}
$nids = array();
foreach ($sibbling as $value){
if( substr( $value->link_path, 0, 5 ) == 'node/' ){
$nids[] = substr( $value->link_path, 5 );
}
}
return implode('+',$nids);
And finally in the plus options we got to check "Allow multiple values "
Save and its done ;-)
Suppose I have parent pages A1 and B1. A1 has child pages A1.1, A1.2,A1.3 and B1 has child pages B1.1, B1.2. I want to list all the respective child pages on A1 and B1. In every child page I have an image and a title. These 2 information needs to be listed in the form of a teaser on the parent page. I need help in doing this whether by coding or by using views, I don't mind as far as I get the proper results. Thank you
You can do this is views by creating a view displaying the fields you require or a teaser. Then add a "Content Nid" contextual filter, in the configeration for this filter under "WHEN THE FILTER VALUE IS NOT AVAILABLE" select "Provide default value" and then "PHP Code" then the code I use is as follows
$children = array();
$current = db_query("select menu_name, mlid from {menu_links} where link_path = :node", array(':node' => $_GET['q']));
$current_info = array();
foreach ($current as $value) {
$current_info[] = $value;
}
if($current_info) {
$result = db_query("select mlid, plid, link_path, link_title from {menu_links} where menu_name=:menu and plid=:mlid and hidden=0 order by weight, link_title", array(':menu' => $current_info[0]->menu_name, ':mlid' => $current_info[0]->mlid));
foreach ($result as $row) {
$children[] = $row;
}
}
$nids = array();
foreach ($children as $value){
if( substr( $value->link_path, 0, 5 ) == 'node/' ){
$nids[] = substr( $value->link_path, 5 );
}
}
return implode('+',$nids);
The last thing to do, under "more" at the bottom of the page sellect "Allow multiple values"
I'm using menu_tree_all_data() to get whole menu structure and then I'm "manually" crawling menu tree.
Also just after reading the tree, I'm calling menu_tree_add_active_path() which will add active trail indicator. It's part of menu block module so you'll have to install it and don't forget to add dependencies for menu block in your module.
$tree = menu_tree_all_data($menu);
menu_tree_add_active_path($tree);
I need to display an intro paragraph on an archive page that displays all posts in the parent and child categories. I'm adding the required content via a sidebar widget and I have code that displays it correctly.
if( is_category( '28' ) || get_sidebar('blog-intro'));
The problem is the side bar widget also displays on all child pages for category 28 => 28/a, 28/b,... I need it on just category 28. Any ideas on how I can filter that out?
The actual category id of a category archive is in the global $cat.
if(is_category()){
global $cat;
if($cat == '28'){
//do stuff here
}
On further lowering of expectations, I think allowing the widget to display on all blog archive pages but not on single posts is acceptable.
But I'm still curious about the answer.
I'd look into get_category()
If I recall correctly, $cat->parent_category() will return 0 if it's a top level category
So:
if( get_query_var( 'cat' ) == 28 ) {
$cat = get_category( 28 );
if( ! $cat->parent_category() ) {
// output content here
}
}
I’ve been using Views to selectively returned nodes, but right now I want to return my nodes and use the Taxonomy term as a group header. I can't see anyway to get Views to do this for me, other then create multiple views on one page.
So I thought I'd right a module. I've written the SQL to return the correct nodes, but I can't work out how to send them to the themeing engine properly. I would like some advice on how to go about this, my tutorial book has examples of building a list as shown below.
foreach ($result as $row2) {
$items[] = l($row2->title,'node/'.$row2->nid.'/edit');
}
return array('#markup' => theme('item_list',array('items' => $items)));
now I want to return my nodes attached image file in Teaser mode, and the title of the node, plus (and I dont want to get ahead of myself) I may also want a couple of the addition node fields appended to the title. Should be easy right? I can't work it out at all.
I have wrangled my way around it (a bit) by using what I'm sure is a non drupal method which looks a bit like this, trouble is I can't get my output to work with ColorBox module, so I'm thinking if I can get official Teaser node data out it might work better, and i'd feel better knowing I was doing things in a drupaly way :)
foreach ($result as $row2) {
$items .= '<img title="'.$row2->title.' '.$row2->fielddata.'" alt="'.$row2->title.'" src="http://localhost/theme/sites/default/files/styles/thumbnail/public/field/image/'.$row2->filename .'"></a>';
$items .= '</div></div></div></div>';
}
return array('#markup' => $items);
Really appreciate any time you take to help me out and thanks in advance.
The following code should help. If you don't already have it, install the devel module, it gives you a wonderful function called dpm() which will print the contents of an array/object to the messages area.
// Get some nodes ids
$nids = db_query('SELECT nid FROM {node}')->fetchCol();
// Load up the node objects
$nodes = node_load_multiple($nids);
// This will print the node object out to the messages area so you can inspect it to find the specific fields you're looking for
dpm($nodes);
// I guess you'll want to do something like this:
$terms = array();
foreach ($nodes as $node) {
// Load the taxonomy term associated with this node. This will be found in a field as this is how taxonomy terms and nodes are related in D7
$term = taxonomy_term_load($node->field_vocab_name['und'][0]['tid']);
// Set up the array
if (!isset($terms[$term->name])) {
$terms[$term->name] = array();
}
// Create some markup for this node
$markup = '<h3>' . l($node->title . ' ' . $node->field_other_field['und'][0]['value'], "node/$node->nid") . '</h3>';
// Add an image
$image = theme('image', array('path' => $node->field_image['und'][0]['uri'], 'alt' => $node->title));
$markup.= $image;
// Add the markup for this node to this taxonomy group's list
$terms[$term->name][] = $markup;
}
// Make up the final page markup
$output = '';
foreach ($terms as $term_name => $node_list) {
$output .= '<h2>' . check_plain($term_name) . '</h2>';
$output .= theme('item_list', array('items' => $node_list));
}
return $output;
Hope that helps
You can get views to group the returned nodes by the taxonomy term for you. Assuming you are using a field view type, just add the taxonomy field and then where it says Format:Unformatted list | Settings click on Settings at the right hand side and choose your taxonomy field as the grouping field.
Note: if you are not using a field view type, or if you are not using unformatted list then the instructions will be a variation of the above.