0

To merge loggedin and annoymous user's cart for same session.

User (username:jone) adds something in the cart (Product 1). Before add to cart: Cart is empty User adds something in the cart (Product 1). After add to cart: Order ID 15821 Line item IDs 159808

User registers and logins User logs out

Annoymous User adds something in the cart (Product 1). Before add to cart: Cart is empty Annoymous User adds something in the cart (Product 1). After add to cart: Order ID 15822 Line item IDs 159809

Annoymous User adds something in the cart (Product 2). Before add to cart: Order ID 15822 Line item IDs 159809 Annoymous User adds something in the cart (Product 2). After add to cart: Order ID 15822 Line item IDs 159809, 159810

Viewing the above log, somebody would assume that this user has bought 2 different products.

Now User checks out and login with username : jone
we want to after the login show/add last order product (159808) However, currently we have the following situation.

Order 15821 Line item IDs 159808 are exist in database in checkout_checkout mode(status). Order 15822 exists with 2 line items, 2 different products. Line IDs are 159809, 159810.

I want to be displaying all line items 159808, 159809, 159810. but line item 159808 are not display

please suggest any solution.

1

1 Answer 1

0

I have solved this, by custom code. First you create a login handler like.

<?php
function custom_module_form_alter(&$form, $form_state, $form_id) {

    if($form_id == 'commerce_checkout_form_checkout'){
        $form['buttons']['continue']['#submit'][2] = 'paypal_check_handler';
    }
}

function paypal_check_handler(&$form, $form_state){

    $order_number = $form_state['order']->order_number;
    $return = _get_current_stock($order_number);

    if($return['status']=='goto_paypal'){

     }else{   
       db_query("UPDATE commerce_order SET status = 'cart' where order_id='$order_number'");
       drupal_set_message("We are sorry - Following items are no longer available in stock after you added them in shopping cart. <br/>".'&ldquo; '.$return['product_name'].' &rdquo;');
       drupal_goto('cart');
    }
}

/**
 * This function check current stock.
 */
function _get_current_stock($order_id) {

    global $user;
    $order = commerce_cart_order_load($user->uid); // get user order details
    // get all product id of current order
    $results = db_query("SELECT cp.product_id AS product_id, cli.quantity AS order_qty,fcs.commerce_stock_value AS stock_qty FROM commerce_line_item AS cli LEFT JOIN commerce_product AS cp ON cli.line_item_label=cp.sku LEFT JOIN field_data_commerce_stock AS fcs ON cp.product_id=fcs.entity_id WHERE cli.order_id='$order_id' AND cli.type IN ('product','commerce_auction_lineitem') ")->fetchAll();
    $product_ids = $qty = $stock_qty = array();
    /*
     * @var $product_ids            : Product ID array.
     * @var $qty[$product_ids]      : get each Product order quantity.
     * @var $stock_qty[$product_ids]: get each Product stock quantity.
     */
    foreach($results As $value){
        if(!empty($value->product_id)){
            $product_ids[] = $value->product_id;
            $qty[$value->product_id] = (int)$value->order_qty;
            $stock_qty[$value->product_id] = (int)$value->stock_qty;
        }
    }
    // product id array convert in string forn sql.
    $product_ids_query = implode($product_ids, ',');
    /*
     * Here get product remaining stock
     * @stock   : Stock
     * @order   : Order qty in order table ( status is checkout_payment).
     * @avail   : available qty
     * @stock - @order = @avail
     */
    $results2 = db_query("SELECT cp.title AS product_name, cli.line_item_label AS line_item, cp.product_id AS product_id,cli.quantity AS order_qty, FORMAT(SUM(cli.quantity),0) total_order_qty,  FORMAT(fcs.commerce_stock_value,0) AS total_stock_qty, FORMAT((fcs.commerce_stock_value - SUM(cli.quantity)),0) As available_qty FROM commerce_line_item AS cli LEFT JOIN commerce_product AS cp ON cli.line_item_label=cp.sku LEFT JOIN field_data_commerce_stock AS fcs ON cp.product_id=fcs.entity_id LEFT JOIN commerce_order AS co ON co.order_id=cli.order_id WHERE co.`status`='checkout_payment' AND fcs.commerce_stock_value!=0 AND cp.product_id IN ($product_ids_query) GROUP BY cp.product_id")->fetchAll();
    $available_qty = array();
    foreach($results2 As $value2){
        $available_qty[$value2->product_id] = $value2->available_qty;
        $available_product_name[$value2->product_id] = $value2->product_name;

    }
    $redirection_var = array();
    $redirection_var['status'] = 'goto_paypal';
    foreach($product_ids AS $product_chk){
        if(!empty($available_qty[$product_chk])){
            $current_stock_qty = (int)$available_qty[$product_chk]; 
        } else {
            $current_stock_qty = (int)$stock_qty[$product_chk];
        }

        $ordered_qty = $qty[$product_chk];
        if($current_stock_qty > 0){
            // go to paypal
             $redirection_var;
        } else {
            $current_product_name = $available_product_name[$product_chk];
            $redirection_var['product_name'] = $current_product_name;
            $redirection_var['status'] = 'not_goto_paypal';
            return $redirection_var;
        }
    }
    return $redirection_var;
}
?>

Thankyou :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.