digitalpenguin / commerce_referrals

A referral module for Commerce on MODX.
1 stars 1 forks source link

Update referral on order to use a custom order field #4

Closed Mark-H closed 1 year ago

Mark-H commented 5 years ago

0.12 introduced custom order fields. It would be nice to see the referrals module use that to add the referral information to the order view: https://docs.modmore.com/en/Commerce/v1/Developer/Order_Fields.html

You'd probably want a custom field type that returns empty for the customer, but a link pointing to the referral object for the admin.

muzzwood commented 5 years ago

Do you mean in addition to injecting HTML into the order page, or as a replacement? e.g.: https://github.com/digitalpenguin/commerce_referrals/blob/master/core/components/commerce_referrals/src/Admin/Order/ReferralSection.php

Mark-H commented 5 years ago

It should be able to replace that completely. :)

muzzwood commented 5 years ago

Cool! I'll try and get that sorted soon.

muzzwood commented 1 year ago

Finally switched this to use an order field... (Only 4 years later 🙈 )

Screenshot 2023-06-30 at 19-30-15 Commerce  raquo Order 00103 MODX 3

sebastian-marinescu commented 1 year ago

Hi, very nice!

Can this order field be accessed in the email to the store owner?

sebastian-marinescu commented 1 year ago

I just tried it like this:

                {% if order_fields.referred_by %}
                <tr>
                    <td>
                        <div class="commerce-referrals-referrer-field" style="display: flex; flex-direction: column; gap: 5px; background: #f1f1f1; padding: 10px 15px; border-radius: 3px; margin: 20px 0 10px;">
                            <p>
                                <em>Empfohlen von:</em> 
                            </p>

                            <span><b>{{ order_fields.referred_by.company }}</b></span>
                            <span>{{ order_fields.referred_by.contact_name }}</span>
                            <span>{{ order_fields.referred_by.email }}</span>
                            <span>{{ order_fields.referred_by.phone }}</span>
                            <span>{{ order_fields.referred_by.website }}</span>
                        </div>
                    </td>
                </tr>
                {% endif %}

The if is working, but the values are empty. I just read that in Twig this would need a custom render-function registered, or something :/ Do you know if there is another way?

Or do you know by accident, if I can use a modx-tag inside the Twig-template? Then I would (ab)use something like the migxJsonToPlaceholders-snippet for that :D

muzzwood commented 1 year ago

Hmmm I'll have a look into making that available. It is a bit of a customised order field... normally it's just a name => value affair. This one stores an array as json in the value.

What output are you getting from simply {{ order_fields.referred_by }}?

and yup, MODX tags should work in the Twig template files.

sebastian-marinescu commented 1 year ago

When simply outputting {{ order_fields.referred_by }} I got the JSON, as expected:

{"id":1,"class_key":"CommerceReferralsReferrer","properties":null,"name":"Test-Agency","token":"stilvoller","contact_person":"Sebastian G. Marinescu","email":"sebastian....","phone":"+4....","website":"","address1":"","address2":"","country":"","comment":"Test-Referrer vom Entwickler","added_on":0,"position":0,"added_on_formatted":""}

(I dotted out personal information)

And if a JSON I could work inside a snippet. But when I tried with the migxJsonToPlaceholders snippet, that basically json_decode's a JSON-String and sets MODX placeholders - in multiple variations/tries - I always got this error:

Error (TypeError) while processing status change action "Order Notification to Merchant" in status change "Payment Received" with message: ArrayIterator::construct(): Argument #1 ($array) must be of type array, null given | Trace: #0 /www/core/cache/includes/elements/modsnippet/30.include.cache.php(9): ArrayIterator->construct(NULL)

1 /www/core/model/modx/modscript.class.php(76): include('/www/htdocs/w01...')

2 /www/core/model/modx/modparser.class.php(537): modScript->process(NULL)

3 /www/core/components/pdotools/model/pdotools/pdoparser.class.php(273): modParser->processTag(Array, true)

4 /www/core/model/modx/modparser.class.php(251): pdoParser->processTag(Array, true)

5 /www/core/components/pdotools/model/pdotools/pdoparser.class.php(64): modParser->processElementTags('', '...', true, true, '[[', ']]', Array, 9)

6 /www/core/components/commerce/src/Adapter/Revolution.php(252): pdoParser->processElementTags('', '...', true, true, '[[', ']]', Array, 10)

7 /www/core/components/commerce/model/commerce/comorderemailmessage.class.php(42): modmore\Commerce\Adapter\Revolution->parseMODXTags('...')

8 /www/core/components/commerce/model/commerce/comstatuschangeactionemail.class.php(92): comOrderEmailMessage->send()

9 /www/core/components/commerce/model/commerce/comstatuschange.class.php(81): comStatusChangeActionEmail->process(Object(comProcessingOrder_mysql), Object(comStatus_mysql), Object(comStatus_mysql), Object(comStatusChange_mysql))

10 /www/core/components/commerce/model/commerce/comorder.class.php(936): comStatusChange->processChange(Object(comProcessingOrder_mysql))

11 /www/core/components/commerce/src/Frontend/Steps/Payment.php(386): comOrder->triggerPaidStatusChange()

12 /www/core/components/commerce/src/Frontend/Steps/Payment.php(157): modmore\Commerce\Frontend\Steps\Payment->handleResult(Object(comPaymentMethod_mysql), Object(comTransaction_mysql), Object(modmore\Commerce\Gateways\Manual\ManualTransaction), true, Array)

13 /www/core/components/commerce/src/Frontend/Steps/Payment.php(75): modmore\Commerce\Frontend\Steps\Payment->setPaymentMethod(Object(comPaymentMethod_mysql), Array)

14 /www/core/components/commerce/src/Frontend/Checkout/Process.php(69): modmore\Commerce\Frontend\Steps\Payment->process(Array)

15 /www/core/components/commerce/src/Frontend/Checkout/Process.php(39): modmore\Commerce\Frontend\Checkout\Process->runStep(Object(modmore\Commerce\Frontend\Steps\Payment), Array)

16 /www/core/cache/includes/elements/modsnippet/1157.include.cache.php(33): modmore\Commerce\Frontend\Checkout\Process->run(Array)

17 /www/core/model/modx/modscript.class.php(76): include('/www/htdocs/w01...')

18 /www/core/model/modx/modparser.class.php(537): modScript->process(NULL)

19 /www/core/components/pdotools/model/pdotools/pdoparser.class.php(273): modParser->processTag(Array, true)

20 /www/core/model/modx/modparser.class.php(251): pdoParser->processTag(Array, true)

21 /www/core/components/pdotools/model/pdotools/pdoparser.class.php(64): modParser->processElementTags('', '...', true, false, '[[', ']]', Array, 9)

22 /www/core/model/modx/modresource.class.php(546): pdoParser->processElementTags('', '...', true, false, '[[', ']]', Array, 10)

23 /www/core/model/modx/modresource.class.php(498): modResource->parseContent()

24 /www/core/model/modx/modresponse.class.php(62): modResource->prepare()

25 /www/core/model/modx/modrequest.class.php(138): modResponse->outputContent(Array)

26 /www/core/model/modx/modrequest.class.php(122): modRequest->prepareResponse()

27 /www/core/model/modx/modx.class.php(1461): modRequest->handleRequest()

28 /www/index.php(54): modX->handleRequest()

29 {main}

It seems to me that the snippet is processed before twig-variables, therefore {{ order_fields.referred_by }} is always empty/null for the snippet.

muzzwood commented 1 year ago

Hi @sebastian-marinescu

Add this to DigitalPenguin\Referrals\Order\Field\ReferrerField:

public function renderForCustomer()
{
    return $this->renderForAdmin();
}

Then render it in your email template with {{ order_fields.referred_by|purify }} You can style it, as is, in the template with a <style> block. Or override the twig template at admin/orders/fields/referrer.twig

I'll push that change to the repo so it'll be in the next version.