Jump to content

Same product options for all products query

red sun

Recommended Posts

Please bear with me on this one...


I'm developing a site for a client with over 3,000 products with the same 5 dependent select boxes (eg: click option 3 from select #1 - select #4 becomes visible) with approximately 10 differently-priced options each per select box.


I've spent days writing over 500 lines of jQuery just to handle the visibility of the dependent select boxes and options and also provide a running total on the product detail page.


My biggest issue is with the way CubeCart assigns product options. I can create option sets to quickly assign all the options to each product but the option prices are added per product rather than stored with the master option. If my client wants to import the basic product information from a CSV from their old site they are going to have to manually add prices for over 50 options to 3,000 products.


I can clone a single completed product with option prices in place and amend product name, price, description, seo meta info etc per product but this means that importing a CSV isn't possible.


In addition, I dread to think of trying to update 3,000 x 50 product option prices next year.  A basic search and replace on the database won't do the job as some option prices are currently the same but may need to differ in the future.


I'd love to be able to bypass the lengthy product options in the admin area and hard-code the options as tick boxes (or selects, radio buttons - whatever suits) into "content.product.php" as they are the same for every single product, and then use jQuery or similar to set 5 text field option values to hold the selected product option text (eg: "option 4: +£5.00" etc) so it gets stored with the order  - this bit I can do.


However I'd also need to be able to pass a total for the product option prices to the "view basket" page so that the total price on the "view basket" page is correct and it gets stored with the order (eg: item price is £100, total of product options is £30 so the product total price on the "view basket" page is £130).


This would make it so much easier to create the initial product inventory and update prices in the future as all I'd have to change are the hard-coded values in "content.product.php".


Is there a means of having a product option text field which could pass a variable numeric value (the total of the product options) to be used in the calculation on the "view basket" page and which would also be stored with the order.


This scenario hasn't been easy to describe - I appreciate your patience if you've got to the end.


Many thanks


Link to comment
Share on other sites

"I've spent days writing over 500 lines of jQuery..."


You are doing better than me. I've spent over 500 days of expending serious brain power trying to come up with a solution.


Let me read over your post a few times and I'll get back to you.

Link to comment
Share on other sites

"I need to be able to pass a total for the product option prices to the "view basket" page so that the total price on the "view basket" page is correct and it gets stored with the order (eg: item price is £100, total of product options is £30 so the product total price on the "view basket" page is £130)."


If what you have to give back to CubeCart is a javascript-powered sum-total for the options selected, that is probably do-able.


The incredibly neat thing about the basket is that you can put anything you want into it. (This is different from the Cart. If I forget to explain it, remind me.)


Let's start with the Cart class, add() method. After some preliminaries, the place where the item gets added to the basket happens at line 328 (for CC5213). And specifically line 342 when adding a product instead of adding another of the exact same thing.


$this->basket['contents'] is an array indexed with a hash value that uniquely identifies the product with chosen option combo.


Earlier, at line 285, CubeCart goes through the process of fetching the product data from the database, calculating the best price but without dealing with the options.


Eventually we get to line 409 which offers us a hook, 'class.cart.add.save', to add into the basket whatever we want.


A snippet can be written to use the value of $_POST['my_options_total']. The value in this field is your javascript-powered sum-total of the options price differentials (e.g., "option 4: +£5.00"). The snippet will assign the value to $this->basket['contents'][$hash]['my_options_total'].


Now we have it in the basket. Note that no calculations have been performed at this time.


The next steps are to make sure CubeCart keeps using this basket element and to discard the upcoming (re)calculations of the total from the CC_options_assign database table.


More to follow.

Link to comment
Share on other sites

Many thanks - I had been looking at "cart.class.php" for clues to how to pass a single jQuery-calculated total for all options but it's beyond my limited programming ability, especially when it comes to storing it with the order.


Really appreciate your input.

Link to comment
Share on other sites

Well, it turns out that the code where we need to make our interdiction isn't coded to let us do that easily. Obstacle #1 is that the option's total cost is summed directly into the product's price and is then discarded. (I hate that kind of programming!) Thus, once out of the loop, we would have to recreate the means of calculating the option's total cost for the item, subtract it from the item's price, and add in your own.


Or we would need to make some hard edits to the code so that the option's total cost becomes part of the item's array of properties. Then, when out of the loop, we can simply subtract the option's total cost and add your own.


So, if you are fine with hard edits, I will come up with something shortly. Or, if you want to stick with using hooks, I can do that too.

Link to comment
Share on other sites

I'm not familiar with implementing hooks, but understand that this would probably be preferable to hard edits which would need to be replicated whenever I carry out a CC update.


I'm not overly worried that the option costs are summed into the product's total cost, as long as the value of {$CART_TOTAL} in box.basket.php also remains accurate. I can create an "options total" text field option and write the calculated option total in via jQuery to store the value with the product order. It's the value from this field that we'd want added to the product's original price (or sale price) in any case.


I'm more than happy to implement the simplest solution you can find.

Link to comment
Share on other sites

So far, I've worked out the simplest recalculation for the Shopping Basket sidebox. That means, if taxes are complicated, or there is a discount based on the subtotal, the math might get a bit off.


Create a snippet for 'class.cart.add.save'. The contents are:

// Uses the 'class.cart.add.save' hook
// Specific to the View Product page's 'Add to basket' button
// Expects the custom javascript-powered calculated options total
//   to be in a form element with name="my_options_total"
// Saves that value in the basket's contents for that product
  $this->basket['contents'][$hash]['my_options_total'] = $_POST['my_options_total'];


Create a snippet for 'class.cart.get'. The contents are:

// Uses the 'class.cart.get' hook
// After all of the (less than ideally programmed) calculations are made,
//   subtracts the options total (queried from the database), and
//   adds the custom options total that may be in the basket for that product
//   If not, the recalculation is ignored
  foreach($this->basket_data as $hash => $product){
      $this->basket_data[$hash]['price_display'] =
        - $this->basket['contents'][$hash]['option_line_price']
        + $this->basket['contents'][$hash]['my_options_total'];

Again, this has only been shown to work on the Shopping Basket sidebox under the simplest of conditions.


The next step is to do the calculations for the View Basket and checkout/gateway processes.

Link to comment
Share on other sites

Many thanks for this. I think it might be best for me to work up an example of the product detail page so you can see what I'm trying to create.


I've backed up my previous version which used the complex product options stored in admin. Please bear with me, it will take a while to re-write the jQuery and set up the new text-field only product options.

Link to comment
Share on other sites

  • 1 month later...

I've finally managed to re-write the entire jQuery for the product options, using option text fields to store the customer's options rather than select drop-downs populated from the CubeCart admin area. This means I can now import a product CSV for the 3,000+ products and it will also make the task of updating the options and prices each year a lot simpler - I just have to update my jQuery script and the hard-coded values in "content.product.php".


I have a product option text field on the page ( name="productOptions[24][]" ) which is storing the product options total cost with the order, but had to revert to using an additional field as suggested by bsmither - "my_options_total" - as the square brackets assigned by CubeCart to the field name appeared to be causing a problem with the snippet scripts.


bsmither's snippets for using a text field to calculate the Shopping Basket sidebox values are working perfectly - see the example site at: http://framedindulgence.redsundesign.co.uk (the Shopping Basket sidebox is in the masthead area). You have to select a prescription lens to trigger the options (single, bi-focal, vari-focal).


I have left the product options text fields which are getting stored with the order as visible for now (under the product image) but I'll hide these on the final version. I only had them visible to easily verify the values being written to them. Likewise, the test calculation fields under the "buy" button will also be hidden in the final version.


I'd really appreciate help with getting the Shopping Basket sidebox values into the checkout. Would it help that we can make reference to the value stored with the order in "productOptions[24][]" ?


Many thanks again...

Link to comment
Share on other sites

  • 3 weeks later...

Hi again. Is there a potential work-around if it's not possible to provide a hook for the calculations for the View Basket and checkout/gateway processes?


One thing I thought of is creating a product option which contains the values for all the possible product option totals (from 1 to 300 or so). I could set the value of the product option drop-down using JavaScript and it's value would then be stored with the order and used for calcuating the total price. It's a bit convoluted and the big draw-back with this is the fact that Cubecart doesn't store the option cost values in the product option but rather with each individual product which would mean that creating the products would take a very long time (even if cloning a 'master' product).


Is there any way that we could import the 3,000+ products by CSV as planned and then apply the same product options and corresponding option cost values to every product in one go?

Link to comment
Share on other sites

Join the conversation

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

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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