I wanted to drop some notes in there to helpfully assist someone who might be doing weird stuff in Magento similar to me.
As short as possible – a current project uses a custom design interface for our products (in Flash) for the final sell-able item. Every item is a unique design (serialized) on a “stock” product.
In the end of the design process the customer design is serialized (unique). The problem was simple – how do you get the serial # in the cart with the item and pass through the product option phase (not directly to cart).
At first you say – No Problem! Use the
?option[##]=stuff
Not so simple.
After the design, options need to be selected like material type or what ever. So here is some of my solution…
You can AJAX in your results if you want, but this jumps over the “options” phase of the product page. Your AJAX string looks like this (we can’t use .htaccess rewrites because of Flash – BOO!)
AJAX CALL PROTOTYPE : [code]/index.php/checkout/cart/add/product/113/?options[27]=17067[/code]
27 = option ID and 17067 is our generated serial#
Wrap and trigger that JS how you need.
Ultimately I had to inject a hidden input (dynamically) – the input looks like this (PHP code):
$inputStr = '<input type="hidden" value="'.$_GET['serial'].'" name="options['.$ret.']">';
This input is printed inside the form here:
…./template/catalog/product/view.phtml
Where the $_GET['serial'] is the unique design instance appended to the product page (generated by a call to the PHP header() function using variables returned from our designer – buncha’ sick tight queries and code to do this!)
The $ret is the option # for the product. This is returned from a backwards query on the item/sku in Magento.
The basic function is:
// this is actually encapsulated in a class and I leave it as a public static function....
function get_option($product_id){
$product_id = mysqlEscapeString($product_id);
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$readresult=$write->query("SELECT option_id FROM catalog_product_option WHERE product_id = $product_id AND sku = 'serial';");
$row = $readresult->fetch();
if (intval($row['option_id'])>1) {
return mysqlEscapeString($row['option_id']);
}else{
return false;
}
// there is a lot of room in this do what you want!
mysqlEscapeString is a local function that preps data for use in MySQL – replace with your favorite one, there should be a call like this in the Zend framework. It was faster to use from my library as Zend is sluggish.
This function is fed the Magento product ID (easy to get that info) while “serial” is the sku of the option in Magento (we use one sku for these products and options make things simple)
One of the reasons why I am putting this post up is to warn people on some odd stuff and failures. I am not about to worry too much on the how and why, instead let me tell I experience fail.
The fail:
The AJAX call above works well BUT if you apply a “?option[##]=#### to the form action by appending to the
this->getAddToCartUrl($_product);
on the file
…./template/catalog/product/view.phtml
The /uenc/STUFF-HERE in the form action”THATLINK”will see extra garbage.
That garbage will cause failure when the form is submitted. The “garbage”is totally weird and I stopped caring how/why (an unfortunate consequence working with Magento that I dislike). In short I was getting the standard UENC/STUFF with an underscore and other alpha-numeric info added. This breaks the form submission.
So don’t mess with the action URL!
I hope this helps, drop a note if you need more or clarification. This was a long battle in creating a hybrid between a legacy system and Magento.
Closing note – you can not do this in Magento with the Wishlist. Your expectation of the wishlist operating like a “holding area” that is a parallel dimension to the cart is wrong. This “stamping” of the product instance with my required serial # is not possible in the wishlist. (add that feature right?)
Until this is dialed in (if ever), I wired in our own custom design manager. Now back to that work…