Jump to content

Show available loyalty points in cart summary?


Recommended Posts

1.6.1.5 here,

 

Since we've updated our e-shop, my clients started requiring for a (seemed to be) simple alteration in cart-summary.tpl - I've received multiple requests asking to add total loyalty points (available for the customer) to be shown.

 

So the question is - how to show total available loyalty points in cart summary?

 

I've tried implementing these code changes inside cart-summary.tpl (ignoring the duplicate CSS settings for now):

			{if $displayVouchers}
				<p id="title" class="title-offers">{l s='Take advantage of our exclusive offers:'}</p>
				<div id="display_cart_vouchers">
					{foreach $displayVouchers as $voucher}
						{if $voucher.code != ''}<span class="voucher_name" data-code="{$voucher.code|escape:'html':'UTF-8'}">{$voucher.code|escape:'html':'UTF-8'}</span> - {/if}{$voucher.name}<br />
					{/foreach}
				</div>
			{/if}
		</div>
		
		<!-- Offer to convert loyalty points -->
		
		<div id="cart_voucher" class="cart_voucher mar_b1 {if isset($errors_discount) && $errors_discount}hidden{/if}">
					<p class="cart_voucher_title">{l s='Loyalty points'}</p>
					<p>{l s='You have'} {$available_loyalty_points_atm} {l s='loyalty points available. Would You like to convert them into a discount voucher?'}</p>
					<span class="btn btn-default">{l s='Yes I do!'}</span>
		</div>

		<!-- END OF Offer to convert loyalty points -->
		
		{/if}

there's a $available_loyalty_points_atm variable that is surely unknown for the Cart.php controller, so I've described the variable there, just  above the checkQuantities function:

        $summary = array(
            'delivery' => $delivery,
            'delivery_state' => State::getNameById($delivery->id_state),
            'invoice' => $invoice,
            'invoice_state' => State::getNameById($invoice->id_state),
            'formattedAddresses' => $formatted_addresses,
            'products' => array_values($products),
            'gift_products' => $gift_products,
            'discounts' => array_values($cart_rules),
            'is_virtual_cart' => (int)$this->isVirtualCart(),
            'total_discounts' => $total_discounts,
            'total_discounts_tax_exc' => $total_discounts_tax_exc,
            'total_wrapping' => $this->getOrderTotal(true, Cart::ONLY_WRAPPING),
            'total_wrapping_tax_exc' => $this->getOrderTotal(false, Cart::ONLY_WRAPPING),
            'total_shipping' => $total_shipping,
            'total_shipping_tax_exc' => $total_shipping_tax_exc,
            'total_products_wt' => $total_products_wt,
            'total_products' => $total_products,
            'total_price' => $base_total_tax_inc,
            'total_tax' => $total_tax,
            'total_price_without_tax' => $base_total_tax_exc,
            'is_multi_address_delivery' => $this->isMultiAddressDelivery() || ((int)Tools::getValue('multi-shipping') == 1),
            'free_ship' =>!$total_shipping && !count($this->getDeliveryAddressesWithoutCarriers(true, $errors)),
            'carrier' => new Carrier($this->id_carrier, $id_lang),
	    'available_loyalty_points_atm' = (int)LoyaltyModule::getPointsByCustomer((int)$cookie->id_customer);

        );

        $hook = Hook::exec('actionCartSummary', $summary, null, true);
        if (is_array($hook)) {
            $summary = array_merge($summary, array_shift($hook));
        }

        return $summary;
    }

And it doesn't work. Probably due to the LoyaltyModule::getPointsByCustomer being unknown in Cart.php controller file.

 

So I've added the formula from loyalty-program.php (inside module folder) just right after the CheckDiscountValidity function:

	public static function getPointsByCustomer($id_customer)
	{

		$validity_period = Configuration::get('PS_LOYALTY_VALIDITY_PERIOD');
		$sql_period = '';
		if ((int)$validity_period > 0)
			$sql_period = ' AND datediff(NOW(),f.date_add) <= '.$validity_period;

		return
			Db::getInstance()->getValue('
		SELECT SUM(f.points) points
		FROM `'._DB_PREFIX_.'loyalty` f
		WHERE f.id_customer = '.(int)($id_customer).'
		AND f.id_loyalty_state IN ('.(int)(LoyaltyStateModule::getValidationId()).', '.(int)(LoyaltyStateModule::getNoneAwardId()).')
		'.$sql_period)
			+
			Db::getInstance()->getValue('
		SELECT SUM(f.points) points
		FROM `'._DB_PREFIX_.'loyalty` f
		WHERE f.id_customer = '.(int)($id_customer).'
		AND f.id_loyalty_state = '.(int)LoyaltyStateModule::getCancelId().'
		AND points < 0
		'.$sql_period);
	}

And then re-wrote the variable definition:

'available_loyalty_points_atm' = getPointsByCustomer((int)$cookie->id_customer);

But no luck - the page crashes. Anyone could please help me solve this? Thanks in advance!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...