Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Modifying JSON string Custom Option
 
MrBaseball34
Sr. Member
 
Total Posts:  164
Joined:  2011-06-07
 

I’m adding a product to my cart that has a custom option that is a JSON string with this code:

$val          = array();
$SP           = array(); // other info added to this array
$val["ecode"$sku;
$SP["CEI"]    $CEI;
$val["SP"]    $SP;
$params       = array("product"   => $product_id,
                      
"qty"       => $qty
                      
"price"     => $event_price,
                      
"item_json" => json_encode($val)
                      );
$cart->addProduct($product$params);
Now, I have a requirement to update the CEI part of the SP at a particular time when the corresponding CEF product is removed from the cart. I know how to get and determine which item to get the item_json from, but how do I “stick it back in” the product after modifying it?

This is my attempt but I’m not sure how to place it back on the order item after modifying it:

$cart       Mage::helper("checkout/cart")->getCart();
$items      $cart->getItems();
$cart_count count($items);
$event      "";
foreach (
$items as $item{
    $sku 
trim($item->getProduct()->getSku());
    
// figure out what ecode is for the CEF item
    
if ($sku2delete == "CEF"{
        $idata 
$item->getOrigData();
        
// *********************************************
        // The CEF product also has item_json and it
        // contains the ecode corresponding to the
        // event product that "goes with" the CEF.
        // *********************************************
        
if(isset($idata["product_options"])) {
            $opt 
unserialize($idata["product_options"]);
            
$item_json json_decode($opt["info_buyRequest"]["item_json"]);
            if(
array_key_exists("ecode"$item_json)) {
                
// *********************************************
                // We"ve found it so get the event code and 
                // break the loop.
                // *********************************************
                
$event       $item_json->ecode;
                
$part        $item_json->SP->PID;
                break;
            
}
        }
    }                
}
// have to loop again in case event item was before CEF
foreach ($items as $item{
    $sku 
trim($item->getProduct()->getSku());
    if(
$sku == $event{
        $idata 
$item->getOrigData();
        if(isset(
$idata["product_options"])) {
            
// *********************************************
            // Get the item_json from the CEF product
            // *********************************************
            
$opt unserialize($idata["product_options"]);
            
$item_json json_decode($opt["info_buyRequest"]["item_json"]);
            if(
$item_json->SP->PID == $part{
                
// *********************************************
                // This HAS to match the Part AND Event
                // *********************************************
                // Remove the CEI object
                // *********************************************
                
unset($item_json->SP["CEI"]);
                
// *********************************************
                // Now, set the Decl value for the CEI object
                // *********************************************
                
$item_json->SP["CEI"]["Decl"1;
                
// *********************************************
                // How do I put the item_json back into the cart 
                // item here???
                // *********************************************
                
break;
            
}
        }              
    }
}
Also, is there a better way to get the product in the cart by SKU vs. having run through the loop again?
 
Magento Community Magento Community
Magento Community
Magento Community
 
MrBaseball34
Sr. Member
 
Total Posts:  164
Joined:  2011-06-07
 

Doesn’t anyone know how to do this?

 
Magento Community Magento Community
Magento Community
Magento Community
 
MrBaseball34
Sr. Member
 
Total Posts:  164
Joined:  2011-06-07
 

Doesn\’t anyone know how to do this?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top