3

Let as assume i got 1 order that order have 10 product's after that some reason i want to Delete for particular product only i can keep 9 product . After Delete total also update based on product.

public function deleteproductAction()
{
    $order_id = $this->getRequest()->getParam('order_id');
    $product_id = $this->getRequest()->getParam('product_id');

     $_order = Mage::getModel('sales/order')->load($order_id);
        $items = $_order->getAllItems();
        foreach ($items as $item){
        $base_grand_total = $_order->getBaseGrandTotal();

        $base_subtotal = $_order->getBaseSubtotal();
        $base_tva = $_order->getBaseTaxAmount();

        $grand_total = $_order->getGrandTotal();

        $subtotal = $_order->getSubtotal();
        $tva = $_order->getTaxAmount();


        $base_subtotal_incl_tax = $_order->getBaseSubtotalInclTax();

        $subtotal_incl_tax = $_order->getSubtotalInclTax();

        $total_item_count = $_order->getTotalItemCount();



        if($item->getProductId()==$product_id){
            $item_price = $item->getPrice();
            $item_tva = $item->getTaxAmount();
            $item->delete();
            $_order->setBaseGrandTotal($base_grand_total-$item_price-$item_tva);

            $_order->setBaseSubtotal($base_subtotal-$item_price);

            $_order->setBaseTaxAmount($base_tva-$item_tva);

            $_order->setGrandTotal($grand_total-$item_price-$item_tva);

            $_order->setSubtotal($subtotal-$item_price);

            $_order->setTaxAmount($tva-$item_tva);


            $_order->setBaseSubtotalInclTax($base_subtotal_incl_tax-$item_price);

            $_order->setSubtotalInclTax($subtotal_incl_tax-$item_price);

            $_order->setTotalItemCount(count($items)-1);

            $_order->save(); 
        }

        }
Mage::getSingleton('adminhtml/session')
                           ->addSuccess('Order Updated Successfully');
        $this->_redirect('adminhtml/sales_order/view', array('order_id' => $_order->getId()));
    }

.phtml file

<td><a href="<?php echo $this->getUrl('orderstatus/adminhtml_index/deleteproduct', array('order_id' => $_item->getOrderId(),'product_id' => $_item->getProductId())) ?>">Delete</a></td>

It's working fine but in seller dashboard it showing deleted product also. How to remove from Seller Dashboard also

enter image description here

public function saveProductData($id,$wholedata){

    $wholedata['use_config_manage_stock'] = 0;

    if($id){
        $wholedata['id'] = $id;
        Mage::dispatchEvent('mp_customattribute_deletetierpricedata', $wholedata);
    }

    /**
     * Initialize product categories
     */

    $cats=array();
    if(isset($wholedata['category'])){
        foreach($wholedata['category'] as $keycat){
            array_push($cats,$keycat);
        }
    }
    if(isset($wholedata['status']) && $wholedata['status'] && $id){
        $status=$wholedata['status']; 
    }
    else{       
        $status=Mage::helper('marketplace')->getIsProductApproval()? 2:1;
    }

    /*
    * Marketplace Product save before Observer
    */
    Mage::dispatchEvent('mp_product_save_before', $wholedata);

    $storeId=Mage::app()->getStore()->getStoreId();

    $baseCurrencyCode = Mage::app()->getStore()->getBaseCurrencyCode();
    $currentCurrencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
    $allowedCurrencies = Mage::getModel('directory/currency')->getConfigAllowCurrencies(); 
    $rates = Mage::getModel('directory/currency')->getCurrencyRates($baseCurrencyCode, array_values($allowedCurrencies));
    if(!$rates[$currentCurrencyCode]){
        $rates[$currentCurrencyCode] = 1;
    }

    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

    $magentoProductModel = Mage::getModel('catalog/product')->setStoreId($storeId)->load($id);
    $magentoProductModel->setStoresIds(array($storeId));
    if($id){
        $magentoProductModel->setStoreId($storeId);
        $wholedata['attribute_set_id'] = $magentoProductModel->getAttributeSetId();
        $wholedata['type_id'] = $magentoProductModel->getTypeId();
        foreach($wholedata as $key=>$val)
        {
            $magentoProductModel->setData($key,$val);
        }
        //To stop Magento regenerating url-key for store, set following,
        $magentoProductModel->setUrlKey(false);
    }else{
        $magentoProductModel->setData($wholedata);
    }   

    if(isset($wholedata['special_price']) && $wholedata['special_price']){
        $special_price = $wholedata['special_price']/$rates[$currentCurrencyCode];
        $magentoProductModel->setSpecialPrice($special_price);
    }
    if(isset($wholedata['price']) && $wholedata['price']){
        $price = $wholedata['price']/$rates[$currentCurrencyCode];
        $magentoProductModel->setPrice($price);
    }
    $magentoProductModel->setStoresIds(array($storeId));
    $magentoProductModel->setWebsiteIds(array(Mage::getModel('core/store')->load( $storeId )->getWebsiteId()));
    if($this->isConfigurable($wholedata['type_id'])){
        $attr = array();
        if(isset($wholedata['supperattr']) && $wholedata['supperattr']){
            $attr=explode(',',$wholedata['supperattr']);
        }

        $attributeId = 0;

        if(isset($attr[0]) && $attr[0]){
            $attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product',$attr[0]);
        }

        if($attributeId){
            $magentoProductModel->getTypeInstance()->setUsedProductAttributeIds(array($attributeId));
        }
        if (array_key_exists('asso_pro', $wholedata)) {
            $asspro = $wholedata['asso_pro'];
            $data[$asspro] = array();
        }
        $i = 0;  
        $configurable_attributes_data = '';
        foreach($attr as $attrCode){
            if($attrCode){
                $super_attribute= Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode);
                $configurableAtt = Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);
                $configurable_attributes_data[] = array(
                    'id'             => $configurableAtt->getId(),
                    'label'          => $configurableAtt->getLabel(),
                    'use_default'    => "0",
                    'position'       => $super_attribute->getPosition(),
                    'values'         => $configurableAtt->getPrices() ? $configProduct->getPrices() : array(),
                    'attribute_id'   => $super_attribute->getId(),
                    'attribute_code' => $super_attribute->getAttributeCode(),
                    'frontend_label' => $super_attribute->getFrontend()->getLabel(),
                    "store_label"    => $super_attribute->getFrontend()->getLabel(),
                );
                $i++;
            }
        }
        /**
         * Initialize data for configurable product
         */
        if ($configurable_attributes_data) {
            $magentoProductModel->setConfigurableAttributesData($configurable_attributes_data);
        }
        $affect_configurable_product_attributes = 1;
        $magentoProductModel->setCanSaveConfigurableAttributes($affect_configurable_product_attributes);
    }
    $magentoProductModel->setCategoryIds($cats);
    $magentoProductModel->setStatus($status);
    if($wholedata['type_id']=='downloadable'){
        if(!isset($wholedata['stock'])){
            $wholedata['stock'] = 10000; 
        }
    }
    if(!isset($wholedata['is_in_stock'])){
        $wholedata['is_in_stock'] = 0;
    }
    if(!isset($wholedata['stock'])){
        $wholedata['stock'] = 0;
    }
    $magentoProductModel->setStockData(array(
                       'use_config_manage_stock' => $wholedata['use_config_manage_stock'],
                       'manage_stock'=>1,
                       'is_in_stock' => $wholedata['is_in_stock'], //Stock Availability
                       'qty' => $wholedata['stock'] //qty
                   )
    );      
    $saved=$magentoProductModel->save();
    $lastId = $saved->getId();
    //$this->_saveStock($lastId,$wholedata['stock'],$wholedata['is_in_stock'],$wholedata['use_config_manage_stock']); 
    $wholedata['id'] = $lastId;
    Mage::dispatchEvent('mp_customoption_setdata', $wholedata);
    $vendorId = Mage::getSingleton('customer/session')->getCustomer()->getId();
    $seller_product_id = 0;
    $seller_products = Mage::getModel('marketplace/product')->getCollection()
                        ->addFieldToFilter('mageproductid',array('eq'=>$lastId))
                        ->addFieldToFilter('userid',array('eq'=>$vendorId));
    foreach ($seller_products as $seller_product) {
        $seller_product_id = $seller_product->getIndexId();
    }
    $collection1=Mage::getModel('marketplace/product')->load($seller_product_id);
    $collection1->setMageproductid($lastId);
    $collection1->setUserid($vendorId);
    if(!$id){
        $collection1->setStatus($status);
    }
    $collection1->save();
    if(!is_dir(Mage::getBaseDir().'/media/marketplace/')){
        mkdir(Mage::getBaseDir().'/media/marketplace/', 0755);
    }
    if(!is_dir(Mage::getBaseDir().'/media/marketplace/'.$lastId.'/')){
        mkdir(Mage::getBaseDir().'/media/marketplace/'.$lastId.'/', 0755);
    }
    $target =Mage::getBaseDir().'/media/marketplace/'.$lastId.'/';
    if(isset($_FILES['images']['name']) && $_FILES['images']['name']){
        $allow_extension = explode(',',Mage::helper('marketplace')->storeImageExtension());
        $images_arr = $_FILES['images'];
        $i = 0;
        foreach($images_arr['name'] as $image){
            if($images_arr['tmp_name'][$i] != ''){
                $splitname = explode('.', $image);
                $splitname[0] = str_replace('-', '', $splitname[0]);
                $image_name = preg_replace('/[^A-Za-z0-9\-]/', '', $splitname[0]);
                $target1 = $target.$image_name.".".$splitname[1];
                $file_extension = pathinfo($image, PATHINFO_EXTENSION);
                if(in_array(strtolower($file_extension),$allow_extension)){
                    move_uploaded_file($images_arr['tmp_name'][$i],$target1);
                }
            }
            $i++;
        }
    }
    if(isset($wholedata['defaultimage'])){
        if($wholedata['defaultimage']){
            $splitname = explode('.', $wholedata['defaultimage']);
            if(isset($splitname[1]) && $splitname[1]){
                $splitname[0] = str_replace('-', '', $splitname[0]);
                $image_name = preg_replace('/[^A-Za-z0-9\-]/', '', $splitname[0]);
                $wholedata['defaultimage'] = $image_name.".".$splitname[1]; 
            }
        }
    }else{
        $wholedata['defaultimage'] = '';
    }

    $this->_addImages($lastId,$wholedata['defaultimage'],$storeId);

    if($wholedata['type_id']=='downloadable'){
        if(isset($_FILES) && count($_FILES) > 0)    {
            /*sort sample array*/
            if(isset($wholedata['samples'])){
                if(count($wholedata['samples'])){
                    $sample_arr = array();
                    foreach ($wholedata['samples'] as $value) {
                        array_push($sample_arr,$value);
                    }
                    $wholedata['samples'] = $sample_arr;
                }
            }

            /*sort link array*/
            if(isset($wholedata['link'])){
                if(count($wholedata['link'])){
                    $link_arr = array();
                    foreach ($wholedata['link'] as $value) {
                        array_push($link_arr,$value);
                    }
                    $wholedata['link'] = $link_arr;
                }
            }

            $this_sample_path = Mage::getBaseDir()."/media/marketplace/".$lastId."/sample/";
            $this_download_path = Mage::getBaseDir()."/media/marketplace/".$lastId."/download/";    
            $this_mainsample_path = Mage::getBaseDir()."/media/marketplace/".$lastId."/mainsample/";            

            if (!is_dir($this_sample_path))
                mkdir($this_sample_path, 0755);

            if (!is_dir($this_download_path))
                mkdir($this_download_path, 0755);

            if (!is_dir($this_mainsample_path))
                mkdir($this_mainsample_path, 0755); 

            $allow_sample_extension = explode(',',Mage::helper('marketplace')->storeSampleExtension());
            $allow_link_extension = explode(',',Mage::helper('marketplace')->storeLinkExtension());

            if(isset($_FILES["wk_samples"])){
                foreach($_FILES["wk_samples"]["tmp_name"]  as $key => $value){
                    if(isset($wholedata['samples'][$key]['type']) && $wholedata['samples'][$key]['type']=='file'){
                        if($_FILES['wk_samples']['tmp_name'][$key] != '' ){
                            $file_name = $_FILES['wk_samples']['name'][$key];
                            $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
                            if(in_array(strtolower($file_extension),$allow_sample_extension)){
                                move_uploaded_file($value,$this_mainsample_path.$file_name);
                                $wholedata['samples'][$key]['file']=$this_mainsample_path.$file_name;
                            }
                        }
                    }
                }
            }

            if(isset($_FILES["linksample"])){
                foreach($_FILES["linksample"]["tmp_name"]  as $key => $value){
                    if(isset($wholedata['link'][$key]['sample']['type']) && $wholedata['link'][$key]['sample']['type']=='file'){
                        if($_FILES['linksample']['tmp_name'][$key] != '' ){
                            $file_name = $_FILES['linksample']['name'][$key];
                            $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
                            if(in_array(strtolower($file_extension),$allow_sample_extension)){
                                move_uploaded_file($value,$this_sample_path.$file_name);
                                $wholedata['link'][$key]['sample']['file']=$this_sample_path.$file_name;
                            }
                        }
                    }
                }
            }

            if(isset($_FILES["wk_link"])){
                foreach($_FILES["wk_link"]["tmp_name"]  as $key => $value){
                    if(isset($wholedata['link'][$key]['type']) && $wholedata['link'][$key]['type']=='file'){
                        if($_FILES['wk_link']['tmp_name'][$key] != '' ){
                            $file_name = $_FILES['wk_link']['name'][$key];
                            $file_extension = pathinfo($file_name, PATHINFO_EXTENSION);
                            if(in_array(strtolower($file_extension),$allow_link_extension)){
                                move_uploaded_file($value,$this_download_path.$file_name);
                                $wholedata['link'][$key]['file']=$this_download_path.$file_name;
                            }
                        }
                    }
                }
            }
        }
        $this->AddImages($lastId,$storeId,$rates,$currentCurrencyCode,$wholedata);
    }
    Mage::dispatchEvent('mp_customattribute_settierpricedata', $wholedata);

    /*
    * Marketplace Product save after Observer
    */
    Mage::dispatchEvent('mp_product_save_after', $wholedata);

    return $lastId;
}

enter image description here

14
  • 1
    i think seller have it own order information check that in database Jan 13, 2016 at 4:37
  • ya its storing marketplace_saleslist this table also how to delete both place
    – Magento 2
    Jan 13, 2016 at 4:45
  • table structure and did ext the save the price too? Jan 13, 2016 at 4:47
  • yes check my updated question
    – Magento 2
    Jan 13, 2016 at 4:51
  • i think it is better to leave the delete product or ask the ext developer about it Jan 13, 2016 at 4:54

2 Answers 2

1

Following Works for me.

$_order = Mage::getModel('sales/order')->load(order_id);
$items = $_order->getAllItems();
foreach ($items as $item) {
    $base_grand_total = $_order->getBaseGrandTotal();

    $base_subtotal = $_order->getBaseSubtotal();
    $base_tva = $_order->getBaseTaxAmount();

    $grand_total = $_order->getGrandTotal();

    $subtotal = $_order->getSubtotal();
    $tva = $_order->getTaxAmount();


    $base_subtotal_incl_tax = $_order->getBaseSubtotalInclTax();

    $subtotal_incl_tax = $_order->getSubtotalInclTax();

    $total_item_count = $_order->getTotalItemCount();



    if ($item->getSku() == 'product-sku') {
        $item_price = $item->getPrice();
        $item_tva = $item->getTaxAmount();
        $item->delete();
        $_order->setBaseGrandTotal($base_grand_total - $item_price - $item_tva);

        $_order->setBaseSubtotal($base_subtotal - $item_price);

        $_order->setBaseTaxAmount($base_tva - $item_tva);

        $_order->setGrandTotal($grand_total - $item_price - $item_tva);

        $_order->setSubtotal($subtotal - $item_price);

        $_order->setTaxAmount($tva - $item_tva);


        $_order->setBaseSubtotalInclTax($base_subtotal_incl_tax - $item_price);

        $_order->setSubtotalInclTax($subtotal_incl_tax - $item_price);

        $_order->setTotalItemCount(count($items) - 1);

        $_order->save();
    }
}
0
+50

Add bellow code in your function deleteproductAction() and try again

public function deleteproductAction()
{
    $order_id = $this->getRequest()->getParam('order_id');
    $product_id = $this->getRequest()->getParam('product_id');

     $_order = Mage::getModel('sales/order')->load($order_id);
     $items = $_order->getAllItems();
     $updateQty = 0;
     foreach ($items as $item){
        $base_grand_total = $_order->getBaseGrandTotal();
        $base_subtotal = $_order->getBaseSubtotal();
        $base_tva = $_order->getBaseTaxAmount();
        $grand_total = $_order->getGrandTotal();

        $subtotal = $_order->getSubtotal();
        $tva = $_order->getTaxAmount();

        $base_subtotal_incl_tax = $_order->getBaseSubtotalInclTax();

        $subtotal_incl_tax = $_order->getSubtotalInclTax();

        $total_item_count = $_order->getTotalItemCount();


        if($item->getProductId()==$product_id){
            $marketplaceSales = Mage::getModel('marketplace/saleslist')->getCollection()
                ->addFieldToFilter('mageproid',$product_id) 
                ->addFieldToFilter('mageorderid',$_order->getId()); 

            $item_price = $item->getPrice();
            $item_tva = $item->getTaxAmount();
            $item->delete();
            $updateQty =  $updateQty+1;
            if($id = $marketplaceSales->getFirstItem()->getAutoid()) {
                $saleslist = Mage::getModel('marketplace/saleslist')->load($id);
                $saleslist->delete();
            }

            $_order->setBaseGrandTotal($base_grand_total-$item_price-$item_tva);
            $_order->setBaseSubtotal($base_subtotal-$item_price);
            $_order->setBaseTaxAmount($base_tva-$item_tva);
            $_order->setGrandTotal($grand_total-$item_price-$item_tva);
            $_order->setSubtotal($subtotal-$item_price);
            $_order->setTaxAmount($tva-$item_tva);
            $_order->setBaseSubtotalInclTax($base_subtotal_incl_tax-$item_price);
            $_order->setSubtotalInclTax($subtotal_incl_tax-$item_price);
            $_order->setTotalItemCount(count($items)-1);
            $_order->save();
        }     
        }

    $orderModel = Mage::getModel('sales/order')->load($_order->getId());
    $orderModel->setTotalQtyOrdered($orderModel->getTotalQtyOrdered()-$updateQty);
    $orderModel->setBaseTotalQtyOrdered($orderModel->getBaseTotalQtyOrdered()-$updateQty);
    $orderModel->save();

Mage::getSingleton('adminhtml/session')
                           ->addSuccess('Order Updated Successfully');
        $this->_redirect('adminhtml/sales_order/view', array('order_id' => $_order->getId()));
}
22
  • It's working but if i check order quantiy it showing 2 but i already delete 1
    – Magento 2
    Jan 22, 2016 at 11:01
  • Updated ans pls check now
    – Abdul
    Jan 22, 2016 at 11:24
  • I have added code $_order->setTotalQtyOrdered($_order->getTotalQtyOrdered()-1) $_order->setBaseTotalQtyOrdered($_order->getBaseTotalQtyOrdered()-1)
    – Abdul
    Jan 22, 2016 at 11:25
  • can i use updated code or use this only
    – Magento 2
    Jan 22, 2016 at 11:28
  • i tried both but still same
    – Magento 2
    Jan 22, 2016 at 11:31

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.