Jump to content

Recommended Posts

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

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.

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 />';
}
}

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

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.

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;
 	}
} 
}

 

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

 

 

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?

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

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	
}

  • 2 weeks later...

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;
 	}
} 
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.