init
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Quote\Model;
|
||||
|
||||
class Quote extends \Magento\Quote\Model\Quote
|
||||
{
|
||||
/**
|
||||
* 客户要求同一个产品(即使相同的子产品),多次加入购物车,不能合并到同一个item。因为订货账户会不一样
|
||||
* @param $product
|
||||
* @return false
|
||||
*/
|
||||
public function getItemByProduct($product)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function addCustomDataToItem($item, $request): void
|
||||
{
|
||||
$fields = ['lab_id', 'cd', 'quote', 'customer_discount_percent', 'note', 'item_sort'];
|
||||
foreach ($fields as $field) {
|
||||
$item->setData($field, $request->getData($field));
|
||||
}
|
||||
}
|
||||
|
||||
public function addProduct(
|
||||
\Magento\Catalog\Model\Product $product,
|
||||
$request = null,
|
||||
$processMode = \Magento\Catalog\Model\Product\Type\AbstractType::PROCESS_MODE_FULL
|
||||
) {
|
||||
if ($request === null) {
|
||||
$request = 1;
|
||||
}
|
||||
if (is_numeric($request)) {
|
||||
$request = $this->objectFactory->create(['qty' => $request]);
|
||||
}
|
||||
if (!$request instanceof \Magento\Framework\DataObject) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('We found an invalid request for adding product to quote.')
|
||||
);
|
||||
}
|
||||
|
||||
if (!$product->isSalable()) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(
|
||||
__('Product that you are trying to add is not available.')
|
||||
);
|
||||
}
|
||||
|
||||
$cartCandidates = $product->getTypeInstance()->prepareForCartAdvanced($request, $product, $processMode);
|
||||
|
||||
/**
|
||||
* Error message
|
||||
*/
|
||||
if (is_string($cartCandidates) || $cartCandidates instanceof \Magento\Framework\Phrase) {
|
||||
return (string)$cartCandidates;
|
||||
}
|
||||
|
||||
/**
|
||||
* If prepare process return one object
|
||||
*/
|
||||
if (!is_array($cartCandidates)) {
|
||||
$cartCandidates = [$cartCandidates];
|
||||
}
|
||||
|
||||
$parentItem = null;
|
||||
$errors = [];
|
||||
$item = null;
|
||||
$items = [];
|
||||
foreach ($cartCandidates as $candidate) {
|
||||
// Child items can be sticked together only within their parent
|
||||
$stickWithinParent = $candidate->getParentProductId() ? $parentItem : null;
|
||||
$candidate->setStickWithinParent($stickWithinParent);
|
||||
|
||||
$item = $this->getItemByProduct($candidate);
|
||||
if (!$item) {
|
||||
$item = $this->itemProcessor->init($candidate, $request);
|
||||
$item->setQuote($this);
|
||||
$item->setOptions($candidate->getCustomOptions());
|
||||
$item->setProduct($candidate);
|
||||
$this->addCustomDataToItem($item, $request);
|
||||
// Add only item that is not in quote already
|
||||
$this->addItem($item);
|
||||
}
|
||||
$items[] = $item;
|
||||
|
||||
/**
|
||||
* As parent item we should always use the item of first added product
|
||||
*/
|
||||
if (!$parentItem) {
|
||||
$parentItem = $item;
|
||||
}
|
||||
if ($parentItem && $candidate->getParentProductId() && !$item->getParentItem()) {
|
||||
$item->setParentItem($parentItem);
|
||||
}
|
||||
|
||||
$this->itemProcessor->prepare($item, $request, $candidate);
|
||||
|
||||
// collect errors instead of throwing first one
|
||||
if ($item->getHasError()) {
|
||||
$this->deleteItem($item);
|
||||
foreach ($item->getMessage(false) as $message) {
|
||||
if (!in_array($message, $errors)) {
|
||||
// filter duplicate messages
|
||||
$errors[] = $message;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!empty($errors)) {
|
||||
throw new \Magento\Framework\Exception\LocalizedException(__(implode("\n", $errors)));
|
||||
}
|
||||
|
||||
$this->_eventManager->dispatch('sales_quote_product_add_after', ['items' => $items]);
|
||||
return $parentItem;
|
||||
}
|
||||
|
||||
public function getItemsCollection($useCache = true)
|
||||
{
|
||||
if ($this->hasItemsCollection() && $useCache) {
|
||||
return $this->getData('items_collection');
|
||||
}
|
||||
if (null === $this->_items || !$useCache) {
|
||||
$this->_items = $this->_quoteItemCollectionFactory->create();
|
||||
$this->_items->setOrder('item_sort', 'asc');
|
||||
$this->extensionAttributesJoinProcessor->process($this->_items);
|
||||
$this->_items->setQuote($this);
|
||||
}
|
||||
return $this->_items;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
namespace Stemcell\Quote\Model\Quote;
|
||||
|
||||
use Magento\Framework\App\ObjectManager;
|
||||
use Magento\Quote\Api\CartRepositoryInterface;
|
||||
use Magento\Quote\Model\Quote\Address\Total\AbstractTotal;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class CustomerDiscount extends AbstractTotal
|
||||
{
|
||||
const CODE = 'customer_discount';
|
||||
|
||||
public function __construct(
|
||||
protected CartRepositoryInterface $cartRepository,
|
||||
)
|
||||
{
|
||||
$this->setCode(self::CODE);
|
||||
}
|
||||
|
||||
public function getLabel()
|
||||
{
|
||||
return '优惠金额';
|
||||
}
|
||||
|
||||
public function collect(
|
||||
\Magento\Quote\Model\Quote $quote,
|
||||
\Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment,
|
||||
\Magento\Quote\Model\Quote\Address\Total $total
|
||||
) {
|
||||
parent::collect($quote, $shippingAssignment, $total);
|
||||
|
||||
$discountAmount = 0.0;
|
||||
|
||||
|
||||
$items = $shippingAssignment->getItems();
|
||||
if (!count($items)) {
|
||||
return $this;
|
||||
}
|
||||
|
||||
foreach ($items as $item) {
|
||||
// skip parent items like configurable
|
||||
if ($item->getParentItemId()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$percent = (float)$item->getData('customer_discount_percent');
|
||||
|
||||
if ($percent <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// get row total (price * qty) - use getRowTotalInclTax or getRowTotal as appropriate
|
||||
$rowTotal = $item->getRowTotal(); // row total without discount
|
||||
if ($rowTotal <= 0) {
|
||||
continue;
|
||||
}
|
||||
$itemDiscount = round($rowTotal * (($percent) / 100), 2);
|
||||
|
||||
// add to item discount amounts
|
||||
$currentDiscount = (float)$item->getDiscountAmount();
|
||||
$item->setDiscountAmount($currentDiscount + $itemDiscount);
|
||||
|
||||
// $item->setDiscountAmount($item->getRowTotal()*($percent/100));
|
||||
$item->setBaseDiscountAmount($item->getRowTotal()*($percent/100));
|
||||
$item->setDiscountPercent($percent);
|
||||
$item->setRowTotalWithDiscount($item->getRowTotal()*($percent/100));
|
||||
$item->setRowTotal($item->getRowTotal()*((100-$percent)/100));
|
||||
$item->setBaseRowTotal($item->getRowTotal()*((100-$percent)/100));
|
||||
$item->setRowTotalInclTax($item->getRowTotal()*((100-$percent)/100));
|
||||
$item->setBaseRowTotalInclTax($item->getRowTotal()*((100-$percent)/100));
|
||||
$item->save();
|
||||
|
||||
// track discount for total
|
||||
$discountAmount += $itemDiscount;
|
||||
}
|
||||
|
||||
if ($discountAmount != 0.0) {
|
||||
$total->addTotalAmount($this->getCode(), -$discountAmount);
|
||||
$total->setTotalAmount($this->getCode(), -$discountAmount);
|
||||
$total->addBaseTotalAmount($this->getCode(), -$discountAmount);
|
||||
}
|
||||
|
||||
$quote->getExtensionAttributes()->setCustomerDiscount(-$discountAmount);
|
||||
$quote->setData($this->getCode(), -$discountAmount)->save();
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
|
||||
{
|
||||
$amount = $quote->getData($this->getCode());
|
||||
if ($amount != 0) {
|
||||
return [
|
||||
'code' => $this->getCode(),
|
||||
'title' => __($this->getLabel()),
|
||||
'value' => $amount
|
||||
];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user