Category deep function - php

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..)

Related

How to display multiple woocommerce attributes?

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?

WooCommerce Attribute Filter with No Plugin

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>';
?>

Custom dropdown list of terms as links (woocommerce, wordpress)

I want to add a custom dropdown list of an attribute (in this case it's the brand), with the options lead to the attribute page as a link.
I got this working
add_filter('woocommerce_before_shop_loop','wc_reg_for_menus', 1, 2);
function wc_reg_for_menus() {
$terms = get_terms( 'pa_marke' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<select>';
foreach ( $terms as $term ) {
echo '<option value="'.$term->name.'">'.$term->name.'</option>';
}
echo '</select>';
}
}
And I think I somehow need to add this part
get_term_link( WP_Term|int|string $term, string $taxonomy = '' )
Thank you!
Felix
Your code works well for getting values for a specific taxonomy (attribute).I only changed the get_terms function.
I also added some data attributes to get the taxonomy and slug of each term (option).
add_filter( 'woocommerce_before_shop_loop','wc_reg_for_menus' );
function wc_reg_for_menus() {
$terms = get_terms( array(
'taxonomy' => 'pa_marke',
'hide_empty' => false,
));
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<select id="shop_pa_marke">';
foreach ( $terms as $term ) {
echo '<option value="' . $term->name . '" data-taxonomy="' . $term->taxonomy . '" data-slug="' . $term->slug . '">' . $term->name . '</option>';
}
echo '</select>';
}
}
Then you will need to use a jQuery script to redirect the user based on the chosen attribute option.
add_action( 'wp_footer', 'redirect_after_select_option' );
function redirect_after_select_option() {
?>
<script type="text/javascript">
jQuery( function($){
$('#shop_pa_marke').change(function(){
const url = window.location.href;
const taxonomy = $(this).children("option:selected").data('taxonomy').replace('pa_','filter_');
const slug = $(this).children("option:selected").data('slug');
let urlParams = new URLSearchParams(window.location.search);
urlParams.set(taxonomy, slug);
window.location.replace( '?'+urlParams.toString() );
});
});
</script>
<?php
}
The code has been tested and works. Add it to your active theme's functions.php.
Thank you!
Just now I found a solution without jQuery, that works quite fine as well:
add_filter('woocommerce_before_shop_loop','wc_reg_for_menus', 1, 2);
function wc_reg_for_menus() {
$terms = get_terms( 'pa_marke' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
echo '<select onchange="location = this.value;">';
foreach ( $terms as $term ) {
echo '<option value="'.get_term_link( $term ).'">'.$term->name.'</option>';
}
echo '</select>';
}
}
Only after the new page loaded, the first option of the menu is selected again, but that´s ok for me, because it won't be displayed there later on.

Hide row if ACF sub field is checked

I got a ACF Reaper field with a couple of rows I am trying to show. However, I only want the show row if it has a checkbox checked (Checkbox is a subfield within the repeater). I am trying to accomplish that by using if in_array as described in ACF documentation under "Conditional logic":
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) )
I am outputting the result in a WordPress shortcode. For now, my code kinda works, except it shows all results within the repeater field (also those that are unchecked). What am I missing ??
My code:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", get_sub_field( 'bestyrelse' ) ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo get_field( 'upload_dokument' );
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
Managed to solve issue and with the help from #maggiathor's answer. For some reason echo was causing issue. I had to use return insted:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
$content = '<ul class="dokumenter">';
foreach( $rows as $row ) {
if( !in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$pdf = $row['upload_dokument'];
$content = $content . '<li>' . $pdf . '</li>';
}
}
}
$content = $content . '</ul>';
return $content;
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );
You cannot use get_sub_field() within the foreach loop, either you need to use a have_rows-while-loop or access it from the associative array:
function investor_bestyrelse_shortcode() {
$rows = get_field('budgetter_og_nyhedsbreve');
if( $rows ) {
echo '<ul class="slides">';
foreach( $rows as $row ) {
if( in_array( "bestyrelsevalg", $row['bestyrelse'] ) ) {
$image = $row['upload_dokument'];
echo '<li>';
echo $row['upload_dokument'];
echo '</li>';
}
}
echo '</ul>';
}
}
add_shortcode( 'investor_bestyrelse', 'investor_bestyrelse_shortcode' );

Loop through and compare

I have this code here the product tags can be animals, life or market. What I am trying to do is separate their list, but this code creates a new ul for every tag after animals and I do not know why, can anyone help?:
<?php
$versionvalues = get_the_terms( $product->id, 'product_tag');
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
$currentTag = $versionvalue->name;
if($currentTag != $previousTag){
echo '</ul><ul class="products" style="text-align:center;">';
$previousTag = $currentTag;
}else{
$previousTag = $currentTag;
}
}
}
?>
Maybe wordpress doesn't allow me to put get_the_terms values into a variable. I did an echo of the $previousTag in the loop is keeps returning animals.
I have realized that is code is inside my woocommerce loop for my products, so $versionvales gets updated for each product and $previousTag will also reset to animals for each product.
You need to initialize $previousTag = 'animals' before entering the foreach loop, otherwise it will be reinitialized to equal 'animals' at the start of each iteration:
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
.....
}
}
You reinitialized the $previousTag in each loop.
<?php
$versionvalues = get_the_terms( $product->id, 'product_tag');
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
$currentTag = $versionvalue->name;
if($currentTag != $previousTag){
echo '</ul><ul class="products" style="text-align:center;">';
$previousTag = $currentTag;
} else {
$previousTag = $currentTag;
}
}
}
?>
just do not overwrite the variable each time
$versionvalues = get_the_terms( $product->id, 'product_tag');
if($versionvalues){
$previousTag = 'animals';
foreach ( $versionvalues as $versionvalue ) {
$currentTag = $versionvalue->name;
if($currentTag != $previousTag){
echo '</ul><ul class="products" style="text-align:center;">';
$previousTag = $currentTag;
}else{
$previousTag = $currentTag;
}
}
}
?>

Categories