How to display sub-categories of a category page? - wordpress-theming

I have this code to display a menu in the header, but I want to show the child categories for the selected category page not the parents categories.
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
foreach ( $category_id as $navItem ) {
$class_names = $value = '';
$classes = empty( $navItem->classes ) ? array() : (array) $navItem->classes;
$classes[] = 'menu-item-' . $navItem->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $navItem ) );
if ( in_array( 'current-menu-item', $classes ) )
$class_names .= ' active';
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $navItem->ID, $navItem );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
echo '<li ' . $id . ' ' . $class_names . '>'.$navItem->title.'</li>';
}
?>
thank you

Ok I have made some search and I was able to fix this one
this is the code
<?php
if(is_category() && the_category_ID(false) != NULL)
{
$categories = get_categories(array("child_of" => the_category_ID(false), "hide_empty" => 0));
foreach ($categories as $category)
{
$class_names = $value = '';
$classes = (array) $navitem ='';
$classes[] = 'menu-item-' . $category->cat_ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $category ) );
if ( in_array( 'current-menu-item', $classes ) )
$class_names .= ' active';
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $category->cat_ID, $category );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
echo '<li ' . $id . ' ' . $class_names . '>'.$category->cat_name.'</li>';
}
}
?>
hope it helps someone too
the_category_ID()
this one gets the ID of the current page category and we use false so it wont print the ID
and then loop inside for the sub-categories or child categories and gets their names + slug for URL

Related

filter for selected record

I selected a product and then I have this product in a table, when I want to select other product I have to filter the suggestion of the products it should show the options without the product to add to the table. But I dont know how, if you know an option or an idea I'd appreciate it.
query:
$Input = $request->key;
$almacenOrigen = $request->almacenOrigen;
$lengthSku = 16;
$Sku = substr($Input, 0, $lengthSku);
$name = DB::table('products')
->leftJoin('line_products', 'products.lineProduct_id', '=', 'line_products.id')
->leftJoin('inventories', 'products.id', '=', 'inventories.product_id')
->leftJoin('warehouses', 'inventories.warehouse_id', '=', 'warehouses.id')
->where('inventories.status_id', 1)
->where('products.sku', $Sku)
->where('warehouses.id', $almacenOrigen)
->whereNull('products.deleted_at')
->select(
'products.sku',
'products.reference',
'products.id',
'products.name as nameP',
'line_products.skuLength',
'line_products.expirationLength',
'line_products.lotLength',
'line_products.traceabilityLength',
'line_products.name as nameL',
'inventories.lot',
'inventories.warehouse_id',
'inventories.id as inventory_id',
'inventories.traceability'
)
->get();
$html = '';
if (!empty($name)) {
$Expiration = substr($Input, $name[0]->skuLength, $name[0]->expirationLength);
$yearToday = substr(Carbon::now(), 0, 2);
$year = $yearToday . substr($Expiration, 2, 2);
$month = substr($Expiration, 4, 2);
$day = substr($Expiration, 6, 2);
$Expiration = $year . '-' . $month . '-' . $day;
$Lot = substr($Input, $name[0]->skuLength + $name[0]->expirationLength, $name[0]->lotLength);
$Lot = substr($Lot, 2);
$Traceability = substr($Input, $name[0]->skuLength + $name[0]->expirationLength + $name[0]->lotLength, $name[0]->traceabilityLength);
$Traceability = substr($Traceability, 2);
foreach ($name as $value) {
$html .= '
<div>
<a style="color: #000000" class="suggest-element" exist="1" inventory_id="'.($value->inventory_id).'" lot="'.($value->lot).'" warehouse_id="'.($value->warehouse_id).'" "'
. '" sku="' . ($value->sku)
. '" reference="' . ($value->reference)
. '" caducidad="' . ($Expiration)
. '" lote="' . ($Lot)
. '" trazabilidad="' . ($Traceability)
. '" nameL="' . ($value->nameL)
. '" nameP="' . ($value->nameP)
. '" data="' . ($value->sku . $Expiration . $Lot . $Traceability)
. '" id="' . $value->id . '">'
. ($value->inventory_id . "-" .$value->sku .$Expiration . $Lot . $Traceability ) .
'</a>
</div>'
;
Example:
This are the suggestions, if I select id = 1 this is added in a table then when I want to select another the id=1 have not to appear.
|id |---- sku ----|
| 1 |108888893024899|
| 2 |108888893024896|
| 3 |108888893024895|

2 or 3 Checkboxes in MySQL query

I created the following SQL Query, which works fine. However, I want to add 2 other checkbox $Post's from another label. Now I'm a little bit without any idea how I can add it. Do I have to add an "AND" between the tbs queries?
Here is the code
include "db_connect.inc.php";
$sql = "SELECT * FROM profiles";
$sql .= " WHERE profilename = '". $_POST["profilename"] ."' ";
$sql .= " AND ort = '". $_POST["ort"] ."' ";
$sql .= "AND jahren = '" . $_POST["alter"] . "' ";
$tbs = array();
foreach( array( 'tb1', 'tb2', 'tb3' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`grosse` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$tbs = array();
foreach( array( 'tb4', 'tb5', 'tb6', 'tb7' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`haare` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num==0) echo "Kein Profil gefunden";
echo "<table border='1'>";
echo "<tr><td>Profile</td><td>Alter</td>";
echo "<td>Ort</td><td>Brustgrösse</td>";
echo "<td>Haarfarbe</td></tr>";
while ($dsatz = mysqli_fetch_assoc($res))
{
echo "<tr>";
echo "<td>" . $dsatz["profilename"] . "</td>";
echo "<td>" .$dsatz["jahren"] . "</td>";
echo "<td>" .$dsatz["ort"] . "</td>";
echo "<td>" .$dsatz["grosse"] . "</td>";
echo "<td>" .$dsatz["haare"] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
i could it resolve it. I had a wrong entry in the DB. The query works fine!

Looking for a cleaner way to run a query that has to check for multiple "AND" conditions

I'm running a query against a database and have to check for multiple conditions to be true. The code I have pulls the data I want, but if I had to check many more columns, it could get out of control very quickly. I don't want a query that where I have to write out a dozen or so "AND" conditions. I'm learning some MySQL as I go and any help would be appreciated.
<?php
$servername = "localhost";
$username = "xxxx";
$password = "xxxx";
$dbname = "oldga740_SeniorProject";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Set up and run my Query
$sql = "SELECT Project, Client, DateReceived, LastName, FinalReviewDate FROM Projects
WHERE FinalReviewDate IS NOT NULL
AND DateDelivered IS NULL
AND DateAccepted IS NULL
ORDER BY DateAccepted DESC";
$result = $conn->query($sql);
//Display results
if ($result->num_rows > 0) {
echo "<table><tr><th>Client</th><th>Project</th><th>Point of Contact</th><th>Date Project Received</th><th>Final Review Date</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["Client"]. "</td><td>" . $row ["Project"]. "</td><td> " . $row["LastName"]. "</td><td> " . $row ["DateReceived"]. "</td><td> " . $row["FinalReviewDate"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
You can use an array for your and conditions. But in general you have to be careful if you are going to have prepared statements and bind values. Also, you can create your WHERE clause dynamically in a similar way like the following:
EDIT:
$where = array(
array(
'column'=>'FinalReviewDate',
'operator'=>'AND',
'condition'=>'IS NOT NULL',
'value'=>'',
),
array(
'column'=>'DateDelivered',
'operator'=>'AND',
'condition'=>'IS NULL',
'value'=>'',
),
array(
'column'=>'DateAccepted',
'operator'=>'AND',
'condition'=>'IS NULL',
'value'=>'',
),
array(
'column'=>'other_column',
'operator'=>'OR',
'condition'=>'>=',
'value'=>5,
),
);
$temp = array();
foreach($where as $key => $value) {
if ($value['value'] == '') {
$temp[] = $value['operator'].' '.$value['column'].' '.$value['condition'];
} else {
$temp[] = $value['operator'].' '.$value['column'].' '.$value['condition'].' '.$value['value'];
}
}
$where_sql = '1 = 1 '.implode(' ', $temp);
echo $where_sql;
Result:
1 = 1 AND FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL OR other_column >= 5
OLD ANSWER
$ands = array(
array(
'column'=>'FinalReviewDate',
'condition'=>'IS NOT NULL'
),
array(
'column'=>'DateDelivered',
'condition'=>'IS NULL'
),
array(
'column'=>'DateAccepted',
'condition'=>'IS NULL'
),
);
$temp = array();
foreach($ands as $key => $value) {
$temp[] = $value['column'].' '.$value['condition'];
}
$and_sql = implode(' AND ', $temp);
echo $and_sql;
Result:
FinalReviewDate IS NOT NULL AND DateDelivered IS NULL AND DateAccepted IS NULL

Select dropdown option not selecting on mobile phone (iphone 4s)

When you select another option than default on a mobile phone, the native mobile phone selection list appears. When you then select another option, the price changes however the selected weight does not appear in the selection box.
Open for example this page on the mobile phone.
<select class="variation-selector" id="<?php echo esc_attr( sanitize_title( $name ) ); ?>" name="attribute_<?php echo sanitize_title( $name ); ?>">
<option value="" disabled="disabled"><?php echo __( 'Choose an option', 'woocommerce' ) ?>…</option>
<?php
if ( is_array( $options ) ) {
if ( isset( $_REQUEST[ 'attribute_' . sanitize_title( $name ) ] ) ) {
$selected_value = $_REQUEST[ 'attribute_' . sanitize_title( $name ) ];
} elseif ( isset( $selected_attributes[ sanitize_title( $name ) ] ) ) {
$selected_value = $selected_attributes[ sanitize_title( $name ) ];
} else {
$selected_value = '';
}
// Get terms if this is a taxonomy - ordered
if ( taxonomy_exists( $name ) ) {
$orderby = wc_attribute_orderby( $name );
switch ( $orderby ) {
case 'name' :
$args = array( 'orderby' => 'name', 'hide_empty' => false, 'menu_order' => false );
break;
case 'id' :
$args = array( 'orderby' => 'id', 'order' => 'ASC', 'menu_order' => false, 'hide_empty' => false );
break;
case 'menu_order' :
$args = array( 'menu_order' => 'ASC', 'hide_empty' => false );
break;
}
$terms = get_terms( $name, $args );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) )
continue;
echo '<option value="' . esc_attr( $term->slug ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $term->slug ), false ) . '>' . apply_filters( 'woocommerce_variation_option_name', $term->name ) . '</option>';
}
} else {
foreach ( $options as $option ) {
echo '<option value="' . esc_attr( sanitize_title( $option ) ) . '" ' . selected( sanitize_title( $selected_value ), sanitize_title( $option ), false ) . '>' . esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) ) . '</option>';
}
}
}
?>
</select>
Edit:
The above code outputs:
<select class="variation-selector selecter-element" id="pa_gewicht" name="attribute_pa_gewicht">
<option value="" disabled="disabled">Kies een optie…</option>
<option value="175-g" selected="selected">175 g</option>
<option value="350-g">350 g</option>
</select>
<div class="selecter closed">
<span class="selecter-selected">175 g</span>
<div class="selecter-options" style="display: none;">
<span class="selecter-item disabled first" data-value="">Kies een optie…</span>
<span class="selecter-item selected" data-value="175-g">175 g</span>
<span class="selecter-item last" data-value="350-g">350 g</span>
</div>
</div>
As you can see it duplicates it. Found out this is cause by a line in my javascript, namely:
$j(".orderby, .variations td.value select").selecter();
Which in turn uses the jquery.fs.selecter.min.js

In woocommerce, what determines the order_item_id?

I've got a webshop running with woocommerce. The webshop has products with variations. When I add the same product with the same variation, I want it to generate a new order_item_id, in stead of combining the two under the same order_item_id.
I can't seem to find the bit of code that is responsible for generating the order_item_id.
Could anyone tell me where this bit of code is located?
My goal is to make each product unique in stead of stacking items that are the same.
-EDIT
Ok, so I've narrowed it down to class-wc-cart.php
On line 782 the product ID is generated with the following code:
public function generate_cart_id( $product_id, $variation_id = '', $variation = '', $cart_item_data = array() ) {
$id_parts = array( $product_id );
if ( $variation_id ) $id_parts[] = $variation_id;
if ( is_array( $variation ) ) {
$variation_key = '';
foreach ( $variation as $key => $value ) {
$variation_key .= trim( $key ) . trim( $value );
}
$id_parts[] = $variation_key;
}
if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) {
$cart_item_data_key = '';
foreach ( $cart_item_data as $key => $value ) {
if ( is_array( $value ) ) $value = http_build_query( $value );
$cart_item_data_key .= trim($key) . trim($value);
}
$id_parts[] = $cart_item_data_key;
}
return md5( implode( '_', $id_parts ) );
}
Now all that is left is to edit in a unique ID via a timestamp or something.
Will keep you guys posted.
Solved it.
Open class-wc-cart.php, and add a timestamp, or anything else that is unique to $id_parts.
Just like in the following code:
public function generate_cart_id( $product_id, $variation_id = '', $variation = '', $cart_item_data = array() ) {
$id_parts = array( $product_id );
if ( $variation_id ) $id_parts[] = $variation_id.time();
if ( is_array( $variation ) ) {
$variation_key = '';
foreach ( $variation as $key => $value ) {
$variation_key .= trim( $key ) . trim( $value );
}
$id_parts[] = $variation_key.time();
}
if ( is_array( $cart_item_data ) && ! empty( $cart_item_data ) ) {
$cart_item_data_key = '';
foreach ( $cart_item_data as $key => $value ) {
if ( is_array( $value ) ) $value = http_build_query( $value );
$cart_item_data_key .= trim($key) . trim($value);
}
$id_parts[] = $cart_item_data_key.time();
}
return md5( implode( '_', $id_parts ) );
}