Im trying to create a stock report for my store which is mainly for "variable" and some "simple" products.
The idea is the user selects the category from a drop-down list and page refreshes showing the stock for the chosen category.
The problem im having seems to be with the form sending the data to the query.
If I manually code the slug for the category I want everything works fine for variable and simple products. however, when I try to implement the form to Post the category to the query I start getting debug errors below.
[20-Sep-2018 09:52:42 UTC] PHP Fatal error: Uncaught Error: Call to undefined method WC_Product_Simple::get_available_variations() in C:\wamp64\www\devbb.co.uk\wp-content\themes\bb-theme\page-stock.php:79
Stack trace:
#0 C:\wamp64\www\devbb.co.uk\wp-includes\template-loader.php(74): include()
#1 C:\wamp64\www\devbb.co.uk\wp-blog-header.php(19): require_once('C:\\wamp64\\www\\d...')
#2 C:\wamp64\www\devbb.co.uk\index.php(17): require('C:\\wamp64\\www\\d...')
#3 {main}
thrown in C:\wamp64\www\devbb.co.uk\wp-content\themes\bb-theme\page-stock.php on line 79
The part I really don't understand is the fact that when i write the category in myself everything works but when the form tries to pass the same data all the errors appear?
Any help would be really appreciated, thanks
My Code:
<main>
<form id="test" name="test1" method="post">
<select id="cat-select-box" name="amt_per">
<?php
$cat_args = array(
'taxonomy' => "product_cat",
'orderby' => 'slug',
'order' => 'ASC',
'hide_empty' => 1,
);
$cats_select_list = get_terms( 'product_cat', $cat_args );
foreach ($cats_select_list as $select_list){
//if ( strpos($select_list->slug, 'express') || ( strpos($select_list->slug, 'clearance') ) === false) {
echo '<option class="amt-button" name="amt_per" value="' . $select_list->slug . '">' . str_replace ('-', ' ', $select_list->slug) . '</option>';
//}
}
?>
</select>
</form>
<table id="fx_stock_manager">
<?php
$default = 'my-hockey-club-clearance';
$club_cat = isset($_POST['amt_per'])? $_POST['amt_per']: $default;
$query = new WC_Product_Query( array(
//'limit' => 10,
'orderby' => 'title',
'order' => 'ASC',
'return' => 'ids',
//'category' => 'my-hockey-club-clearance',
'category' => $club_cat,
) );
$products = $query->get_products();
foreach ($products as $prod) {
$actual = wc_get_product( $prod );
$variations = $actual->get_available_variations();
foreach ($variations as $key => $value) {
echo '<tr>';
echo '<td><a title="' . $actual->get_sku() . '" href="' . get_permalink($actual->get_id()) . '">' . $actual->get_name() . '</a></td>';
echo '<td>';
foreach ($value['attributes'] as $attr_key => $attr_value) {
$prefix = 'attribute_pa_';
$str = $attr_key;
if (substr($str, 0, strlen($prefix)) === $prefix) {
$str = substr($str, strlen($prefix));
}
echo '<table>';
echo '<tr>';
echo '<td>' . $str . '</td>';
echo '<td>' . $attr_value . '</td>';
echo '</tr>';
echo '</table>';
}
echo '</td>';
echo '<td class="fx_stock_count">' . $value['availability_html'] . '</td>';
echo '</tr>';
}
}
?>
</table>
<script>
$(function() {
$('#cat-select-box').on('change', function(e) {
$(this).closest('form')
.trigger('submit')
})
})
</script>
There are some mistakes in your code since Woocommerce 3… I have also made some little additions: - to keep the selected menu item when item on reload, - added a labeled starting option.
Try the following instead:
Tested and works.