Magento 2의 합계를 주문하는 데 요금을 추가하는 방법
-
13-12-2019 - |
문제
다음 링크는
를 설명합니다.http://excellencemagentoblog.com/ 블로그 / 2012 / 01 / 27 / 마젠토 - 추가 비용 - 할인 - 총 /
자홍색 1의 합계를 주문하기 위해 수수료를 추가합니다.
이제이 기능은 마젠토 2에서 견적 모듈 로 이동됩니다.
나는 수집 및 가져 오기와 같은 개념을 여전히 동일하게 생각한다. Magento 2에서 누군가가 이것을 시도 했습니까?
해결책
아래 단계를 따르면 도움이 될 것입니다. 내 모듈에서 방금 수수료 열을 추가했습니다.
이것은 장바구니에 하나의 행을 추가 할 것입니다. 체크 아웃 페이지의 요금 및 측면 막대
또한 총 금액에 수수료 금액을 추가했습니다 (100 명으로 유지 된 요금 정적 가치)
주문이 요금이 부과되면 주문보기에 기록 된 경우 총 블록에서 요금의 새로운 행을 볼 수 있지만 관리자 측은 아직 구현하지 않으면 해당 답변을 게시 할 수 있습니다
모듈 etc 폴더에 sales.xml 생성
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd">
<section name="quote">
<group name="totals">
<item name="fee" instance="Sugarcode\Test\Model\Total\Fee" sort_order="150"/>
</group>
</section>
</config>
.
App \ Code \ SugarCode \ Test \ View \ Frontend \ Web \ JS \ View \ Checkout \ Cart \ Totals \ fee.js
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
define(
[
'Sugarcode_Test/js/view/checkout/summary/fee'
],
function (Component) {
'use strict';
return Component.extend({
/**
* @override
*/
isDisplayed: function () {
return true;
}
});
}
);
.
App \ Code \ SugarCode \ Test \ View \ Frontend \ Web \ JS \ View \ Checkout \ Summary \ Fee.js
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/*jshint browser:true jquery:true*/
/*global alert*/
define(
[
'Magento_Checkout/js/view/summary/abstract-total',
'Magento_Checkout/js/model/quote',
'Magento_Catalog/js/price-utils',
'Magento_Checkout/js/model/totals'
],
function (Component, quote, priceUtils, totals) {
"use strict";
return Component.extend({
defaults: {
isFullTaxSummaryDisplayed: window.checkoutConfig.isFullTaxSummaryDisplayed || false,
template: 'Sugarcode_Test/checkout/summary/fee'
},
totals: quote.getTotals(),
isTaxDisplayedInGrandTotal: window.checkoutConfig.includeTaxInGrandTotal || false,
isDisplayed: function() {
return this.isFullMode();
},
getValue: function() {
var price = 0;
if (this.totals()) {
price = totals.getSegment('fee').value;
}
return this.getFormattedPrice(price);
},
getBaseValue: function() {
var price = 0;
if (this.totals()) {
price = this.totals().base_fee;
}
return priceUtils.formatPrice(price, quote.getBasePriceFormat());
}
});
}
);
.
App \ Code \ SugarCode \ Test \ View \ Frontend \ Web \ Template \ Checkout \ Summary \ Fee.html
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<!-- ko -->
<tr class="totals fee excl">
<th class="mark" scope="row">
<span class="label" data-bind="text: title"></span>
<span class="value" data-bind="text: getValue()"></span>
</th>
<td class="amount">
<span class="price"
data-bind="text: getValue(), attr: {'data-th': title}"></span>
</td>
</tr>
<!-- /ko -->
.
App \ Code \ SugarCode \ Test \ View \ Frontend \ Web \ Template \ Checkout \ Cart \ totals \ fee.html
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<!-- ko -->
<tr class="totals fee excl">
<th class="mark" colspan="1" scope="row" data-bind="text: title"></th>
<td class="amount">
<span class="price" data-bind="text: getValue()"></span>
</td>
</tr>
<!-- /ko -->
.
앱 \ Code \ SugarCode \ Test \ Model \ Total \ Fee.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Sugarcode\Test\Model\Total;
class Fee extends \Magento\Quote\Model\Quote\Address\Total\AbstractTotal
{
/**
* Collect grand total address amount
*
* @param \Magento\Quote\Model\Quote $quote
* @param \Magento\Quote\Api\Data\ShippingAssignmentInterface $shippingAssignment
* @param \Magento\Quote\Model\Quote\Address\Total $total
* @return $this
*/
protected $quoteValidator = null;
public function __construct(\Magento\Quote\Model\QuoteValidator $quoteValidator)
{
$this->quoteValidator = $quoteValidator;
}
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);
$exist_amount = 0; //$quote->getFee();
$fee = 100; //Excellence_Fee_Model_Fee::getFee();
$balance = $fee - $exist_amount;
$total->setTotalAmount('fee', $balance);
$total->setBaseTotalAmount('fee', $balance);
$total->setFee($balance);
$total->setBaseFee($balance);
$total->setGrandTotal($total->getGrandTotal() + $balance);
$total->setBaseGrandTotal($total->getBaseGrandTotal() + $balance);
return $this;
}
protected function clearValues(Address\Total $total)
{
$total->setTotalAmount('subtotal', 0);
$total->setBaseTotalAmount('subtotal', 0);
$total->setTotalAmount('tax', 0);
$total->setBaseTotalAmount('tax', 0);
$total->setTotalAmount('discount_tax_compensation', 0);
$total->setBaseTotalAmount('discount_tax_compensation', 0);
$total->setTotalAmount('shipping_discount_tax_compensation', 0);
$total->setBaseTotalAmount('shipping_discount_tax_compensation', 0);
$total->setSubtotalInclTax(0);
$total->setBaseSubtotalInclTax(0);
}
/**
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return array|null
*/
/**
* Assign subtotal amount and label to address object
*
* @param \Magento\Quote\Model\Quote $quote
* @param Address\Total $total
* @return array
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function fetch(\Magento\Quote\Model\Quote $quote, \Magento\Quote\Model\Quote\Address\Total $total)
{
return [
'code' => 'fee',
'title' => 'Fee',
'value' => 100
];
}
/**
* Get Subtotal label
*
* @return \Magento\Framework\Phrase
*/
public function getLabel()
{
return __('Fee');
}
}
.
App \ Code \ SugarCode \ Test \ etc \ module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Sugarcode_Test" setup_version="2.0.6" schema_version="2.0.6">
<sequence>
<module name="Magento_Sales"/>
<module name="Magento_Quote"/>
<module name="Magento_Checkout"/>
</sequence>
</module>
</config>
.
App \ Code \ SugarCode \ Test \ View \ Frontend \ 레이아웃 \ CheckOut_cart_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.cart.totals">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="block-totals" xsi:type="array">
<item name="children" xsi:type="array">
<item name="fee" xsi:type="array">
<item name="component" xsi:type="string">Sugarcode_Test/js/view/checkout/cart/totals/fee</item>
<item name="sortOrder" xsi:type="string">20</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Sugarcode_Test/checkout/cart/totals/fee</item>
<item name="title" xsi:type="string" translate="true">Fee</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>
.
App \ Code \ SugarCode \ Test \ View \ Frontend \ Layout \ CheckOut_index_index.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceBlock name="checkout.root">
<arguments>
<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="checkout" xsi:type="array">
<item name="children" xsi:type="array">
<item name="sidebar" xsi:type="array">
<item name="children" xsi:type="array">
<item name="summary" xsi:type="array">
<item name="children" xsi:type="array">
<item name="totals" xsi:type="array">
<item name="children" xsi:type="array">
<item name="fee" xsi:type="array">
<item name="component" xsi:type="string">Sugarcode_Test/js/view/checkout/cart/totals/fee</item>
<item name="sortOrder" xsi:type="string">20</item>
<item name="config" xsi:type="array">
<item name="template" xsi:type="string">Sugarcode_Test/checkout/cart/totals/fee</item>
<item name="title" xsi:type="string" translate="true">Fee</item>
</item>
</item>
</item>
</item>
<item name="cart_items" xsi:type="array">
<item name="children" xsi:type="array">
<item name="details" xsi:type="array">
<item name="children" xsi:type="array">
<item name="subtotal" xsi:type="array">
<item name="component" xsi:type="string">Magento_Tax/js/view/checkout/summary/item/details/subtotal</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</item>
</argument>
</arguments>
</referenceBlock>
</body>
</page>
.
App \ Code \ SugarCode \ Test \ View \ Frontend \ Layout \ Sales_Order_view.xml
<?xml version="1.0"?>
<!--
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="order_totals">
<block class="Sugarcode\Test\Block\Sales\Order\Fee" name="fee"/>
</referenceContainer>
</body>
</page>
.
App \ Code \ SugarCode \ Test \ Block \ Sales \ Order \ Fee.php
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Tax totals modification block. Can be used just as subblock of \Magento\Sales\Block\Order\Totals
*/
namespace Sugarcode\Test\Block\Sales\Order;
class Fee extends \Magento\Framework\View\Element\Template
{
/**
* Tax configuration model
*
* @var \Magento\Tax\Model\Config
*/
protected $_config;
/**
* @var Order
*/
protected $_order;
/**
* @var \Magento\Framework\DataObject
*/
protected $_source;
/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Tax\Model\Config $taxConfig
* @param array $data
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Tax\Model\Config $taxConfig,
array $data = []
) {
$this->_config = $taxConfig;
parent::__construct($context, $data);
}
/**
* Check if we nedd display full tax total info
*
* @return bool
*/
public function displayFullSummary()
{
return true;
}
/**
* Get data (totals) source model
*
* @return \Magento\Framework\DataObject
*/
public function getSource()
{
return $this->_source;
}
public function getStore()
{
return $this->_order->getStore();
}
/**
* @return Order
*/
public function getOrder()
{
return $this->_order;
}
/**
* @return array
*/
public function getLabelProperties()
{
return $this->getParentBlock()->getLabelProperties();
}
/**
* @return array
*/
public function getValueProperties()
{
return $this->getParentBlock()->getValueProperties();
}
/**
* Initialize all order totals relates with tax
*
* @return \Magento\Tax\Block\Sales\Order\Tax
*/
public function initTotals()
{
$parent = $this->getParentBlock();
$this->_order = $parent->getOrder();
$this->_source = $parent->getSource();
$store = $this->getStore();
$fee = new \Magento\Framework\DataObject(
[
'code' => 'fee',
'strong' => false,
'value' => 100,
//'value' => $this->_source->getFee(),
'label' => __('Fee'),
]
);
$parent->addTotal($fee, 'fee');
// $this->_addTax('grand_total');
$parent->addTotal($fee, 'fee');
return $this;
}
}
.
위의 단계가 아래로 실행되면 아래 명령이 수행됩니다. 이는 중요합니다. 즉, JS & HTML 파일이 PUB / Static 폴더에서 누락됩니다. 따라서 PUB / static 폴더에서 JS 및 HTML 파일을 만드는 명령을 실행합니다
bin \ magento 설정 : 정적 내용 : 배포
다른 사람들이 다른 사람을 돕는 내 대답을 수락하면
다른 팁
주문에 추가 비용을 추가하기위한 사용자 정의 모듈을 개발했습니다.
추가 요금은 카트 페이지, 체크 아웃 페이지, 송장 및 Creditmemo 에 표시됩니다.또한 가격 유형을 관리자 구성에서 수정 및 백분율 로 선택할 수도 있습니다.
Pradeep의 대답은 매우 도움이되지만 중요한 점이 없습니다.
SugarCode \ Test \ Model \ Total :: Collect ()는 Magento의 Magento \ Quote \ Model \ v @ vantotalscollector :: Collect (), 각 주소에 대해 두 번 호출됩니다.이 시점에서 인용구 테이블에 저장된 합계를 만듭니다.주문이나 체크 아웃의 웹 사이트에 표시되지 않습니다.
이런 이유로 collect ()가 호출되는 한 번만 수수료를 수집하는 것이 중요합니다.제공된 배송 된 항목이 있는지 확인하여 수행 할 수 있습니다.
$items = $shippingAssignment->getItems();
if (!count($items)) {
return $this;
}
.
SugarCode \ Test \ Model \ Total :: Collect ()
이 코드를 추가하십시오.위의 답변을 기반으로 한 사용자 정의 수수료 확장을 개발했습니다.
주석을 주석
$total->setGrandTotal($total->getGrandTotal() + $balance);
.
acp \ sugarcode \ test \ model \ Total \ fee.php
그것은 당신을 도울 것입니다 !!
2 회 수수료 문제로 어려움을 겪고있는 모든 사람들은 수집 방법에 수수료를 적용하기 위해 다음 코드를 시도하십시오.
$fee = 10;
$total->addTotalAmount('fee', $fee);
$total->addBaseTotalAmount('fee', $fee);
$total->setBaseGrandTotal($total->getBaseGrandTotal());
.
수집 방법은 이와 같이 보일 것입니다.
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);
if (!count($shippingAssignment->getItems())) {
return $this;
}
$fee = 10;
$total->addTotalAmount('fee', $fee);
$total->addBaseTotalAmount('fee', $fee);
$total->setBaseGrandTotal($total->getBaseGrandTotal());
$quote->setFee($fee);
return $this;
}
.