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 (revisited)
 
MrBaseball34
Sr. Member
 
Total Posts:  164
Joined:  2011-06-07
 

In the following script, we are attempting to modify the item_json custom option on certain products when other products are being removed from the cart.

I know this is a LONG script but it is easy to understand. The only problem is it isn’t working and I don’t know why. Magento is notorious for not really doing what you want if you don’t know EXACTLY how to do it and I’m not sure if I am doing thing correctly.

It seems that the attempt to save the option back into the product just doesn’t work. What am I doing wrong? I will post a followup for extra points if a working solution is posted.

First, 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, including ecode
$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);

The full script will be posted below.

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

The posting limitations won’t allow me to post the full remove_from_cart script so I have to break it up:

include_once '../app/Mage.php';
Mage::app();

if (isset(
$_GET['sku'])) {
    $sku2delete 
$_GET['sku'];
}
if ($sku2delete != ''{
    $CAN_DELETE 
TRUE;
    try 
{
        Mage
::getSingleton('core/session', array('name' => 'frontend'));
        
$cart       Mage::helper('checkout/cart')->getCart();
        
$items      $cart->getItems();
        
$cart_count count($items);
        
$event      "";
        
$part       = -1;
        
$cart_skus  = array();
        
$del_c      false;
        foreach (
$items as $item{
            $sku 
trim($item->getProduct()->getSku());
            
// *********************************************
            // Record all the ItemID's in the cart_skus array
            // by SKU
            // *********************************************
            
$cart_skus[trim($sku)]['itemId'$item->getItemId();
            if (
$sku2delete == "CEF_item" && $sku == "CEF_item"{
                $idata 
$item->getOrigData();
writeit("line: "__LINE__); // line 30
writeit("Item_json: "print_r($idata["item_json"]true));                                               
                
// *********************************************
                // The CEF_item product also has item_json and it
                // contains the ecode correponding to the
                // event product that "goes with" the CEF_item.
                // *********************************************
                
if(isset($idata["item_json"])) {
                    $item_json 
json_decode($idata["item_json"]);
                    if(
array_key_exists("ecode"$item_json)) {
                        
// *********************************************
                        // We need to get the ecode and PID
                        // for this CEF_item so we can match the event and
                        // modify the item_json in it.
                        // *********************************************
                        
$event $item_json->ecode;
                        
$part  $item_json->SP->PID;
                    
}
                }
            }                
            
if (preg_match('/^[0-9]{8}[a-zA-Z]{2}/'$sku2delete)) {
                $idata 
$item->getOrigData();
writeit("line: "__LINE__); // line 52
writeit("Item_json: "print_r($idata["item_json"]true));                                               
                if(isset(
$idata["item_json"])) {
                    $item_json 
json_decode($idata["item_json"]);
                    if(
array_key_exists("ecode"$item_json)) {
                        
// *********************************************
                        // We need to get the ecode and PID
                        // for this event and set $del_c to true if 
                        // this is an event being deleted.
                        // *********************************************
                        
if($item_json->ecode == $sku2delete{
                            $del_c 
true;
                            
$event $item_json->ecode;
                            
$part  $item_json->SP->PID;
                        
}
                    }                
                }
            }
        }
 
Magento Community Magento Community
Magento Community
Magento Community
 
MrBaseball34
Sr. Member
 
Total Posts:  164
Joined:  2011-06-07
 

$CEF_item2del = -9999// dummy value...
        // *********************************************
        // This code gets the PID fromt he logged in user,
        // it is used later.
        // *********************************************
        
$pidsess Mage::getSingleton('core/session', array('name'=>'frontend'));
        
$piddata $pidsess->getNA_pid();
        
$pid = (isset($piddata["PID"])) ? $piddata["PID"]0;
        
        foreach (
$items as $item{
            $sku 
trim($item->getProduct()->getSku());
            
// *********************************************
            // If this is an event item and we are not deleting the 
            // CEF_item, we are going to update the item_json
            // *********************************************
            
$idata $item->getOrigData();
writeit("line: "__LINE__); // line 84
writeit("Item_json: "print_r($idata["item_json"]true));                                               
            if(
$sku == $event && !$del_c{
                $options 
$item->getOptions();
                foreach (
$options as $option{
                    $code 
$option->getCode();
                    if(
trim($code) === 'info_buyRequest'{
                        $o 
$option->getOrigData();
                        
$opt unserialize($o['value']);
                        
$item_json json_decode($opt['item_json']);
writeit("line: "__LINE__); // line 94
writeit("Item_json: "print_r(json_encode($item_json), true));                                               
                        
// *********************************************
                        // This HAS to match the Event (already matched above)
                        // AND part
                        // *********************************************
                        
if($item_json->SP->PID == $part{
                            
// *********************************************
                            // Remove the CEI object
                            // *********************************************
                            
unset($item_json->SP->CEI);
                            
// *********************************************
                            // Now, set the Decl value for the CEI object
                            // SET TO 1 ONLY IF IT IS FOR THE LOGGED IN USER.
                            // OTHERWISE SET TO NULL.
                            // *********************************************
                            
if($item_json->SP->PID == $pid{
                                $item_json
->SP->CEI->Decl 1;
                            
else {
                                $item_json
->SP->CEI->Decl null;
                            
}                                    
                            
writeit
("line: "__LINE__); // line 116
writeit("Item_json: "print_r(json_encode($item_json), true));                                               
                            
                            
$item_json json_encode($item_json);
                            
$opt['item_json'$item_json;
                            
// *********************************************
                            // Now, set option value back in
                            // *********************************************
                            
$option->setValue(serialize($opt)); // Line 124
                            
$option->save();
                            
$cart->save();
                        
$o $option->getOrigData();
                        
$opt unserialize($o['value']);
                        
$item_json json_decode($opt['item_json']);
writeit("line: "__LINE__); // line 130
writeit("Item_json: "print_r(json_encode($item_json), true));                                               
                        
}
                    }
                }
            } 
elseif($sku == 'CEF_item' && $del_c{
                
// *********************************************
                // We need to delete the CEF_item for the Event being 
                // deleted. Has to match the part, too!
                // *********************************************
                
$idata $item->getOrigData();
writeit("line: "__LINE__); // line 141
writeit("Item_json: "print_r($idata["item_json"]true));                                               
                
$options $item->getOptions();
                foreach (
$options as $option{
                    $code 
$option->getCode();
                    if(
trim($code) === 'info_buyRequest'{
                        $o 
$option->getOrigData();
                        
$opt unserialize($o['value']);
                        
$item_json json_decode($opt['item_json']);
writeit("line: "__LINE__); // line 150
writeit("Item_json: "print_r(json_encode($item_json), true));                                               
                        if(
$item_json->ecode == $event && $item_json->SP->PID == $part{
                            
// *********************************************
                            // Get the ID of the item so we can delete it later.
                            // *********************************************
                            
$CEF_item2del =  $item->getItemId();
                        
}
                    }
                }
            }
        }
 
Magento Community Magento Community
Magento Community
Magento Community
 
MrBaseball34
Sr. Member
 
Total Posts:  164
Joined:  2011-06-07
 

// *********************************************
        // Delete the passed item sku.
        // *********************************************
        if(isset($cart_skus[$sku2delete]['itemId'])) {
            $cart
->removeItem($cart_skus[$sku2delete]['itemId']);
            
// *********************************************
            // Remove the CE item if needed
            // *********************************************
            
if($CEF_item2del != -9999{
                $cart
->removeItem($CEF_item2del);
            
}
            $cart
->save();
        
}
        
// *********************************************
        // redirects back to cart and exit
        // *********************************************
        
header('Location: /store/checkout/cart');
        exit;
    
catch (Exception $e{
        writeit
("Exception: "print_r($etrue));
    
}
}


// *********************************************
// Function to write to log file.
// *********************************************
function writeit($title$msg{
    $fp 
fopen("/var/tmp/remove_from_cart.txt"'a');
    
fwrite($fp$title $msg "\n");
    
fclose($fp);
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
MrBaseball34
Sr. Member
 
Total Posts:  164
Joined:  2011-06-07
 

This is contents of the remove_from_cart.txt when I remove a CEF_item item from the cart:

line30
Item_json
{"ecode":"SDCC","SP":{"PID":"1158341","FName":"first_name","LName":"last_name","CEI":{"Decl":"null","NOL":"ES","ST":"AK","LIC":"1234","LICtxt":"LNum","CTid":"157883","Hrs":"20","U":"20","P":"0"}}}
line
84
Item_json
{"ecode":"SDCC","SP":{"PID":"1158341","FName":"first_name","LName":"last_name","CEI":{"Decl":"null","NOL":"ES","ST":"AK","LIC":"1234","LICtxt":"LNum","CTid":"157883","Hrs":"20","U":"20","P":"0"}}}
line
94
Item_json
{"ecode":"SDCC","SP":{"PID":"1158341","FName":"first_name","LName":"last_name","CEI":{"Decl":"null","NOL":"ES","ST":"AK","LIC":"1234","LICtxt":"LNum","CTid":"157883","Hrs":"20","U":"20","P":"0"}}}
line
116
Item_json
{"ecode":"SDCC","SP":{"PID":"1158341","FName":"first_name","LName":"last_name","CEI":{"Decl":1}}}
line
130
Item_json
{"ecode":"SDCC","SP":{"PID":"1158341","FName":"first_name","LName":"last_name","CEI":{"Decl":"null","NOL":"ES","ST":"AK","LIC":"1234","LICtxt":"LNum","CTid":"157883","Hrs":"20","U":"20","P":"0"}}}
line
84
Item_json
{"ecode":"SDCC","SP":{"PID":"1158341","FName":"first_name","LName":"last_name","CEI":{"Decl":"null","NOL":"ES","ST":"AK","LIC":"1234","LICtxt":"LNum","CTid":"157883","Hrs":"20","U":"20","P":"0"}}}

It looks like the item_json IS being changed but when setting it back into the cart item on line 124, it just isn’t working.

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