0

basically if a user has added two products on cart he is allowed to add two vouchers, and i've done that part but when a product that was added on basket has been removed by the user the voucher still exist on cart. can i use voucher rules to solve this problem?

 public function deleteAction()
{

    $products = 2;
    // $vouchers_added = 2;
    $quote_id = (int)$this->getRequest()->getParam('id');
    $voucher = Mage::getModel('healthystart/voucher')->load($quote_id);
    if ($quote_id) {
        if  ($products === $voucher){
            $this->addErrorMessage('good.');
            return;
        }
        else {
            if($voucher < $products){
                $this->addErrorMessage('Remove a voucher please.');
                $this->_goBack();
            }
        }

        parent::deleteAction();
    }
}
3
  • Some more information is required in order to help you, such as what voucher extension are you using, what event(s) are you listening to, show us some code?
    – philwinkle
    Aug 21, 2013 at 15:39
  • hi, that is the code that I thought it will work, but is does 20% of what I want it to do. Aug 21, 2013 at 15:44
  • Any movement on this? Anything I can do to help clarify an answer for you? Please update your question if you have more information that can help us close this out.
    – philwinkle
    Sep 6, 2013 at 19:24

1 Answer 1

1

The list of potential issues with your specific problem go well beyond the scope of this question. For instance, there should be no reason to be rewriting a controller's action based on what you show here. But for the sake of helping you debug and putting you on the right path, consider the following:

Comparison:

In this context, $voucher is an object of type Healthystart_Model_Voucher. You are comparing it to an integer, $products. They will never be equivalent.

Schema:

Your use of Mage::getModel('healthystart/voucher')->load() suggests that you only expect one voucher model back. However, unless this is enforced by the schema of the storage engine, any number of results could be returned when loading a voucher by the quote id.

In that case, you probably would only ever get the first item and thus the voucher count will always be one.

Optimization:

  • This line: $quote_id = (int)$this->getRequest()->getParam('id') casts a potential null if no param named ID exists to an int - if you cast null to an int, it will return 0. Because there is no else condition on your outer else, this action will do nothing and return a blank page. Is this intentional?

  • You don't need $voucher unless if($quote_id) evaluates to true - in that case, you shouldn't load it outside the if - it's unnecessary.

  • You are calling parent::deleteAction() only when there is a valid $quote_id; is this intentional?

Conclusion:

There is not enough information to help you with your actual problem. Hopefully the above points will lead you to your eventual solution.

HTH, Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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