Skip to content

Instantly share code, notes, and snippets.

@steveosoule
Last active April 10, 2024 22:07
Show Gist options
  • Save steveosoule/74343ab1bd67c8d61ec3381deb1ccee8 to your computer and use it in GitHub Desktop.
Save steveosoule/74343ab1bd67c8d61ec3381deb1ccee8 to your computer and use it in GitHub Desktop.
Miva - Add & Remove Basket Charges
<mvt:if expr="g.Delete_Charge">
<h2>Delete Charge</h2>
<mvt:do file="g.Module_Library_DB" name="l.gone" value="BasketCharge_Delete_All_Type(g.Basket:basket_id, 'CUSTOM_CHARGE')" />
</mvt:if>
<mvt:if expr="g.Add_Charge">
<mvt:assign name="l.basket_charge:basket_id" value="g.Basket:basket_id" />
<mvt:assign name="l.basket_charge:module_id" value="0" />
<mvt:assign name="l.basket_charge:type" value="'CUSTOM_CHARGE'" />
<mvt:assign name="l.basket_charge:descrip" value="'Custom Charge'" />
<mvt:assign name="l.basket_charge:amount" value="-20" />
<mvt:assign name="l.basket_charge:disp_amt" value="'-20.00'" />
<mvt:assign name="l.basket_charge:tax_exempt" value="0" />
<mvt:comment>insert charge</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.chargecount" value="BasketCharge_Insert( l.basket_charge )" />
</mvt:if>
<mvt:if expr="g.Delete_Charge OR g.Add_Charge">
<mvt:comment>
| Reload Charges List
</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.success" value="BasketChargeList_Load_Basket( g.Basket:basket_id, l.settings:charges )" /><mvt:comment>For some reason, using `l.settings:basket:charges` would never "refresh" the charges on the page, so we'll need to reassign it</mvt:comment>
<mvt:assign name="l.settings:basket:charges" value="l.settings:charges" />
<mvt:comment>
| Regenerate Currency Formatted Charges
</mvt:comment>
<mvt:foreach iterator="charge" array="basket:charges">
<mvt:do file="g.Module_Store_Module_Currency" name="l.settings:charge:formatted_disp_amt" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:charge:amount )" />
</mvt:foreach>
<mvt:comment>
| Reload Basket Totals
</mvt:comment>
<mvt:do file="g.Module_Library_DB" name="l.settings:basket:total" value="Basket_Total( g.Basket:basket_id )" />
<mvt:do file="g.Module_Store_Module_Currency" name="l.settings:basket:formatted_total" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:basket:total )" />
Total: &mvt:basket:formatted_total;<br>
<mvt:do file="g.Module_Library_DB" name="l.settings:basket:sub_total" value="Basket_SubTotal( g.Basket:basket_id )" />
<mvt:do file="g.Module_Store_Module_Currency" name="l.settings:basket:formatted_sub_total" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:basket:sub_total )" />
Sub Total: &mvt:basket:formatted_sub_total;<br>
<mvt:do file="g.Module_Library_DB" name="l.settings:basket:item_total" value="BasketItem_Total( g.Basket:basket_id )" />
<mvt:do file="g.Module_Store_Module_Currency" name="l.settings:basket:formatted_item_total" value="CurrencyModule_AddFormatting( g.Store:currncy_mod, l.settings:basket:item_total )" />
Item Total: &mvt:basket:formatted_item_total;<br>
<mvt:if expr="l.settings:splitpayment:remaining">
<mvt:assign name="l.settings:splitpayment:remaining" value="l.settings:basket:total" />
<mvt:assign name="l.settings:splitpayment:formatted_remaining" value="l.settings:basket:formatted_total" />
Remaining Total: &mvt:splitpayment:formatted_remaining;<br>
</mvt:if>
</mvt:if>
@srilekhanetelixir
Copy link

Hi,

I've tried the above code. I'm able to add a custom charge and update the basket total. I want to add a condition whether the basket contains this custom charge or not. How can I add this?

@steveosoule
Copy link
Author

Hello,

you should be able to do something like this:

<!-- if the l.settings:basket or l.settings:global_minibasket data is already on the page... -->
<mvt:assign name="l.found_custom_charge" value="miva_array_filter( l.settings:basket:charges, 0, l.charge, 'l.charge:type EQ \'CUSTOM_CHARGE\'', l.custom_charges )" />
<!-- OR if you need to load the data... -->
<mvt:do file="g.Module_Library_DB" name="l.found_custom_charge" value="BasketChargeList_Load_Type( g.Basket:basket_id, 'CUSTOM_CHARGE', l.custom_charges )" />

<mvt:if expr="l.found_custom_charge">
	<mvt:assign name="g.mvt_debug" value="glosub( miva_array_serialize( l.custom_charges ), ',', asciichar( 10 ) )" />
	<!--
	@@custom_charges
	===========
	&mvt:global:mvt_debug;
	-->
</mvt:if>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment