silvershop/silvershop-core

Decimal for quantity unit

sparkcom opened this issue · 2 comments

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.

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

image

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