I've gotten somewhat stuck writing a small method to apply product ids and its associated super attribute to an array session.
What i'm trying to do is if a certain attribute set (for example a waistcoat) is already present in the array then it won't add onto the array, but instead overwrite the position with an updated id and super attribute. I'm currently rewriting how magento adds to the cart to enable the usage of optional configurable products in a bundle.
Currently the script takes an ajax argument, and then applies this to (after splitting out some redundant and unwanted data) the session to be held onto until the add to cart method is called.
My current codebase consists of the following:
<?php
// Handler for holding onto package products.
umask(0);
require_once 'app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', array ('name' => 'frontend'));
$product = $_POST['productAdd'];
$split = explode(",",$product);
$actual = $split[0] . ',' . $split[1] . ',' . $split[2];
$_product = Mage::getModel('catalog/product')->load($split[0]);
$attributeSet = Mage::getModel('eav/entity_attribute_set')->load($_product->getAttributeSetId())->getAttributeSetName();
if(!empty($_SESSION['products'])) {
$arrayLength = count($_SESSION['products']);
for($i = 0; $i <= $arrayLength; $i++) {
if(strstr($_SESSION['products'][$i], $attributeSet)) {
$_SESSION['products'][$i] = $actual;
break;
}else{
$_SESSION['products'][] = $actual;
}
}
}else{
$_SESSION['products'][] = $product;
}
var_dump($_SESSION['products']);
?>
This works for the first occurence in the array and properly overwrites the index position, however, any subsequent additions to the array it doesn't overwrite but instead just appends to the end of the array, which isn't what i'm after!]
Sample output:
array(4) {
[0]=> string(19) "15302, 959, Jackets"
[1]=> string(21) "15321, 1033, Trousers"
[2]=> string(21) "15321, 1033, Trousers"
[3]=> string(21) "15321, 1033, Trousers"
}
If anyone can give me a shove in the right direction it would be most appreciated!
Thanks!
in_array? - John WH Smith