Jump to content

Manually add products to cart with discount


Recommended Posts

Hi there!

 

I developed a set configurator module for a customer, which allows users to configure their own set with right products (different sizes, different types, etc...).

 

Everything fine: the user completes all steps and then adds automatically all products to cart.

 

 

Step 9:

 

TPL:

{foreach from=$smarty.post.pcs key=product_id item=pieces}
    <input class="total-prod-pcs" type="hidden" name="pcs" data-prod-id="{$product_id}" data-prod-price="{convertPrice price=$ProductPrice}" value="{$pieces}" id="pcs{$product_id}">
{/foreach}
<button type="button" class="add-to-cart-configurator">GO!</button> 

JAVASCRIPT:

$('.add-to-cart-configurator').click(function() {
    $('.loading-page').show();
    $(".total-prod-pcs").each(function(){
        var pcs = $(this).val();
        var id = $(this).data("prod-id");
        var totalprice = $('#totalprice').val();
        var fullprice = $('#fullprice').val();
        $.get('{/literal}{$link->getPageLink('cart')|escape:'html':'UTF-8'}{literal}?add=1&qty=' + pcs + '&id_product=' + id + '&token={/literal}{$token}{literal}');
        }).promise().done(function () { 
            window.location.href = '{/literal}{$link->getModuleLink('twconfigurator', 'configurator')}{literal}?step=10&fullp={/literal}{if $smarty.post.fullprice}{$smarty.post.fullprice}{/if}{literal}&discp={/literal}{if $smarty.post.totalprice}{$smarty.post.totalprice}{/if}{literal}';
     });
});

Here comes the problem: as my customer requests, ALL customers that buy a complete set (using configurator) become a 10% discount on all inserted products.

 

I tried to add a cart rule to cart to assign a discount.

 

Step 10:

 

TPL

<a class="configurator-button-reverse" href="{$link->getModuleLink('twconfigurator', 'discount')}?gill={$cookie->id_cart}">
     <i class="fa fa-shopping-cart"></i> {l s='Terminate and go to cart' mod='twconfigurator'}
</a> 

PHP

protected function SetCartDiscount($cart) {
     Db::getInstance()->execute('
          INSERT INTO `'._DB_PREFIX_.'cart_cart_rule` (id_cart_rule,id_cart)
          VALUES (1,'.(int)$cart.')
          ON DUPLICATE KEY UPDATE
          id_cart_rule = VALUES(id_cart_rule),
          id_cart = VALUES(id_cart)'
     );
     header('Location: '.$this->context->link->getPageLink('order-opc',false));
}

This is not working, in fact every time i click the button nothing happens BUT, if I go back to step 10 and click the button again it works.

 

I tried to remove the header redirect to see how the function affects the database and, until the user will be not redirect to cart, the cart rule is inserted.

 

It seems that the cart doesn't accept the rule.

 

Can you help me with this? How can I fix it?

 

If you have other ideas, you're obviously welcome! :)

 

Thanks in advance!

Edited by antonio9357 (see edit history)
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...