문제

I created a form to delete dataobjects from the frontend. the problem is it won't delete.

That's my code. Can someone point me in the right direction?

public function deleteFromCart($ID) { 
    $fields = new FieldList( 
        new HiddenField('ID', 'ID', $ID)
    );

    $actions = new FieldList( 
        new FormAction('doDeleteFromCart', 'löschen') 
    );

    return new Form($this, 'deleteFromCart', $fields, $actions); 
}

public function doDeleteFromCart($data) {
    $cart = CartItem::get()->byID($data['ID']);
    $cart->delete();

    return $this->redirectBack();
}

that's my template

    <% loop getCart %>
        <% if CartItems %>
            <% loop CartItems %>
                $Title - $Amount - $Price - $Sum - $Top.deleteFromCart($ID)
            <% end_loop %>
        <% else %>
            Keine Artikel im Warenkorb
        <% end_if %>
    <% end_loop %>

Also $this->redirectBack(); doesn't work. After submitting the Form I end up here home/deleteFromCart

Thank you in advance

도움이 되었습니까?

해결책

Rather than a form, I would suggest using a link that calls an action to remove items from your cart. It is much simpler and there are less possible problems.

Template

<% loop $getCart %>
    <% if $CartItems %>
        <% loop $CartItems %>
            $Title - $Amount - $Price - $Sum - <a href="{$Top.Link}remove/{$ID}">Remove from cart</a>
        <% end_loop %>
    <% else %>
        Keine Artikel im Warenkorb
    <% end_if %>
<% end_loop %>

Controller

private $allowed_actions = array(
    'remove'
);

public function remove() {
    $cartItemID = $this->request->param('ID');

    if ($cartItemID && $cart = CartItem::get()->byID($cartItemID)) {
        $cart->delete();
    }

    return array();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top