I am a beginner in wordpress (especially woocommerce). I'm wondering how to do something like this?
I have a global variable product that has product in it.
defined( 'ABSPATH' ) || exit;
global $product;
if( ! is_a( $product, 'WC_Product' ) ){
$product = wc_get_product(get_the_id());
}
Here I check what attributes the product has and foreach here:
<?php
$attributes = $product->get_attributes();
$attribute_keys = array_keys( $attributes );
foreach ( $attributes as $attribute_name => $att ){
$options = $att->get_options();
$att_arr[] = wc_attribute_label($attribute_name);
?>
<div class="pick-color">
<p>Pick color</p>
<div class="colors">
<?php
foreach($options as $option_id){
$term = get_term( $option_id, $attribute_name);
echo "<div>";
echo "<div class='single-color'>";
echo "<a href='javascript:void(0);' class='attribute $term->taxonomy color-$term->slug' data-id='$term->slug'></a>";
echo "</div>";
echo "<span class='color-name'>$term->slug</span>";
echo "</div>";
}
?>
</div>
</div>
<?php } ?>
This is not good, I know and I get this result:
How can I separate the color from the size and have the color have circles and the size of the letters as in the first picture?
Related
I have an example that outputs a list of only child categories:
<div class="categories">
<?php
if ( is_product_category() ) {
$term = get_queried_object();
$taxonomy = $term->taxonomy;
echo '<h3>Запчасти для ' . $term->name . '<h3>'; // выводим текущую категорию
echo '<div class="subcategories-woc">';
// получаем дочерние, если существуют
if ( $term_children = get_term_children( $term->term_id, $taxonomy ) ) {
echo '<ul>';
foreach ( $term_children as $key => $term_child ) {
$term_child = get_term_by( 'id', $term_child, $taxonomy );
// выводим дочерние
if ( $term->term_id == $term_child->parent ) {
echo '<li class="shadow">' . $term_child->name . '</li>';
}
}
echo '</ul>';
}
echo '</div>';
}
?>
</div>
This is what I need, but I would also like to display products that relate to it under each category.
How can I do this?
I tried to use different methods for everything to no avail
Okay so I'm trying to create dynamic product filters that display certain product attributes for certain categories. The attributes that are displayed are chosen via checkbox on the "Edit Category" page.
I have a majority of the coding done, but I'm stuck on this one part. How do I leverage WooCommerce Layered-Nav or WordPress AJAX to turn this form and checkboxes into active product filters?
$ischecked is the array that contains the desired attributes to filter by. The WP_Query grabs all active values for our checked attributes.
<?php
echo '<form action="https://monroedirect.com/mrohardware/product-category/knobs/" method="GET">';
foreach ($ischecked as $display_attribute) {
$tax_val = str_replace(' ', '-', preg_replace('/[^\p{L}\p{N}\s]/u', '', strtolower($display_attribute)));
echo '<div class="attribute_filter" data-op="OR" data-taxonomy="pa_'.$tax_val.'" data-name="'.$display_attribute.'" id="af_'.$i.'">
';
echo '<div class="af_head"><h3>'.$display_attribute.'</h3></div>';
echo '<input type="hidden" name="query_type_'.$tax_val.'" value="or">';
$output = array(); // Initializing
$products = ( new WP_Query($args) );
if ( $products->have_posts() ) {
while ( $products->have_posts() ) {
$products->the_post();
$product = wc_get_product( get_the_id() );
$product_attributes = $product->get_attributes();
$attributes = $ischecked;
foreach ( $attributes as $attribute ) {
$taxonomy = 'pa_' . $attribute;
$values = $product->get_attribute($taxonomy);
if ( ! empty($values) ) {
if($attribute == $display_attribute) {
array_push($output, $values);
}
}
}
}
}
$attrib_list = array_unique($output);
echo '<ul>';
foreach ($attrib_list as $atrib) {
$atribraw = str_replace(' ', '-', strtolower($atrib));
echo '<li>
<input data-name="'.$atrib.'" id="filter_'.$tax_val.'" name="filter_'.$tax_val.'" type="checkbox" value="'.$atrib.'">'.$atrib.'</li>';
}
echo '</ul>';
?>
<?php
echo '</div>';
$i++;
}
echo '<button>Filter Products</button>';
echo '</form>';
?>
I have a problem on the "my account" page of my woocommerce store.
Indeed, when I log in with a new customer account that has never placed an order, I have 3 content containers that disappear.
After testing, they only appear if I place an order with the account in question.
Do you have any idea where this could be coming from?
The 3 divs in question correspond to :
the navigation menu of the page my account that disappears only on the dashboard tab (it reappears on all other tabs - my orders, my addresses ...)
a div with the recently viewed products (see code n°1 below)
a div with the articles recently written by the customer (see code n°2 below)
When an order is placed, all the content reappears.
I also have a div with the recent orders which strangely, does not have any problem of display (see code n°3 below)
Do not hesitate to ask me for clarifications, or if you think that the error comes from a tag elsewhere in my code (a tag of authorization according to the role for example?)
In advance, Thank you very much for your help.
add_shortcode( 'recently_viewed_products', 'bbloomer_recently_viewed_shortcode' );
function bbloomer_recently_viewed_shortcode() {
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array();
$viewed_products = array_reverse( array_filter( array_map( 'absint', $viewed_products ) ) );
if ( empty( $viewed_products ) ) return;
?>
<div class="col-23">
<?php $title = '<h3>Produits consultés récemment</h3>';?>
<?php $product_ids = implode( ",", $viewed_products );?>
<?php return $title . do_shortcode("[products ids='$product_ids' limit='4' columns='4' orderby='post__in' class='recentes-vues']");?>
</div>
<?php }
function custom_track_product_view() {
if ( ! is_singular( 'product' ) ) {
return;
}
global $post;
if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) )
$viewed_products = array();
else
$viewed_products = (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] );
if ( ! in_array( $post->ID, $viewed_products ) ) {
$viewed_products[] = $post->ID;
}
if ( sizeof( $viewed_products ) > 15 ) {
array_shift( $viewed_products );
}
// Store for session only
wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}
add_action( 'template_redirect', 'custom_track_product_view', 20 );
// PRODUIT RECENT PAGE COMPTE
add_action( 'woocommerce_account_dashboard' , 'recentes_vues', 5 );
function recentes_vues() {
echo do_shortcode('[recently_viewed_products]');
}
add_action( 'woocommerce_account_dashboard' , 'recent_posts', 3 );
function recent_posts() {
if ( is_user_logged_in()) :
global $current_user;
wp_get_current_user();
$author_query = array('posts_per_page' => '-1','author' => $current_user->ID);
$author_posts = new WP_Query($author_query);
?><div id="recentposts">
<?php
if ($author_posts->found_posts) {
?>
<ul class="liststylenone">
<?php
while($author_posts->have_posts()) : $author_posts->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?></ul><?php
}
else {
echo '<p>Aucun article trouvé 😞</p>';
}
else :
echo "not logged in";
endif;
?>
add_action( 'woocommerce_account_dashboard', 'recent_order', 2 );
function recent_order(){
// For logged in users only
if ( is_user_logged_in() ) :
$user_id = get_current_user_id(); // The current user ID
// Get the WC_Customer instance Object for the current user
$customer = new WC_Customer( $user_id );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
if ( is_a( $last_order, 'WC_Order' ) ) {
$order_id = $last_order->get_id(); // Get the order id
$order_data = $last_order->get_data(); // Get the order unprotected data in an array
$order_status = $last_order->get_status(); // Get the order status
$date_created = $last_order->get_date_created();
$order_total = $last_order->get_total();
}
?>
<div class="row last-order">
<div class="col-md-7">
<div class="col-3">
<div class="col-5">
<div class="col-md-4 order-status-box">
<h6 class="status">État : <?php echo esc_html( wc_get_order_status_name( $order_status ) ); ?></h6>
</div>
<p class="totalcmdd"><?php echo $order_total."€"; ?></p>
<?php
setlocale(LC_TIME, 'fr_FR');
date_default_timezone_set('Europe/Paris');
echo utf8_encode(strftime('%d %B %Y', strtotime($date_created)));
//echo date('d-F-Y', strtotime($date_created)); ?>
</p>
</div>
<div style="display: flex;flex-direction: row;justify-content: space-evenly;align-items: center;margin-right: 1.5em;margin-left: 1.5em;padding: 0.3em;background-color: white;text-transform: uppercase;text-decoration: none;font-size: 13px;">
<?php foreach ($last_order->get_items() as $item) :
$product = $item->get_product();
$thumbnail = $product->get_image(array(50, 50));
if ($product->get_image_id() > 0) {
$item_name = '<div class="item-thumbnail">' . $thumbnail . '</div>'; // You had an extra variable here
}
echo $item_name . $item->get_name();
endforeach;
endif;
?>
Is there a better way to get the category names for a custom post type in wordpress?
<?php // get the portfolio categories
$terms = get_the_terms( $post->ID, 'filters' );
if ( $terms && ! is_wp_error( $terms ) ) :
$names = array();
$slugs = array();
foreach ( $terms as $term ) {
$names[] = $term->name;
$slugs[] = $term->slug;
}
$name_list = join( " / ", $names );
$slug_list = join( " category-", $slugs );
endif;
?>
<!-- BEGIN portfolio-item-->
<li class="portfolio-item third column category-<?php echo $slug_list; ?>" data-filter="category-<?php echo $slug_list; ?>">
<?php
$taxonomy = 'filters';
$terms = get_terms($taxonomy);
if ( $terms && !is_wp_error( $terms ) ) :
?>
<ul>
<?php foreach ( $terms as $term ) { ?>
<li><?php echo $term->name; ?></li>
<?php } ?>
</ul>
<?php endif;?>
Or in fuctions.php place this:
function get_the_category_custompost( $id = false, $tcat = 'category' ) {
$categories = get_the_terms( $id, $tcat );
if ( ! $categories )
$categories = array();
$categories = array_values( $categories );
foreach ( array_keys( $categories ) as $key ) {
_make_cat_compat( $categories[$key] );
}
return apply_filters( 'get_the_categories', $categories );
}
and call the function as:
<?php $cat = get_the_category_custompost($post->ID, 'Your Custom Taxonomy'); ?>
My answer seems too simple, but I used this to list the categories from a wordpress plugin called DW Question Answer that has separate categories from the standard wp categories.
I am assuming that Design Wall used custom post types to create the q&a part and taxonomies to create the categories.
<ul>
<?php wp_list_categories('taxonomy=dwqa-question_category&hide_empty=0&orderby=id&title_li=');?>
</ul>
Assuming your custom taxonomy is recipegroups, i have implemented and tested this code in functions.php and i am sure that it will work in plugins too.
$recipeTerms = get_terms(array(
'taxonomy' => 'recipegroups',
));
foreach($recipeTerms as $recipeTerm){
if($recipeTerm->parent==0){
echo "<div class='termsBox'>"; // remove these div's to suit your needs..
$termLink =get_term_link( $recipeTerm );
echo "<a href='$termLink'><div class='termParent'>".$recipeTerm->name."</div></a> ";
$termChilds = get_term_children($recipeTerm->term_id, 'recipegroups' );
foreach($termChilds as $child){
$chTerm = get_term_by( 'id', $child, 'recipegroups');
$termLink =get_term_link( $chTerm );
echo "<a href='$termLink'><div class='top-cat-items'>".$chTerm->name."</div></a>";
}
echo "</div>"; // end of termsBox div
}
}
How to get category deep with space padding on this function. At the moment i have select box with all categories have the same level.
<?php
add_action('add_meta_boxes', 'my_custom_metabox');
function my_custom_metabox() {
add_meta_box('custom-taxonomy-dropdown','Brands','taxonomy_dropdowns_box','post','side','high');
}
function taxonomy_dropdowns_box( $post ) {
global $brand_taxonomy, $taxonomy_name;
wp_nonce_field('custom-dropdown', 'dropdown-nonce');
$terms = get_terms( $brand_taxonomy, 'hide_empty=1&hierarchical=1;');
if ( is_a( $terms, 'WP_Error' ) ) {
$terms = array();
}
$object_terms = wp_get_object_terms( $post->ID, $brand_taxonomy, array('fields'=>'ids'));
if ( is_a( $object_terms, 'WP_Error' ) ) {
$object_terms = array();
}
// you can move the below java script to admin_head
?>
<?php
wp_dropdown_categories('show_option_none=Select category&show_count=1&hierarchical=1&taxonomy=ad_cat');
echo "Brand:";
echo "<select id='custombrandoptions' name='custombrands[]'>";
echo "<option value='0'>None</option>";
foreach ( $terms as $term ) {
if ( in_array($term->term_id, $object_terms) ) {
$parent_id = $term->term_id;
echo "<option value='{$term->term_id}' selected='selected'>{$term->name}</option>";
} else {
echo "<option value='{$term->term_id}'>{$term->name}</option>";
}
}
echo "</select><br />";
echo '<input type="text" value="'.$meta = get_post_meta($post->ID, 'cat_include', true).'" />';
}
source: http://paste.php.lv/dc485b1e6f37f09f916fccc6ae70ed2f?lang=php
I am not sure where your problem is located but maybe you are looking fpr str_repeat http://php.net/manual/en/function.str-repeat.php ..
so you might use these way:
echo '<option value="" ..>'.str_repeat(' ',$currentLevel+1).'</option>';
(I do not understand how you get the deep of level.. perhaps it is a good idea to iterate recusrivly through the hierarchy..)