Jump to content

[SOLVED] Problem in creating cart rule dynamically


Recommended Posts

Hi,

 

I am using prestashop 1.5.4. When creating cart rule dynamically using below code, it create the code but on applying it on cart  it is not showing the code row in cart and also not applying discount on total. If I simply update (open that cart rule in edit mode and just click on update) the cart rule from back office and refresh the cart page in front office , it start displaying.
 

how can I fix it?


$cart_rule = new CartRule();
    
$languages = Language::getLanguages();
foreach ($languages as $key => $language)
{$array[$language['id_lang']]= "TestCode";}
            
$cart_rule->name=$array;

$cart_rule->description = "Test Code";
            
$cart_rule->code = '123456789';
            
$cart_rule->id_currency = $this->context->currency->id;
        
$cart_rule->active = 1;
                        
$cart_rule->date_from = date('Y-m-d 00:00:00');
$cart_rule->date_to = date('Y-m-d h:i:s', mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));
            
$cart_rule->minimum_amount = '1';
$cart_rule->minimum_amount_currency = 1;
                    
$cart_rule->quantity = 1;
            
$cart_rule->quantity_per_user = 1;
            
$cart_rule->reduction_amount = 10;
            
$cart_rule->add();

Best Regards,

Link to comment
Share on other sites

Hi,

 

I figured it out. Just to add :

$cart_rule->reduction_currency = 1;

Actually I want to generate cart rule at the submission of voucher by user in cart page. Is their any hook in prestashop that  solve my purpose or I have to override order controller.   

 

 

Thanks for help!

 

 

Best Regards,

Edited by prestadev001 (see edit history)
Link to comment
Share on other sites

  • 1 year later...
and how to add a gift using this method?
I try, but it does not work.
$cart_rule = new CartRule();

						$languages = Language::getLanguages();
						foreach($languages as $key => $language){
							$array[$language['id_lang']] = "Gift";
						}

						$cart_rule->name = $array;

						$cart_rule->description = "Gift";

						$cart_rule->partial_use = 1;

						$cart_rule->reduction_currency = intval(Configuration::get('PS_CURRENCY_DEFAULT'));
						
						$cart_rule->active = 1;

						$cart_rule->date_from = "$startdate";
						$cart_rule->date_to = "$enddate";

						$cart_rule->minimum_amount = '1';
						$cart_rule->minimum_amount_currency = '1';

						$cart_rule->quantity = '1';

						$cart_rule->quantity_per_user = '1';

						$cart_rule->product_restriction = '1';
						$cart_rule->reduction_percent = '';
						$cart_rule->priority = '1';
						$cart_rule->reduction_product = '';
						$cart_rule->gift_product = $id_gift;
						$cart_rule->date_add = 'NOW()';
						$cart_rule->date_upd = 'NOW()';
						//d($cart_rule);
						$cart_rule->add();

variables  $startdate, $enddate, $id_gift are defined

Link to comment
Share on other sites

×
×
  • Create New...