自動割引クーポンゼロ支払チェックアウトと競合しています
-
13-12-2019 - |
質問
私は、そこにある品目に基づいて、顧客が自分のバスケットに適用される自動割引コードを取得する必要があるプロジェクトに取り組んできました。
これは以下のコードロジックと完全に機能していたと思いました。私はコードが生成された(後端で(カートの割引を示した)。しかし今日、顧客の信用を注文のために排他的に使用することはできません。
私はこのモジュールまで絞り込まれました。しかし、私たちがその場でコードを生み出しているので、Magentoが実際の割引を扱うことが通常どおりに機能するはずです。デバッグは、無料のチェックアウトで利用可能かどうかを確認するときに、Grand Totalがまだ元の価格であることを示しています。
これを互換性があるために入力する必要がある追加の論理はありますか?
クライアントの機密保持の理由から、マスクされたネームスペース/モジュール名がわずかに、このようにいくつかのエラーを見つけることができるようにしてくださいが、コードが実行されているものを実行して実行してください。
期待:
顧客が割引の基準を満たしている場合は、割引コードが生成され、カートに対して割引が得られます。
顧客が完全に支払うのに十分な店舗クレジットがある場合、彼らは店の信用に満足して支払うことができるはずです。
実際の:
割引コードが生成されます(該当する場合)
お客様は、お買い上げのために完全に支払うために店のクレジットを使うことができません。
オブザーバ:checkout_cart_product_add_after
public function checkout_cart_product_add_after($observer)
{
if (!Mage::helper('core')->isModuleOutputEnabled('Namespace_WholesaleDiscount')) {
return;
}
/*
* When emptying the cart this observer is updated
* due to the setting of it to 0 for deletion.
* This prevents running the observer and thus resetting items in quote.
*/
$request = Mage::app()->getRequest()->getParam('update_cart_action');
if ($request == 'empty_cart') {
return;
}
try {
$model = Mage::getModel('salesrule/rule')
->getCollection()
->addFieldToSelect('*')
->addFieldToFilter('name', array('eq' => sprintf('WHOLESALE AUTO_%s', Mage::getSingleton('customer/session')->getSessionId())))
->load();
if ($model->count() < 1) {
// No coupon yet.
$helper = Mage::helper('wholesalediscount');
$helper->createDiscountCode($helper->getDiscountAmount(), Mage::getSingleton('customer/session')->getSessionId());
$model = Mage::getModel('salesrule/rule')
->getCollection()
->addFieldToFilter('name', array('eq' => sprintf('WHOLESALE AUTO_%s', Mage::getSingleton('customer/session')->getSessionId())))
->getFirstItem();
} else {
$helper = Mage::helper('wholesalediscount');
$model = $model->getFirstItem();
$model->setDiscountAmount($helper->getDiscountAmount());
$code = Mage::helper('core')->getRandomString(16);
// Magento initializes the coupon_code, but on model load it is code.
$model->setCouponCode($code);
$model->setCode($code);
$model->save();
}
$couponCode = $model->getCode();
/* END This part is my extra, just to load our coupon for this specific customer */
Mage::getSingleton('checkout/cart')
->getQuote()
->getShippingAddress()
->setCollectShippingRates(true);
Mage::getSingleton('checkout/cart')
->getQuote()
->setCouponCode(strlen($couponCode) ? $couponCode : '')
->collectTotals()
->save();
} catch (Exception $e) {
Mage::logException($e);
}
return $this;
}
.
ヘルパーロジック: 上記のように、私はビジネスロジックまたはクライアント機密性を違反する潜在的に私が感じることは何でもマスクしました。 前述のように、ここで2つの方法は単にディスカウントコードのクーポン(管理者での作業を見ることができ、割引を開くことができる)を生成し、割引を計算します(これは完全にビジネスロジックです)
public function createDiscountCode($discount, $reference)
{
$now = new Zend_Date();
$now->setTimezone('UTC');
$data = array(
'product_ids' => null,
'name' => sprintf('WHOLESALE AUTO_%s', $reference),
'description' => null,
'is_active' => 1,
'website_ids' => array(Mage::app()->getWebsite()->getId()),
'customer_group_ids' => array(Mage::getSingleton('customer/session')->getCustomerGroupId()),
'coupon_type' => 2,
'coupon_code' => Mage::helper('core')->getRandomString(16),
'uses_per_coupon' => 1,
'uses_per_customer' => 1,
'from_date' => $now->getFullYear() . "-". $now->getMonth() . "-" . $now->getDay(),
'to_date' => null,
'sort_order' => null,
'is_rss' => 1,
'rule' => array(
'conditions' => array(
array(
'type' => 'salesrule/rule_condition_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => null
)
)
),
'simple_action' => 'cart_fixed',
'discount_amount' => $this->getDiscountAmount(),
'discount_qty' => 0,
'discount_step' => null,
'apply_to_shipping' => 0,
'simple_free_shipping' => 0,
'stop_rules_processing' => 0,
'rule' => array(
'actions' => array(
array(
'type' => 'salesrule/rule_condition_product_combine',
'aggregator' => 'all',
'value' => 1,
'new_child' => null
)
)
),
'store_labels' => array('E-Liquids With Kits')
);
$model = Mage::getModel('salesrule/rule');
$validateResult = $model->validateData(new Varien_Object($data));
if ($validateResult == true) {
if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
&& isset($data['discount_amount'])) {
$data['discount_amount'] = min(100, $data['discount_amount']);
}
if (isset($data['rule']['conditions'])) {
$data['conditions'] = $data['rule']['conditions'];
}
if (isset($data['rule']['actions'])) {
$data['actions'] = $data['rule']['actions'];
}
unset($data['rule']);
$model->setData($data);
$model->save();
}
}
/*
* Use this to calculate how much money off is allowed based on whats in the cart.
*/
public function getDiscountAmount()
{
$customer = Mage::getSingleton('customer/session')->getCustomer();
$cart = Mage::getModel('checkout/cart');
$discountItems = array();
/* Exclude some Business logic here */
foreach ($cart->getQuote()->getAllVisibleItems() as $_item) {
$product = Mage::getModel('catalog/product')->load($_item->getProduct()->getId());
/* Business Logic to decide if this item is relevent */
$discountItems[] = $_item;
}
// Calculate Discount
$appliedDiscounts = 0;
$discount = 0;
foreach ($discountitem as $_item) {
if ($appliedDiscounts < $allowed) {
$i = 0;
while ($appliedDiscounts < $allowed && $i < $_item->getQty()) {
$appliedDiscounts++;
$discount += $_item->getProduct()->getFinalPrice();
$i++;
}
}
}
return $discount;
}
. 解決
だから将来誰もが同様の問題を抱えている場合、私は私の問題の原因が何であるかを解決することに成功しました。収集合計は、割引が適用された後に数回さないようにするフラグを設定していました。
Mage::getSingleton('checkout/cart')
->getQuote()
->setCouponCode(strlen($couponCode) ? $couponCode : '')
->collectTotals()
->save();
.
上記の交換:
Mage::getSingleton('checkout/cart')
->getQuote()
->setCouponCode(strlen($couponCode) ? $couponCode : '')
->save();
.
これは、顧客の信用を適用しようとすると、合計を計算し、ゼロ支払いチェックアウトの対象となることを意味します。