2

部分作成メモの作成方法を教えてください。たとえば、合計注文額が 50 ポンドで、10 ポンドのクレジット メモを作成したい場合です。以下のコードを使用してクレジットメモを作成しています。

function createCreditMemo($orderId) {
$order = Mage::getModel('sales/order')->load($orderId, 'increment_id');
if (!$order->getId()) {
    print_r('order_not_exists');
}
if (!$order->canCreditmemo()) {
    Mage::log('cannot_create_creditmemo', null, 'product.txt');
    print_r('cannot_create_creditmemo');
}
$data = array();
$refundToStoreCreditAmount = '10.22';

$service = Mage::getModel('sales/service_order', $order);

$creditmemo = $service->prepareCreditmemo($data);

// refund to Store Credit
if ($refundToStoreCreditAmount) {
    // check if refund to Store Credit is available
    if ($order->getCustomerIsGuest()) {
        print_r('cannot_refund_to_storecredit');
    }
    $refundToStoreCreditAmount = max(
        0,
        min($creditmemo->getBaseCustomerBalanceReturnMax(), $refundToStoreCreditAmount)
    );


    if ($refundToStoreCreditAmount) {
        $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice($refundToStoreCreditAmount);

        $creditmemo->setBaseCustomerBalanceTotalRefunded($refundToStoreCreditAmount);
        $refundToStoreCreditAmount = $creditmemo->getStore()->roundPrice(
            $refundToStoreCreditAmount*$order->getStoreToOrderRate()
        );
        // this field can be used by customer balance observer
        $creditmemo->setBsCustomerBalTotalRefunded($refundToStoreCreditAmount);
        // setting flag to make actual refund to customer balance after credit memo save
        $creditmemo->setCustomerBalanceRefundFlag(true);
        print_r($refundToStoreCreditAmount.'<br/>');
        //die('2');
    }


}
//Mage::log($creditmemo, null, 'product.txt');
$creditmemo->setPaymentRefundDisallowed(true);

$creditmemo->register();

$orderCreditMemoStatusCode = Mage_Sales_Model_Order::STATE_CLOSED;
$orderCreditMemoStatusComment = 'Order Refunded.';
$saveTransaction = Mage::getModel('core/resource_transaction')->addObject ($creditmemo )->addObject ( $order )->save ();
$order->addStatusToHistory ( $orderCreditMemoStatusCode, $orderCreditMemoStatusComment, true );

$notifyCustomer = true;
$comment = 'testing refund';
$includeComment = true;

$creditmemo->setEmailSent(true);

// add comment to creditmemo
if (!empty($comment)) {
    $creditmemo->addComment($comment, $notifyCustomer);
}

try {
    Mage::getModel('core/resource_transaction')
        ->addObject($creditmemo)
        ->addObject($order)
        ->save();
    // send email notification

    $creditmemo->sendEmail($notifyCustomer, ($includeComment ? $comment : ''));
} catch (Mage_Core_Exception $e) {
    print_r('data_invalid', $e->getMessage());
}

echo $creditmemo->getIncrementId();

}

以下のコードでは、設定し$refundToStoreCreditAmount = 10;ても全額が返金されます。部分的に返金する方法はありますか?

4

0 に答える 0