Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal for quantity unit #761

Open
sparkcom opened this issue Mar 8, 2021 · 2 comments
Open

Decimal for quantity unit #761

sparkcom opened this issue Mar 8, 2021 · 2 comments

Comments

@sparkcom
Copy link
Contributor

sparkcom commented Mar 8, 2021

Wonder how to get around with the problem that quantity is now integer only.

that's a problem when client is selling things in cubic metres. For example, if someone buy something and he/she wants 1.1 cubic of it.

Currently, we have to set the price per 1/10th cubic metre. and web users need to buy 11 quantity for 1.1 m3.

But this is a not intuitive for vendors or web users.

@jakmax76
Copy link

jakmax76 commented Jul 19, 2023

I have same issue. I need to put in cart m2 quantity. For example 2.30m2
but Quantity is an Int

image

@jakmax76
Copy link

jakmax76 commented Aug 2, 2023

I tried this:

1. OrderItem extension

use SilverShop\Model\OrderItem;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\NumericField;
use SilverStripe\ORM\DataExtension;

class OrderItemExtension extends DataExtension
{
    private static $db = [
        'QuantityDecimal' => 'Decimal(10,2)'
    ];

    public function getQuantity()
    {

        $orderItem = $this->owner->getOwner();
        if ($orderItem && $orderItem instanceof OrderItem) {
            if ($this->owner->QuantityDecimal != 0) {
                return $this->owner->QuantityDecimal;
            } else {
                return $orderItem->getField('Quantity');
            }
        }

        return 1;
    }

    public function updateCMSFields(FieldList $fields)
    {
        $fields->removeByName('Quantity');
        $fields->add(NumericField::create('QuantityDecimal', 'Quantity'));
    }

}

app/_config/extensions.yml

SilverShop\Model\OrderItem:
  extensions:
    - OrderItemExtension

2. ShoppingCartController extension

namespace Controllers;

use SilverShop\Cart\ShoppingCartController;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;

class MyShoppingCartController extends ShoppingCartController
{
    private static $allowed_actions = [
      'setquantity'
    ];

    /**
     * Action: update the quantity of an item in the cart
     *
     * @param HTTPRequest $request
     *
     * @return HTTPResponse
     * @throws \SilverStripe\Control\HTTPResponse_Exception
     */
    public function setquantity($request)
    {
        $product = $this->buyableFromRequest();
        $quantity = (int)$request->getVar('quantity');
        $quantityDecimal = (float)$request->getVar('QuantityDecimal');
        if ($product) {
            $this->cart->setQuantity($product, $quantity, $request->getVars());
            $this->cart->setQuantity($product, $quantityDecimal, $request->getVars());
            $orderItem = $this->cart->get($product, $request->getVars());
            if ($orderItem) {
                // Set both 'quantity' and 'QuantityDecimal' on the OrderItem
                $orderItem->Quantity = $quantity;
                $orderItem->QuantityDecimal = $quantityDecimal;
                $orderItem->write();
            }
        }

        $this->updateLocale($request);
        $this->extend('updateSetQuantityResponse', $request, $response, $product, $quantity);
        return $response ? $response : self::direct();
    }
}

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants