1

こことGoogleの両方でこれに似た質問を上下に検索してきましたが、似たようなものが見つからなかったことに驚いています。

私は顧客グループと段階的価格設定に精通していますが、クライアントが設定した目標に適合していません。

私たちが望んでいるのは、ユーザーがMagentoストアに来て、通常の価格で通常のホームページを表示することです。その時点で、ユーザーがクーポンコードを追加するための目立つテキストフィールドが必要です。このコードで、サイトが更新され、通常の価格が打ち消された(または他の視覚的な方法で「大幅に削減された」新しい割引価格が表示されます。

顧客グループ/段階的価格設定は、顧客がログインする必要があるため、解決策ではありません。NOTLOGGED INグループも、すべてのユーザーに割引が表示されるため、役に立ちません。

これはショッピングカートでも発生しません。それまでには手遅れであり、カタログレベルで発生する必要があるためです。

現在OSCommerceを使用しており、まもなくMagentoに移行します。現在、この動作をエミュレートするために行っているのは、ユーザーが地域をクリックするかクーポンコードを入力できるストアアクセスページの通常のWebサイトにテキストフィールドを配置することです。コードを入力すると、特別価格のカスタムストアにリダイレクトされます。

ストアビューを作成して同じ機能を使用することで、Magentoで現在のメソッドを簡単に再現できることは知っていますが、はるかに強力な新しいプラットフォームに移行するというアイデアの場合、これを行うのは残念なことのようです。

これを行う拡張機能は見たことがありません。このようなことが達成できるかどうか、もしそうなら、どのように達成できるかについて誰かが洞察を持っていますか?

4

2 に答える 2

2

ジョナソン、あなたがどのようにそれを行ったのか興味があります。私はあなたのアプローチを取りませんでした。私のアプローチはもう少し複雑です。私の場合、誰かがURLにクーポンコードを投稿することも許可していますが、Cookieとそのすべてを設定しています。私は基本的にヘッダーに独自のフォームを設定し、ユーザーがクーポン コードを入力して適用できるようにし、メール キャンペーンの URL にクーポンを挿入しました。

詳細に戻るにはしばらく時間がかかるので、Jonathan の言う方法を試してみるのに役立つかもしれないいくつかのコード スニペットを投稿します。

カート コントローラーをオーバーライドして、独自のアクションを追加します。

 public function couponExternalPostAction()
{
        $quote =  $this->_getQuote();
        $couponCode = (string) $this->getRequest()->getParam('coupon_code');
        $validateCoupon = Mage::getModel('package_module/coupon');
        $json = $validateCoupon->addCouponCode($couponCode, $quote, $this->getRequest());

        echo $json;
        return;
}

また、通常の方法で正しく機能させるために、couponPostAction() をオーバーライドする必要がありました。

自分のモデルに addCoupon メソッドがあります

 public function addCouponCode($code, $quote, $request){
    $couponCode = (string) $code;
    $removed = false;

    if ($request->getParam('remove') == 1) {
        $couponCode = '';
        $removed = true;
    }

    $oldCouponCode = $quote->getCouponCode();

    /* No point in applying the rule again if it is the same coupon code that is in the quote */
    if ($couponCode === $oldCouponCode) {
        $json = $this->_getResponseJson($removed, $couponCode, $quote, false, true);
        return $json;
    }
    // Set the code get the rule base on validation even if it doesn't validate (false), which will also add it to the session,  then get our response
    $quote->setCouponCode(strlen($couponCode) ? $couponCode : '');
    $rule = $this->_validateCoupon($quote,$couponCode);
    // add coupon code to cookie, so we can delete from quote if the user closes their browser and comes back
    if($rule && !$removed){
        Mage::getModel('core/cookie')->set('coupon_code', $couponCode, 0, '/', null, null, null, false);
    }else{
       Mage::getModel('core/cookie')->delete('coupon_code');
    }
    $json = $this->_getResponseJson($removed, $couponCode, $quote, $rule);

    //See if the quote id is set  before saving
    $quoteId = $quote->getQuoteId();

    //Save the quote since everything has been set if not the data wont be set on page refresh
    $quote->save();

    //Set the quote id if it wasn't set before saving the quote. This makes sure we work off the same quote and a new one isn't created.
    if(empty($quoteId)){
        $this->_setQuoteId($quote);
    }

    return $json;
}

クーポンの検証

 protected function _validateCoupon($quote,$couponCode){
    $store = Mage::app()->getStore($quote->getStoreId());
    $validator = Mage::getModel('package_module/validator');
    $validator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode());

    return $validator->isValidExternalCode($couponCode, $quote->getShippingAddress(),false);
}

Mage_SalesRule_Model_Validator独自のバリデータ関数で拡張しました

 public function isValidExternalCode($couponCode, $address, $setCoupon = true){
    foreach ($this->_getRules() as $rule) {
        if ($rule->getCode() && (in_array(strtolower($couponCode),explode(',',strtolower($rule->getCode()))))) {
            if($setCoupon){
                $address->setCouponCode($couponCode);
            }
            return $rule;
        }
    }
    return false;
}

ここで、json 応答を生成します

rotected function _getResponseJson($removed, $couponCode, $quote, $rule = false, $isDup = false){
    $json = '{"Response":{';
    if($removed){
        $json .= '"success":"Promotional code was cancelled successfully."';
        Mage::getSingleton('checkout/session')->setData('coupon_rule',null);
    }
    if(!$removed && $isDup){
        $json .= '"error":"' . $couponCode . ' is already applied"';
    }else if(!$removed && $rule){
        $json .= '"success":"Promotional code ' . $couponCode . ' has been applied",';
        $json .= '"couponMessage":"<span>' . $rule->getName() . '</span>"';
        Mage::getSingleton('checkout/session')->setData('coupon_rule','<span>' . $rule->getName() .'</span>');
    }else if(!$removed){
        $json .= '"error":"' . $couponCode . ' is not valid"';
        $quote->setCouponCode('');
    }
    $json .= '}}';
    return $json;
}

また、collect メソッドをオーバーライドする必要がありましたMage_SalesRule_Model_Quote_Discount

public function collect(Mage_Sales_Model_Quote_Address $address)
{
    Mage_Sales_Model_Quote_Address_Total_Abstract::collect($address);
    $quote = $address->getQuote();
    $store = Mage::app()->getStore($quote->getStoreId());


    $eventArgs = array(
        'website_id'        => $store->getWebsiteId(),
        'customer_group_id' => $quote->getCustomerGroupId(),
        'coupon_code'       => $quote->getCouponCode(),
    );

    $this->_calculator->init($store->getWebsiteId(), $quote->getCustomerGroupId(), $quote->getCouponCode());

    $items = $address->getAllItems();
    /* EDITS
     * Moved the if statement for no items in cart down past these previous methods and then if the address type is shipping and the coupon is set
     * add the coupon code to the address to allow the validation to still pick up the coupon code
     */
    if($quote->getCouponCode() && ($address->getAddressType() == Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)){
        $address->setCouponCode($quote->getCouponCode());
    }
    if (!count($items)) {
        return $this;
    }

    $address->setDiscountDescription(array());

    foreach ($items as $item) {
        if ($item->getNoDiscount()) {
            $item->setDiscountAmount(0);
            $item->setBaseDiscountAmount(0);
        }
        else {
            /**
             * Child item discount we calculate for parent
             */
            if ($item->getParentItemId()) {
                continue;
            }

            $eventArgs['item'] = $item;
            Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);

            if ($item->getHasChildren() && $item->isChildrenCalculated()) {
                foreach ($item->getChildren() as $child) {
                    $this->_calculator->process($child);
                    $eventArgs['item'] = $child;
                    Mage::dispatchEvent('sales_quote_address_discount_item', $eventArgs);
                    $this->_aggregateItemDiscount($child);
                }
            } else {
                $this->_calculator->process($item);
                $this->_aggregateItemDiscount($item);
            }
        }
    }

    /**
     * Process shipping amount discount
     */
    $address->setShippingDiscountAmount(0);
    $address->setBaseShippingDiscountAmount(0);
    if ($address->getShippingAmount()) {
        $this->_calculator->processShippingAmount($address);
        $this->_addAmount(-$address->getShippingDiscountAmount());
        $this->_addBaseAmount(-$address->getBaseShippingDiscountAmount());
    }

    $this->_calculator->prepareDescription($address);
    return $this;
}
于 2011-02-01T02:33:47.393 に答える
1

これは必ず達成できます。これには、クーポン フィールドの値を受け入れ、そのユーザーのチェックアウト セッションを開始し ( )、クーポン コードをチェックアウト セッションに保存する( ) コントローラを使用して、カスタム モジュール (ここここから開始) を作成する必要があります。 $session = Mage::getSingleton('checkout/session')$session->setData('coupon_code',$coupon

次に、価格モデルを拡張して、セッションでクーポン コードを確認します。構文Mage_Catalog_Model_Product_Type_Priceを使用して、独自のモジュールでオーバーライドできます。<rewrite>クーポン コード ( $couponCode = Mage::getSingleton("checkout/session")->getData("coupon_code");) を取得します。Price オブジェクトは、Bundle とその他の非 Simple 製品タイプでは異なることに注意してください。

さらに情報が必要な場合は、コード サンプルを投稿できます。

于 2011-02-01T00:34:50.413 に答える