Note that there are some explanatory texts on larger screens.

plurals
  1. POOverwrite array position
    primarykey
    data
    text
    <p>I've gotten somewhat stuck writing a small method to apply product ids and its associated super attribute to an array session.</p> <p>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.</p> <p>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.</p> <p>My current codebase consists of the following:</p> <pre><code>&lt;?php // Handler for holding onto package products. umask(0); require_once 'app/Mage.php'; Mage::app(); Mage::getSingleton('core/session', array ('name' =&gt; 'frontend')); $product = $_POST['productAdd']; $split = explode(",",$product); $actual = $split[0] . ',' . $split[1] . ',' . $split[2]; $_product = Mage::getModel('catalog/product')-&gt;load($split[0]); $attributeSet = Mage::getModel('eav/entity_attribute_set')-&gt;load($_product-&gt;getAttributeSetId())-&gt;getAttributeSetName(); if(!empty($_SESSION['products'])) { $arrayLength = count($_SESSION['products']); for($i = 0; $i &lt;= $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']); ?&gt; </code></pre> <p>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!]</p> <p>Sample output:</p> <pre><code>array(4) { [0]=&gt; string(19) "15302, 959, Jackets" [1]=&gt; string(21) "15321, 1033, Trousers" [2]=&gt; string(21) "15321, 1033, Trousers" [3]=&gt; string(21) "15321, 1033, Trousers" } </code></pre> <p>If anyone can give me a shove in the right direction it would be most appreciated!</p> <p>Thanks!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload