0
votes

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!

2
Have you tried a simple in_array ? - John WH Smith
Yep, doesn't seem to do the trick either! - evensis

2 Answers

0
votes

change your test to check

if(strstr($_SESSION['products'][$i], $attributeSet) !== FALSE) {

strstr can return 0 which will fail your previous test if the string you search for starts at the first position.

The break if the "then" portion of your statement is also unnecessary.

0
votes

Finally figured it out, awful lot of time spent on solving this so I hope this helps someone else out in the future:

umask(0);
require_once 'app/Mage.php';
Mage::app();
Mage::getSingleton('core/session', array ('name' => 'frontend'));
$product = $_POST['productAdd'];
$split = explode(",",$product);
$i = 0;
$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'])) {
    foreach($_SESSION['products'] as $superAttribute) {
        $explode = explode(",",$superAttribute);
        $result = ltrim($explode[2],(' '));
        if($result == $attributeSet) {
            $foundItem = true;
            break;
        }
        $i++;
    }
    if($foundItem) {
        $_SESSION['products'][$i] = $actual;
    }else{
        $_SESSION['products'][] = $actual;
    }
}else{
    $_SESSION['products'][0] = $actual;
}

Output:

array(5) { 
   [0]=> string(18) "6085, 963, Jackets"
   [1]=> string(20) "6087, 1029, Trousers"
   [2]=> string(16) "3369, 1201, Hats"
   [3]=> string(23) "17510, 1327, Waistcoats"
   [4]=> string(24) "15028, 895, Dress Shirts"
} 

This correctly overwrites the array at the desired index, it does have some code in there that trims off the first lump of whitespace (which is what the ajax post is sending). This may not be necessary for someone else implementing this, so bare this in mind!