jarvis
Members-
Posts
543 -
Joined
-
Last visited
Everything posted by jarvis
-
Thanks @requinix Unfortunately, this stems from a Wordpress build and the code is generated by a plugin, so its beyond my control.
-
HI All, I'm hoping someone can help! I need to either remove or change a DIV tag directly above another. Here's the code: <div style="clear:both;"></div> <div class="berocket_lgv_widget" style="float:left;padding: 5px 0px 5px 0px;"> Either the entire first DIV needs removing or change the style to clear:none I thought something like: $(".berocket_lgv_widget").prev().attr("style", " clear:none"); $(".berocket_lgv_widget").prev().remove(); No joy with either. I also tried before() Any help is much appreciated Thanks
-
Apologies, why is it by posting I managed to work these things out Each of my posts when created gets the company ID assigned. So rather than query post by author ID, I queried for all posts linked by company ID This worked! More so because the tests I were doing were based on posts written by the primary user ID, not any other user IDs with the same company ID Problem solved!
-
Hi All, I really hope someone can help me on this. Basically, I'm trying to disable a bunch of accounts linked by a custom field value of company_id. It toggles a custom field of enable_account from 1 to 0 - this part works fine. What I also need to do, is once the user is disabled, locate any posts by the user (author ID) and set it from publish to draft. Now, this is oddly the part not working! If I dump the code out in any file its fine, however, once I put the exact code back to my functions file, it fails. So I have the disable link on a template like so: <a href="#" data-id="<?php echo $user_info->ID; ?>" data-nonce="<?php echo wp_create_nonce('my_disable_account_nonce') ?>" class="disable-account">Disable</a> It s handled by this: $('.disable-account').click(function(){ if (confirm('Are you sure you want to disable this account?')) { var action = 'disable_account'; var id = $(this).data('id'); var nonce = $(this).data('nonce'); //alert('ID: '+id+' Nonce: '+nonce); $.ajax({ type : "POST", data : { action:action, id:id, nonce:nonce}, dataType : "html", url : '<?php echo admin_url('admin-ajax.php');?>', success : function(data) { //alert(this.data); jQuery("#table_data").html(data); console.log("action:" + action + " id: " + id + " nonce: " + nonce); //debug //window.location.reload(); }, error : function(xhr, status, error) { var err = eval("(" + xhr.responseText + ")"); alert(err.Message); } }); } }); Finally, here's the function: function disable_account() { $permission = check_ajax_referer( 'my_disable_account_nonce', 'nonce', false ); if( $permission == false ) { echo '<td colspan="7">Error!</td>'; } else { $response = ''; // Get the company ID $company_id = get_field( 'company_id', 'user_'.$_REQUEST['id'] ); $response .= '<p>Company ID: '.$company_id.'</p>'; // Update the main user update_user_meta( $_REQUEST['id'], 'enable_account', '0' ); $response .= '<p>Primary User ID: '.$_REQUEST['id'].'</p>'; $user_args = array( 'role__in' => array('subscriber', 'staff_member'), 'orderby' => 'meta_value', 'meta_key' => 'company_name', 'order' => 'ASC', 'meta_query'=> array( array( 'relation' => 'AND', # OR array( 'key' => 'enable_account', 'value' => '1', 'compare' => "=", ), array( 'key' => 'company_id', 'value' => $company_id, 'compare' => "=", ) ) ) ); $users = get_users( $user_args ); if ($users): foreach ( $users as $user ) : $user_info = get_userdata( $user->ID ); // Update the main user update_user_meta( $user->ID, 'enable_account', '0' ); #echo '<p>User ID: '.$user->ID.'</p>'; $response .= '<p>User ID: '.$user->ID.'</p>'; global $post; // Get all posts by author ID $myposts = get_posts( array( 'post_status' => 'publish', 'posts_per_page'=> -1, 'author' => $user->ID )); if ( $myposts ) { foreach ( $myposts as $post ) : setup_postdata( $post ); $response .= '<p>Post ID: '.$post->ID.'</p>'; endforeach; wp_reset_postdata(); } endforeach; else: endif; echo $response; } die(); } // Fire AJAX action for logged in users only add_action('wp_ajax_disable_account', 'disable_account'); As I say, using this in an ordinary template works. For now, I've removed the code to change the post from publish to draft as at this stage, its not even displaying the Post ID, so I know its not even getting this far! Have I missed something obvious? Thanks
-
Thanks @requinix It's usually done this way due to the number of redirects and from an admin point of view (someone else maintains it). I thought adding both functions would cater for trailing slash and non-trailing slash I'll review it again
-
Hi All, Am having a "can't see the wood for the trees" moment I have a php redirect file which contains my URLs, like so: return [ '/shop/' => '/products/', ]; I then have a functions file which contains: function 301_redirects(){ $redirects = include __DIR__ . '/redirects.php'; if (isset($redirects[$_SERVER['REQUEST_URI']])) { header("HTTP/1.1 301 Moved Permanently"); header('location: ' . $redirects[$_SERVER['REQUEST_URI']]); exit; } if (isset($redirects[rtrim($_SERVER['REQUEST_URI'], '/')])) { header("HTTP/1.1 301 Moved Permanently"); header('location: ' . $redirects[rtrim($_SERVER['REQUEST_URI'], '/')]); exit; } } If the URL is https://www.mydomain.com/shop then the redirect won't work If the URL is https://www.mydomain.com/shop/ then the redirect works What am I missing? Thanks
-
Thank you Barand, I shall digest this today!
-
I "think" I may have sussed this: add_filter( 'gform_pre_render_2', 'applicant_2_previous_address_history' ); add_filter( 'gform_pre_validation_2', 'applicant_2_previous_address_history' ); function applicant_2_previous_address_history( $form ) { #get the value from the form date input d/m/Y so need to change $get_moved_in_date = rgpost( 'input_1' ); if ( $get_moved_in_date ) { #convert the date format $moved_in_date = date_format(date_create_from_format('d/m/Y', $get_moved_in_date), 'd-m-Y'); #echo "New date format is: ".$moved_in_date. "<br/>"; } #get todays date $today = date("d-m-Y"); #echo "<h3>Today: ".$today."</h3>"; #calculate the difference in days $dateDiff = dateDiffInDays($today, $moved_in_date); #printf("<p>Difference between two dates: " . $dateDiff . " Days</p>"); if ( $dateDiff < 1095 ) { #$flag = 'true'; # get the input from the list $list_values = rgpost( 'input_2' ); $i = 0; #use this to get even rows only due to the way data is stored :( $sum = 0; #use this to increment the no. of rows in the list $previous = null; #use this to calc date difference between rows $days_sum = $dateDiff; #set the total number of days. Don't start at 0, start from current date - time at current residence if ( $list_values ) { foreach ( $list_values as $key => $value ){ #get even rows only if($i%2 == 0){ $sum = $sum + 1; #increment our rows if ( $value ) { #convert the date format $additional_date = date_format(date_create_from_format('d/m/Y', $value), 'd-m-Y'); echo "<h3>Date: ".$additional_date."</h3>"; } if ( $previous !== null ){ # if it's null, we're in the first loop #convert the date format $previous_date = date_format(date_create_from_format('d/m/Y', $previous), 'd-m-Y'); $dateDiff2 = dateDiffInDays($additional_date, $previous_date); printf("<p>Next Iteration Difference between " . $additional_date . " and " . $previous_date . " is: " . $dateDiff2 . " Days</p>"); $days_sum = $days_sum + $dateDiff2; } else { #convert the date format $dateDiff1 = dateDiffInDays($additional_date, $moved_in_date); printf("<p>1st Iteration Difference between " . $additional_date . " and " . $moved_in_date . " is: " . $dateDiff1 . " Days</p>"); $days_sum = $days_sum + $dateDiff1; } $previous = $value; # set the current value, so it will be saved for the next iteration } $i++; } #end foreach $list_values } #endif $list_values #$sum = $sum + 1; #increment our rows echo "<p>Total no of days = " .$days_sum.'</p>'; if ( $days_sum < 1095 ) { echo '<p>Not enough days so loop through list</p>'; $sum = $sum + 1; #increment our rows $flag = 'true'; } } else { echo '<p>Were ok!</p>'; $flag = 'false'; } #we moved in less than 3 years ago, so get history if ( $flag == 'false' ) { return $form; } echo "<p>The sum of array element is = " .$sum.'</p>'; foreach ( $form['fields'] as &$field ) { if ( $field->id == 2 ) { $field->isRequired = true; } if ( $field->id == 3 ) { $_POST['input_3'] = $sum; } } return $form; } From initial testing, this now seems to work!
-
Hi All, I'm hoping someone can help as I've got myself in a muddle on some code. Basically, the form has a date field (date moved in). If the date entered is over 3 years old, all's ok However, if the date entered is less than 3 years old, the user needs to complete a list field. The list field is essentially a repeater field with a date and address field. If the user adds a date and address and this (plus the original date) is more than 3 years combined history - happy days! If the user adds a date and address and they've still not hit 3 years history, then I need to increase the number of rows (this is handled by another function which works by incrementing a field on the form. As it stands, it works until I need another field to appear i.e. they've entered the first date, then entered a date within the list/repeater field but still not got 3 years worth. I think it's more or less there but seem to have muddled some of the logic (I think/hope!). A fresh pair of eyes would be great: Here's my code: function dateDiffInDays($date1, $date2) { # Calulating the difference in timestamps $diff = strtotime($date2) - strtotime($date1); # 1 day = 24 hours # 24 * 60 * 60 = 86400 seconds return abs(round($diff / 86400)); } add_filter( 'gform_pre_render_2', 'applicant_2_previous_address_history' ); add_filter( 'gform_pre_validation_2', 'applicant_2_previous_address_history' ); function applicant_2_previous_address_history( $form ) { #get the value from the form date input d/m/Y so need to change $get_moved_in_date = rgpost( 'input_1' ); if ( $get_moved_in_date ) { #convert the date format $moved_in_date = date_format(date_create_from_format('d/m/Y', $get_moved_in_date), 'd-m-Y'); #echo "New date format is: ".$moved_in_date. "<br/>"; } #get todays date $today = date("d-m-Y"); #echo "<h3>Today: ".$today."</h3>"; #calculate the difference in days $dateDiff = dateDiffInDays($today, $moved_in_date); #printf("<p>Difference between two dates: " . $dateDiff . " Days</p>"); #check if we moved in less than 3 years ago if ( $dateDiff < 1095 ) { #less than 3 years, so need to loop through the list fields, compare dates and tally totals days if ( $days_sum < 1095 ) { # get the input from the list $list_values = rgpost( 'input_2' ); $i = 0; #use this to get even rows only due to the way data is stored :( $sum = 0; #use this to increment the no. of rows in the list $previous = null; #use this to calc date difference between rows $days_sum = $dateDiff; #set the total number of days. Don't start at 0, start from current date - time at current residence if ( $list_values ) { foreach ( $list_values as $key => $value ){ #get even rows only if($i%2 == 0){ #$sum = $sum + 1; #increment our rows if ( $value ) { #convert the date format $additional_date = date_format(date_create_from_format('d/m/Y', $value), 'd-m-Y'); #echo "<h3>Date: ".$additional_date."</h3>"; } if ( $previous !== null ){ # if it's null, we're in the first loop #convert the date format $previous_date = date_format(date_create_from_format('d/m/Y', $previous), 'd-m-Y'); $dateDiff2 = dateDiffInDays($additional_date, $previous_date); printf("<p>Next Iteration Difference between " . $additional_date . " and " . $previous_date . " is: " . $dateDiff2 . " Days</p>"); $days_sum = $days_sum + $dateDiff2; } else { #convert the date format $dateDiff1 = dateDiffInDays($additional_date, $moved_in_date); printf("<p>1st Iteration Difference between " . $additional_date . " and " . $moved_in_date . " is: " . $dateDiff1 . " Days</p>"); $days_sum = $days_sum + $dateDiff1; } $previous = $value; # set the current value, so it will be saved for the next iteration } $i++; } #end foreach $list_values } #endif $list_values $sum = $sum + 1; #increment our rows echo "<p>Total no of days = " .$days_sum.'</p>'; } #endif $days_sum < 1095 echo '<p>Not enough days so loop through list</p>'; $flag = 'true'; } else { echo '<p>Were ok!</p>'; $flag = 'false'; } #endif $dateDiff < 1095 /* if ( $dateDiff < 1095 ) { $value = 'yes'; } else { $value = 'no'; } */ #we moved in less than 3 years ago, so get history if ( $flag == 'false' ) { return $form; } foreach ( $form['fields'] as &$field ) { if ( $field->id == 2 ) { $field->isRequired = true; } echo "<p>The sum of array element is = " .$sum.'</p>'; if ( $field->id == 3 ) { $_POST['input_3'] = $sum; } } return $form; } Thanks
-
Hi All, I have a Bootstrap (v4) carousel. It has numerous slides but one slide has a video. For example: <div class="carousel-item" data-interval="10000"> <video id="player" loop> <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4"><br> Your browser does not support the video tag. </video> <div class="carousel-caption d-md-block"> <h3 data-animation="animated flipInX"> This is the caption for slide 4 </h3> </div> </div> What I'm trying to do, is set the video to play only when you're on that slide (active) and pause when you either scroll away (via a click on the carousel arrows/indicators or the carousel interval). Therefore, I will use the event functionality (https://getbootstrap.com/docs/4.0/components/carousel/#events). As I'm using HTML 5 video tags, this is my solution: $('#carousel').carousel().on('slide.bs.carousel',function(){ $('.carousel-item').find('video').each(function(){ this.pause(); }); }); $('#carousel').carousel().on('slide.bs.carousel',function(){ $('.carousel-item active').find('video').each(function(){ this.play(); }); }); Unfortunately, it doesn't seem to work but I can't see why? Am I missing something very obvious?? Many thanks in advanced
-
HI @Barand Is it possible to ask one more thing? In your very kind example, you have: $headings = [ 'attribute_pa_colour' => 'Colour', 'attribute_pa_description' => 'Description', 'attribute_pa_material' => 'Material', 'attribute_pa_pack-quantity' => 'Pack Quantity', 'attribute_pa_size' => 'Size', 'attribute_pa_system' => 'System', 'attribute_pa_variation' => 'Variation', 'variation_description' => 'Description', 'sku' => 'SKU', 'variation_id' => 'ID', 'price_html' => 'Price' ]; echo '<pre>', print_r($headings), '</pre>'; And if you print his out, you would see: Array ( [attribute_pa_colour] => Colour [attribute_pa_description] => Description [attribute_pa_material] => Material [attribute_pa_pack-quantity] => Pack Quantity [attribute_pa_size] => Size [attribute_pa_system] => System [attribute_pa_variation] => Variation [variation_description] => Description [sku] => SKU [variation_id] => ID [price_html] => Price ) I thought I'd try and automate this, so have the following code: $attribute_taxonomies = wc_get_attribute_taxonomies(); if ( $attribute_taxonomies ) : $taxonomyOfInterest = array(); foreach ($attribute_taxonomies as $tax=>$tax_value) : $attribute = 'attribute_pa_'.$tax_value->attribute_name; $name = 'pa_'.$tax_value->attribute_name; $label = wc_attribute_label( $name ); $taxonomyOfInterest[] = $attribute.' => '.$label.','; endforeach; endif; array_push($taxonomyOfInterest,"'variation_description' => 'Description',", "'sku' => 'SKU',", "'variation_id' => 'ID',", "'price_html' => 'Price'"); echo '<pre>', print_r($taxonomyOfInterest), '</pre>'; But when I print this out, I get: Array ( [0] => attribute_pa_colour => Colour, [1] => attribute_pa_description => Description, [2] => attribute_pa_material => Material, [3] => attribute_pa_pack-quantity => Pack Quantity, [4] => attribute_pa_size => Size, [5] => attribute_pa_system => System, [6] => attribute_pa_variation => Variation, [7] => 'variation_description' => 'Description', [8] => 'sku' => 'SKU', [9] => 'variation_id' => 'ID', [10] => 'price_html' => 'Price' ) Apologies for the terminology but how do I output this as you have it I guess without an index but arrays always need an index, right?
-
Well that sounds jolly interesting and not something i've done before. I'll go and look into it Thanks again
-
Hi @Barand Sorry for the delay replying. Thank you so, so much! That's absolutely bloody brilliant I think I can work out how to remove the bits and bobs I don't need to display (image_id, is_in_stock etc.) Thank you once again!
-
Hi @Barand Mine looks like this: Array ( [attribute_pa_pack-quantity] => [dimensions] => [dimensions_html] => [image] => [image_id] => [is_in_stock] => [is_purchasable] => [is_sold_individually] => [min_qty] => [sku] => [variation_id] => [variation_is_active] => [variation_is_visible] => [weight_html] => ) I did remove a couple of items from the outset to tidy the initial array output (very first post). I'm sorry, I thought it would make life easier but wonder if I've now muddled the situation in doing so!? Otherwise it was rather lengthy: Array ( [0] => Array ( [attributes] => Array ( [attribute_pa_system] => system-1 [attribute_pa_pack-quantity] => 1-x-3m ) [availability_html] => [backorders_allowed] => [dimensions] => Array ( [length] => [width] => [height] => ) [dimensions_html] => N/A [display_price] => 0 [display_regular_price] => 0 [image] => Array ( [title] => xt1cwh_web_600 [caption] => [url] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [alt] => [src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-600x600.png [srcset] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png 600w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png 400w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-150x150.png 150w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-250x250.png 250w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-120x120.png 120w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png 100w [sizes] => (max-width: 600px) 100vw, 600px [full_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [full_src_w] => 600 [full_src_h] => 600 [gallery_thumbnail_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png [gallery_thumbnail_src_w] => 100 [gallery_thumbnail_src_h] => 100 [thumb_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png [thumb_src_w] => 400 [thumb_src_h] => 400 [src_w] => 600 [src_h] => 600 ) [image_id] => 719 [is_downloadable] => [is_in_stock] => 1 [is_purchasable] => 1 [is_sold_individually] => no [is_virtual] => [max_qty] => [min_qty] => 1 [price_html] => [sku] => XT1CWH [variation_description] => [variation_id] => 199 [variation_is_active] => 1 [variation_is_visible] => 1 [weight] => [weight_html] => N/A ) [1] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 1-x-3m ) [availability_html] => [backorders_allowed] => [dimensions] => Array ( [length] => [width] => [height] => ) [dimensions_html] => N/A [display_price] => 0 [display_regular_price] => 0 [image] => Array ( [title] => xt1cwh_web_600 [caption] => [url] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [alt] => [src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-600x600.png [srcset] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png 600w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png 400w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-150x150.png 150w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-250x250.png 250w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-120x120.png 120w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png 100w [sizes] => (max-width: 600px) 100vw, 600px [full_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [full_src_w] => 600 [full_src_h] => 600 [gallery_thumbnail_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png [gallery_thumbnail_src_w] => 100 [gallery_thumbnail_src_h] => 100 [thumb_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png [thumb_src_w] => 400 [thumb_src_h] => 400 [src_w] => 600 [src_h] => 600 ) [image_id] => 719 [is_downloadable] => [is_in_stock] => 1 [is_purchasable] => 1 [is_sold_individually] => no [is_virtual] => [max_qty] => [min_qty] => 1 [price_html] => [sku] => XT2CWH [variation_description] => [variation_id] => 200 [variation_is_active] => 1 [variation_is_visible] => 1 [weight] => [weight_html] => N/A ) [2] => Array ( [attributes] => Array ( [attribute_pa_system] => system-3 [attribute_pa_pack-quantity] => 1-x-3m ) [availability_html] => [backorders_allowed] => [dimensions] => Array ( [length] => [width] => [height] => ) [dimensions_html] => N/A [display_price] => 0 [display_regular_price] => 0 [image] => Array ( [title] => xt1cwh_web_600 [caption] => [url] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [alt] => [src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-600x600.png [srcset] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png 600w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png 400w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-150x150.png 150w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-250x250.png 250w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-120x120.png 120w, https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png 100w [sizes] => (max-width: 600px) 100vw, 600px [full_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600.png [full_src_w] => 600 [full_src_h] => 600 [gallery_thumbnail_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-100x100.png [gallery_thumbnail_src_w] => 100 [gallery_thumbnail_src_h] => 100 [thumb_src] => https://www.domain.co.uk/wp-content/uploads/2019/01/xt1cwh_web_600-400x400.png [thumb_src_w] => 400 [thumb_src_h] => 400 [src_w] => 600 [src_h] => 600 ) [image_id] => 719 [is_downloadable] => [is_in_stock] => 1 [is_purchasable] => 1 [is_sold_individually] => no [is_virtual] => [max_qty] => [min_qty] => 1 [price_html] => [sku] => XT3CWH [variation_description] => [variation_id] => 201 [variation_is_active] => 1 [variation_is_visible] => 1 [weight] => [weight_html] => N/A ) ) Apologies if this completely screws things!?
-
What would cause: Notice: Array to string conversion in... line 93 This refers to echo "<div class='hdg'>{$headings[$h]}</div>$v<br>\n";
-
Thank you so much @Barand for all of your help!
-
Thanks @Barand I could do that, then use str replace to tidy it up - not ideal but a workable solution, what do you think? As I say, I could do with a pointer as to how to display the table though if possible? Sorry for asking! As a thought, the heading would always be the first attribute, so in theory would be 'attribute_pa_system'
-
Thanks @Barand Hmm, I hadn't given that a thought! If i'm honest, i'm struggling a little as to how to output this anyway I hate arrays or they hate me, haven't decided which
-
HI @Barand Sorry to trouble you further. If you have the array coming in with: ( [sku] => XT1ECWH [var_id] => 234 [variation] => n-a [pack_qty] => 2 ) Within your code, you currently have: $systems[$var['attributes'][0]][] = [ 'sku' => $var['sku'], 'var_id' => $var['variation_id'] ?? '', 'pack_qty' => $var['attributes'][1], 'variation' => $var['attributes'][2] ]; Which is utterly brill, however, from product to product, the attributes may differ (some have more, some have less, some are different). For example: ( [sku] => XT1ECWH [var_id] => 234 [variation] => n-a [colour] => black [pack_qty] => 2 [material] => plastic ) Is there anyway to dynamically create the internal part? $systems[$var['attributes'][0]][] = [ 'sku' => $var['sku'], 'var_id' => $var['variation_id'] ?? '', //some sort of loop here to gather the below? // 'pack_qty' => $var['attributes'][1], 'variation' => $var['attributes'][2] ]; Is that even possible?
-
Ah it's because not every product has that specific attribute Is there a way to add a conditional check within the array?
-
Thank you so much! I get a notice of Notice: Undefined offset: 2 which relates to this line: 'variation' => $var['attributes'][2], so now trying to work out what may be causing this Thank you again for all your help
-
Thanks @Barand The variations (attributes) are created within a CMS. So there are various ones: System Pack Quantity Variation Colour Then each attribute has related terms, for example: System - System 1, System 2 etc. Pack Quantity - 1,2,3,10 etc. Variation: Left, Right Colour - Black, White, Grey When a product's created, you specify which Attributes you want and select the necessary variations So $variations then contains the necessary product variations As products differ with which option is first, it'd be ideal not to use a name you see, hence wondering whether you can use the [0] to denote the first attribute instead - I hope that makes sense!?
-
Hi @Barand Thank you for your reply (as always!). May I ask, what does the double question mark mean ('var_id' => $var['variation_id'] ?? '',) Also, as the name is unknown from product to product, rather than a name like $systems[$var['attributes']['attribute_pa_system']] is it possible use a number like: $systems[$var['attributes'][0]] Thanks again
-
Hi All, I hope i'm posting in the right place but also hope someone can help! I've got the following code: <?php $variations = $product->get_available_variations(); if ($variations): ?> <div class="table-responsive"> <table class="table table-striped"> <tbody> <?php foreach ($variations as $key => $value) : ?> <tr> <td><?php echo $value['sku']; ?> <?php echo $value['variation_id']; ?></td> <?php foreach ($value['attributes'] as $attrKey => $attr) : $tax = str_replace('attribute_', '', $attrKey); $term_obj = get_term_by('slug', $attr, $tax); ?> <td><?php echo $term_obj->name; ?></td> <?php endforeach; ?> </tr> <?php endforeach; #$variations?> </tbody> </table> </div><!-- /table-responsive --> <?php endif; #$variations ?> This produces the following table: Product code System Pack Quantity Variation XT1ECWH 234 System 1 2 N/A XT2ECLWH 236 System 2 2 Left XT2ECRWH 237 System 2 2 Right XT3ECWH 238 System 3 2 N/A However, is it possible to alter the code to group the information? So it looks like the below? System 1 System 2 System 3 System 1 XT1ECWH 234 Pack Quantity: 2 Variation: N/A System 2 XT2ECLWH 236 Pack Quantity: 2 Variation: Left XT2ECRWH 237 Pack Quantity: 2 Variation: Right System 3 Sorry, not sure why it's not formatted correctly but hopefully you can see what I'm trying to achieve. The array for $variations is as follows: Array ( [0] => Array ( [attributes] => Array ( [attribute_pa_system] => system-1 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [sku] => XT1ECWH [variation_description] => [variation_id] => 234 ) [1] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => left ) [sku] => XT2ECLWH [variation_description] => Left [variation_id] => 236 ) [2] => Array ( [attributes] => Array ( [attribute_pa_system] => system-2 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => right ) [sku] => XT2ECRWH [variation_description] => Right [variation_id] => 237 ) [3] => Array ( [attributes] => Array ( [attribute_pa_system] => system-3 [attribute_pa_pack-quantity] => 2 [attribute_pa_variation] => n-a ) [price_html] => [sku] => XT3ECWH [variation_description] => ) ) If it helps, I need to always group by the first attribute, in this case : attribute_pa_system Any help on this would be very much appreciated! Thank you in advanced.
-
Thanks @Barand! God I feel so stupid!!! :-(