4

When user remove the Previously added item from cart, than it will be removed from Database table : sales_flat_quote_item_option, i need similar feature.

Question : once Registered user click on custom button "save design" in product view page , we are creating new row in my_designs table and creating new product and saving product information in database.

enter image description here

once customer click on "add to cart" for that product, we want to remove that product information from "my_designs" table. we want to delete that row.

enter image description here

we are using below code to add the product to cart

<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>

<?php $collections = $this->getCollection(); ?>

<?php $_product = $this->getProduct(); ?>

<div id="messages_product_view">
<?php $buttonTitle = Mage::helper('core')->quoteEscape($this->__('Add to Cart')); ?>
</div>

<?php foreach ($collections as $key => $product) {

    echo $product->getName().' <br>';
?>
<form action="<?php echo  Mage::helper('checkout/cart')->getAddUrl($product);?>" method="post">  
<div class="add-to-cart-buttons">

<button type="submit" title="<?php echo $buttonTitle ?>" class="button btn-cart" >
    <span><span>
    <?php echo $buttonTitle ?>
    </span></span>
</button>

<?php echo $this->getChildHtml('', true, true) ?>

</div>
</form>
<hr style='display:inline-block; width:100%;'>
<?php

}
?>
1
  • what is this Vikas. why you Always going with bounty??
    – Amit Bera
    Commented Mar 25, 2017 at 14:51

3 Answers 3

3
+50

First create an observer in your code config.xml

<frontend>
    ........
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <delete_save_design_row>
                    <class>Amasty_Example_Model_Observer</class>
                    <method>_deletedesign</method>
                </delete_save_design_row>
            </observers>
        </checkout_cart_product_add_after>
    </events>
    .........
</frontend>

Now create a new file Amasty/Example/Model/Observer.php with below code

<?php

class Amasty_Example_Model_Observer
{
    public function _deletedesign($observer)
    { 
        $productId = $observer->getProduct()->getId();
        $customerId = Mage::getSingleton('customer/session')->getCustomer()->getId();
        $collection = Mage::getModel('example/mydesign')->getCollection()
                ->addFieldToFilter('product_id', $productId)
                ->addFieldToFilter('customer_id', $customerId);
        $collection->walk("delete");
    }
}
?>
9
  • As in Db table also, i gave name as product_id & customer_id , in observer.php, i am trying as ->addFieldToFilter('product_id', 'product_id') ->addFieldToFilter('customer_id', 'customer_id'); , full code : pastebin.com/QEhj3SN7 , but i am getting this error : Fatal error: Call to a member function addFieldToFilter() on a non-object in line 10 , what wrong i done here?.... Commented Mar 26, 2017 at 14:21
  • Use observer from my code as it is. It will work. Commented Mar 26, 2017 at 14:23
  • yes, i trust you more than me :) but i faced same error..... Commented Mar 26, 2017 at 14:24
  • Check with updated observer code. Commented Mar 26, 2017 at 14:26
  • i got error as can not add the item to shopping cart` : prnt.sc/eongua Commented Mar 26, 2017 at 14:29
2

use controller_action_postdispatch_checkout_cart_add event refer link

get product id from param and customer id from session

use above both id to delete entry from your table

UPDATE Query to delete entry

$collection = Mage::getResourceModel('amasty/mydesign_collection')
        ->addFieldToFilter('product_id', <YOUR_PRODUCT_ID)
        ->addFieldToFilter('customer_id', <CUSTOMER_ID);
$collection->walk("delete");
7
  • Thanks, i will try , if you can update an answer with some code that will be really helpfull..... Commented Mar 25, 2017 at 14:09
  • You have not provided name and model of database from which you want to remove entry @BabyinMagento Commented Mar 25, 2017 at 14:09
  • please check : prnt.sc/eoavvd & here is app/code/local/Amasty/Example/Model/Mysql4/Amasty/collection.php - pastebin.com/aLrf1ASY Commented Mar 25, 2017 at 14:16
  • Find updated answer @BabyinMagento Commented Mar 25, 2017 at 14:38
  • Thanks a lot for updating with code, you can see our database table structure in question, so i am using your code in same file : pastebin.com/0CyrUGqK please check and tell me is there anything else i need to do ? Commented Mar 25, 2017 at 16:17
1

You can use a trigger for that :

CREATE TRIGGER deleteDesign
    ON sales_flat_quote_item
    FOR DELETE
AS
    DELETE FROM my_designs
    WHERE product_id = (SELECT deleted.id FROM deleted) AND customer_id = (SELECT deleted.customer_id FROM deleted)
GO

When an item is removed from sales_flat_quote_item table, it will delete it from my_designs.

1
  • Thanks a lot , i already got solution, so i opened bounty to award the very good answer, i will still try your answer and let you know...... Commented Mar 27, 2017 at 14:29

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.