2013-07-16 94 views
0

我已經有點卡住了寫一個小方法來將產品ID及其關聯的超級屬性應用到數組會話。覆蓋陣列位置

我想要做的是,如果某個屬性集(例如背心)已經存在於數組中,那麼它將不會添加到數組上,而是用更新後的id和super來覆蓋位置屬性。我目前正在重寫magento如何添加到購物車,以便在捆綁包中使用可選的可配置產品。

當前腳本需要一個ajax參數,然後將其應用於(分割出一些冗餘和不需要的數據之後)會話,直到調用add to cart方法。

我當前的代碼庫由以下部分組成:

<?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']); 
?> 

這適用於數組中的第一次出現,並適當覆蓋索引位置,但是,任何後續增加的數組不會覆蓋,而是隻是追加到數組的結尾,這是不是我後很]

輸出示例:

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" 
} 

如果任何人都可以給我一個shov e在正確的方向,將不勝感激!

謝謝!

+0

你試過簡單的'in_array'嗎? –

+0

是的,似乎也沒有伎倆! – evensis

回答

0

終於想通了,非常多的時間解決這個花了,所以我希望這可以幫助別人出未來:

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; 
} 

輸出:

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" 
} 

這正確地覆蓋陣列在期望的索引處,它確實有一些代碼可以修剪掉第一個空格(這是ajax的發送內容)。對於其他人來說,這可能不是必要的,所以記住這一點!

0

改變你的測試,以檢查

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

可以的strstr返回0,如果字符串中,你在第一位置搜索開始將失敗以前的測試。

如果您的聲明的「然後」部分也是不必要的,那麼中斷。

+0

感謝您的建議orangepill,不幸的是沒有去!它仍然在數組中添加後續的索引位置,而第一個位置正確覆蓋。如果沒有它,取消休息似乎會使事情變得更糟糕,它甚至會追加到第一個位置! – evensis