acsonline Posted February 28, 2011 Author Share Posted February 28, 2011 Ah, Well now I realise there is an extra table to check... The SKU is meta_sku in the productmeta table.... then that is passed over to the product_list table using product ID.... I tried the first sql you wrote and that displayed 2x@ etc.. tried the newest sql but had an error show up.. Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1180799 Share on other sites More sharing options...
samoht Posted February 28, 2011 Share Posted February 28, 2011 to make sure the sql works put it directly into your phpmyadmin and replace the table names and the $key value if the sql returns an error let me know what it is BTW, there is no extra table to check - it is just that the sku's are stored on the productmeta table and those are the values that you know. You want the product name and price which is stored on the product_list table and what links them is the product_id. So using INNER JOIN we grab the product_id where the sku is what we have and then pull the name and price from the other table. Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1180800 Share on other sites More sharing options...
acsonline Posted February 28, 2011 Author Share Posted February 28, 2011 Hey, I just ran the system now, and got this: Cannot use object of type stdClass as array in Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1180842 Share on other sites More sharing options...
samoht Posted February 28, 2011 Share Posted February 28, 2011 Opps yes I am sorry you have an object not an array so the code should look like: <?php if (is_page()) { } else { if(!empty($wpdb->prefix)) { $wp_table_prefix = $wpdb->prefix; } else if(!empty($table_prefix)) { $wp_table_prefix = $table_prefix; } // Define the database table names define('WPSC_TABLE_PRODUCT_LIST', "{$wp_table_prefix}wpsc_product_list"); define('WPSC_TABLE_PRODUCTMETA', "{$wp_table_prefix}wpsc_productmeta"); $cf="Linked Products"; $sku = get_post_meta($post->ID, $cf, true); $array = explode(",",$sku); $products = array_count_values($array); foreach($products as $key => $value) { $product = $wpdb->get_row("SELECT meta.product_id, list.name AS name, list.price AS price FROM ".WPSC_TABLE_PRODUCTMETA." AS meta INNER JOIN ".WPSC_TABLE_PRODUCT_LIST." AS list ON ( list.id = meta.product_id ) WHERE `meta_key` IN ( 'sku' ) AND `meta_value` IN ( '{$key}' ) ORDER BY list.id DESC"); echo $value .'x'. $product->name .'@'. $product->price .'<br />'; } } Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1180903 Share on other sites More sharing options...
acsonline Posted March 1, 2011 Author Share Posted March 1, 2011 Wow - that works thank you... Now, is there a similar way to dump all items in to the basket at once? echo $value .' x '. $product->name .' @ £'. $product->price .'<br>'; echo wpsc_add_to_cart_button($product->id) . '<br>'; This code lists one button per product - but I also tried an add all button... Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181163 Share on other sites More sharing options...
samoht Posted March 1, 2011 Share Posted March 1, 2011 I'm not sure about this, but it may work to put each product_id in a hidden input like this: foreach($products as $key => $value) { $product = $wpdb->get_row("SELECT meta.product_id as pid, list.name AS name, list.price AS price FROM ".WPSC_TABLE_PRODUCTMETA." AS meta INNER JOIN ".WPSC_TABLE_PRODUCT_LIST." AS list ON ( list.id = meta.product_id ) WHERE `meta_key` IN ( 'sku' ) AND `meta_value` IN ( '{$key}' ) ORDER BY list.id DESC"); echo $value .' x '. $product->name .' @ '. $product->price .'<br />'; echo '<input type="hidden" name="product_id[]" value="' .$product->pid. '">'; } However, It looks like the wpsc_add_to_cart_button function only processes one product id at a time. (I don't think you want wpsc_add_to_cart_button() anyway since this just shows the add to cart button) The best thing I can think to do is find the function they use to process the form - then we can use that code to populate the cart with multiple products. Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181304 Share on other sites More sharing options...
acsonline Posted March 1, 2011 Author Share Posted March 1, 2011 Ok, I'm looking in to the code to look for the actual function Thanks for your help on this, !!!! Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181311 Share on other sites More sharing options...
acsonline Posted March 1, 2011 Author Share Posted March 1, 2011 I found this in display items functions.... <dt><?php echo __('Add to Cart Shortcode', 'wpsc'); ?>: </dt><dd>[add_to_cart=<?php echo $product_data['id'];?>]</dd> But thats the shortcode generation Is this it? ajax.functions.php function wpsc_add_to_cart() { global $wpdb, $wpsc_cart, $wpsc_theme_path; /// default values $default_parameters['variation_values'] = null; $default_parameters['quantity'] = 1; $default_parameters['provided_price'] = null; $default_parameters['comment'] =null; $default_parameters['time_requested']= null; $default_parameters['custom_message'] = null; $default_parameters['file_data'] = null; $default_parameters['is_customisable'] = false; $default_parameters['meta'] = null; Or function wpsc_add_to_cart_button($product_id, $replaced_shortcode = false) { global $wpdb; if ($product_id > 0){ if(function_exists('wpsc_theme_html')) { $product = $wpdb->get_row("SELECT * FROM ".WPSC_TABLE_PRODUCT_LIST." WHERE id = ".$product_id." LIMIT 1", ARRAY_A); //this needs the results from the product_list table passed to it, does not take just an ID $wpsc_theme = wpsc_theme_html($product); } // grab the variation form fields here $variations_processor = new nzshpcrt_variations; $variations_output = $variations_processor->display_product_variations($product_id,false, false, false); $output .= "<form onsubmit='submitform(this);return false;' action='' method='post'>"; if($variations_output != '') { //will always be set, may sometimes be an empty string $output .= " <p>".$variations_output."</p>"; } $output .= "<input type='hidden' name='wpsc_ajax_action' value='add_to_cart' />"; $output .= "<input type='hidden' name='product_id' value='".$product_id."' />"; $output .= "<input type='hidden' name='item' value='".$product_id."' />"; if(isset($wpsc_theme) && is_array($wpsc_theme) && ($wpsc_theme['html'] !='')) { $output .= $wpsc_theme['html']; } else { $output .= "<input type='submit' id='product_".$product['id']."_submit_button' class='art-button' name='Buy' value='".__('Add To Basket', 'wpsc')."' />"; } $output .= '</form>'; if($replaced_shortcode == true) { return $output; } else { echo $output; } } } Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181316 Share on other sites More sharing options...
samoht Posted March 1, 2011 Share Posted March 1, 2011 ajax.functions.php function wpsc_add_to_cart() { global $wpdb, $wpsc_cart, $wpsc_theme_path; /// default values $default_parameters['variation_values'] = null; $default_parameters['quantity'] = 1; $default_parameters['provided_price'] = null; $default_parameters['comment'] =null; $default_parameters['time_requested']= null; $default_parameters['custom_message'] = null; $default_parameters['file_data'] = null; $default_parameters['is_customisable'] = false; $default_parameters['meta'] = null; This is the function that actually adds items to the cart and what you'll need to use. However, you will need to come up with a way to call this function properly - and it looks as though they are calling it from an ajax script. I assume that you still want a "Add to Cart" button so that the user will see your page with all the ingredients and the quantities for a certain recipe and from that page simply hit the "Add to Cart"? I think we'll have to create a custom function that is triggered by your "Add to Cart" and that will loop through each product to call the function wpsc_add_to_cart() Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181322 Share on other sites More sharing options...
acsonline Posted March 1, 2011 Author Share Posted March 1, 2011 yeah thats what I'd like.... maybe we can create an array of the item number and loop? Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181324 Share on other sites More sharing options...
samoht Posted March 1, 2011 Share Posted March 1, 2011 OK, I am thinking through a way to do this, but I wondered if it wouldn't be better to actually have certain recipes be products themselves? That not only would make the coding much simpler but it would also look nicer on your site. all of the component products and their quantities would then just go into the description field for the recipe and the recipe would have a single cost etc. What do you think? Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181342 Share on other sites More sharing options...
acsonline Posted March 1, 2011 Author Share Posted March 1, 2011 hmmm, I like the idea, but the plan is to sell ingredients - so they can do the shoping but if the recipe takes theire fancy, they clcik buy on the single button, but then they can remove individual items from the basket if they already have them in the cupboard etc... Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181344 Share on other sites More sharing options...
samoht Posted March 1, 2011 Share Posted March 1, 2011 In that case, it still seems that an "add_to_cart_button" per item would be best. That way users don't need to remove items they don't want - they simply don't add them to their cart. Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181349 Share on other sites More sharing options...
acsonline Posted March 1, 2011 Author Share Posted March 1, 2011 very true, but it looks really messy - which is why I was hoping for one button Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181358 Share on other sites More sharing options...
samoht Posted March 1, 2011 Share Posted March 1, 2011 Yea, I understand the look can be a problem, however, that is what CSS is great for. How many ingredients per recipe are we talking about? Can you show me a sample output without the add to cart button ? Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181361 Share on other sites More sharing options...
acsonline Posted March 1, 2011 Author Share Posted March 1, 2011 Well there is 18 on one of the recipes, but I not all the ingredients are in at the moment, however one of the ones has 3 on and that looks terrible Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181380 Share on other sites More sharing options...
acsonline Posted March 2, 2011 Author Share Posted March 2, 2011 Hi, I'm just no further forward on this... any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181823 Share on other sites More sharing options...
samoht Posted March 2, 2011 Share Posted March 2, 2011 Well, I have not been able to test the custom function - and am still thinking that you are now at a styling issue. Because functionally it seems much better for users to not have to remove things from their cart if they don't have to. I threw together a quick CSS example that might make the output display nicer - but really the sky is the limit on how nice you could make this. Notice I removed the button look of the add_to_cart buttons so that it just looks like a text link (cleaner that way I thought), But you could really beautify the table with the add_to_cart buttons being a nice image if you wanted Give this a try: <?php if (is_page()) { } else { if(!empty($wpdb->prefix)) { $wp_table_prefix = $wpdb->prefix; } else if(!empty($table_prefix)) { $wp_table_prefix = $table_prefix; } // Define the database table names define('WPSC_TABLE_PRODUCT_LIST', "{$wp_table_prefix}wpsc_product_list"); define('WPSC_TABLE_PRODUCTMETA', "{$wp_table_prefix}wpsc_productmeta"); $cf="Linked Products"; $sku = get_post_meta($post->ID, $cf, true); $array = explode(",",$sku); $products = array_count_values($array); function tablestyles() { $html = ' <style type="text/css"> .recipe { border-collapse: collapse;table-layout:fixed;width:440px; font-size:small; text-align:center; } .recipe td { border: 1pt solid #ddd; padding:0 3px;} } input.wpsc_buy_button { font-size:10px; background-color:#ffffff; border-style:none; border-color:#ffffff; width:60px; } </style>'; return $html; } add_action('wp_head', 'tablestyles'); ?> <table class="recipe"> <th><td>Quantity</td><td>Product</td><td colspan="2">Price:</td></th> <?php foreach($products as $key => $value) { $product = $wpdb->get_row("SELECT meta.product_id as pid, list.name AS name, list.price AS price FROM ".WPSC_TABLE_PRODUCTMETA." AS meta INNER JOIN ".WPSC_TABLE_PRODUCT_LIST." AS list ON ( list.id = meta.product_id ) WHERE `meta_key` IN ( 'sku' ) AND `meta_value` IN ( '{$key}' ) ORDER BY list.id DESC"); echo '<tr><td>'.$value .' x </td><td>'. $product->name .' @ </td><td> £'. $product->price .'</td><td>'. wpsc_add_to_cart_button($product->pid) .'</td></tr>'; } ?> </table> <?php } Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1181887 Share on other sites More sharing options...
acsonline Posted March 3, 2011 Author Share Posted March 3, 2011 Hi, Thanks for your help, I know what you are saying but I really think I want it to add all the ingredients, so I just need to work out how to display one button to add all products Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1182277 Share on other sites More sharing options...
acsonline Posted March 14, 2011 Author Share Posted March 14, 2011 Hi, Does anybody have any ideas how to get this to generate? I just need to click on one button and add all the products to the shopping cart. This is the function to run add to cart: function wpsc_add_to_cart_button($product_id, $replaced_shortcode = false) { global $wpdb; if ($product_id > 0){ if(function_exists('wpsc_theme_html')) { $product = $wpdb->get_row("SELECT * FROM ".WPSC_TABLE_PRODUCT_LIST." WHERE id = ".$product_id." LIMIT 1", ARRAY_A); //this needs the results from the product_list table passed to it, does not take just an ID $wpsc_theme = wpsc_theme_html($product); } // grab the variation form fields here $variations_processor = new nzshpcrt_variations; $variations_output = $variations_processor->display_product_variations($product_id,false, false, false); $output .= "<form onsubmit='submitform(this);return false;' action='' method='post'>"; if($variations_output != '') { //will always be set, may sometimes be an empty string $output .= " <p>".$variations_output."</p>"; } $output .= "<input type='hidden' name='wpsc_ajax_action' value='add_to_cart' />"; $output .= "<input type='hidden' name='product_id' value='".$product_id."' />"; $output .= "<input type='hidden' name='item' value='".$product_id."' />"; if(isset($wpsc_theme) && is_array($wpsc_theme) && ($wpsc_theme['html'] !='')) { $output .= $wpsc_theme['html']; } else { $output .= "<input type='submit' id='product_".$product['id']."_submit_button' class='art-button' name='Buy' value='".__('Add To Basket', 'wpsc')."' />"; } $output .= '</form>'; if($replaced_shortcode == true) { return $output; } else { echo $output; } } } Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1187319 Share on other sites More sharing options...
acsonline Posted March 15, 2011 Author Share Posted March 15, 2011 Hey, Have you got any ideas how to run this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/228684-trouble-with-array/page/2/#findComment-1187820 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.