Name

CartTrigger — specify subroutines to invoke when cart contents change

SYNOPSIS

subroutine_name...

DESCRIPTION

The directive specifies names of the Perl subroutines to invoke when cart contents change. The subroutines can be defined on both global and catalog level.

The subroutines execute whenever the contents of cart are changed via the standard means available through CGI variable space (i.e. when changes are invoked via the [process] system ActionMap — through mv_order_item and mv_order_quantity field submissions, or from a standard Interchange cart page).

The subroutines will be executed per-change, such that any page process resulting in multiple alterations to the cart will potentially call these functions multiple times.

The following arguments are passed to all specified subroutines:

  • A reference to cart

  • A scalar representing the action that fired the trigger; its value will be one of add, update or delete

  • A hashref pointing to the new row (except for the delete action, in which case this will be undefined)

  • A hashref representing the old row (except for the add action); for the update action, this will be a copy of the row prior to the change. The old row will no longer be a member of the cart

  • The symbolic name of the cart

The return value from each subroutine call is pushed onto an array; when the particular trigger firing is complete (i.e. all subroutines specified in CartTrigger have been called), the full array of results will be returned. However, the initial version of this functionality does not use these return values in any meaningful way.

DIRECTIVE TYPE AND DEFAULT VALUE

Catalog directive

EXAMPLES

Example: Quantity of subitems follows their master item

The quantity of sub items are automatically changed to the quantity of the corresponding master item regardless of the current quantity.

Sub <<EOS
sub cascade_quantities {
	my ($cartref, $action, $newref, $oldref, $cartname) = @_;

	# act upon the main cart only
	return unless $cartname eq 'main';

	if ($action eq 'update' && $newref->{mv_si} == 0
		&& $newref->{quantity} != $oldref->{quantity}) {
		# update quantities of sub items
		for my $subref (grep {$_->{mv_mi} eq $newref->{mv_mi}} @$cartref) {
			$subref->{quantity} = $newref->{quantity};
		}
	}
}
EOS

CartTrigger cascade_quantities
CartTriggerQuantity Yes

NOTES

It must be noted that the Interchange cart subsystem is based on arrayrefs of hashrefs (all Perl programming terms) — there is no object encapsulation for limiting or monitoring program access to the contents of any cart. Consequently, direct manipulation of the cart from within Perl will not cause these triggers to fire. The triggers only fire when the cart contents are modified through the standard Interchange CGI-based variable processing. Therefore, it is assumed (for the moment, at least) that any programmer sufficiently comfortable or confident to manipulate cart contents directly can also be given the responsibility of deciding whether or not it is appropriate to invoke cart triggers along the way.

AVAILABILITY

CartTrigger is available in Interchange versions:

4.6.0-5.9.0 (git-head)

SOURCE

Interchange 5.9.0:

Source: lib/Vend/Config.pm
Line 714

['CartTrigger',     'routine_array',   ''],

Source: lib/Vend/Config.pm
Line 3785 (context shows lines 3785-3819)

sub parse_routine_array {
my($item,$settings) = @_;

return '' unless $settings;

my $c;
if(defined $C) {
  $c = $C->{$item};
}
else {
  no strict 'refs';
  $c = ${"Global::$item"};
}

my @mac;

if($settings =~ /^[-\s\w,]+$/) {
  @mac = grep /\S/, split /[\s,]+/, $settings;
}
else {
  push @mac, $settings;
}

if(ref($c) eq 'ARRAY') {
  push @$c, @mac;
}
elsif($c) {
  $c = [$c, @mac];
}
else {
  $c = scalar(@mac) > 1 ? [ @mac ] : $mac[0];
}

return $c;
}

AUTHORS

Interchange Development Group

SEE ALSO

CartTriggerQuantity(7ic), ItemAction(7ic)

DocBook! Interchange!