Jump to content

Automatic Creation of Cart Rule Upon Product Purchase


smallbizwiz

Recommended Posts

All -

Please view the statement of work to create a Cart Rule anytime a product is purchased from a specific category. This is version 1, and I just want a lot of it hard-coded to get "something" to the customer. If it works well functionally, we will release another SOW to make it more dynamic.

 

http://www.smallbizwiz.biz/Uploads/statementOfWork-PrestaShopEnhancements-JohnsPassGiftCerts-9-AUG-2013.pdf

 

Thanks!

Teo

Link to comment
Share on other sites

All -

I thought this was a ~4 hour effort, and after getting quotes for over $1k to do this, I decided to do it myself. This took me one hour of research. This forum post helped the most: http://www.prestashop.com/forums/topic/247960-solved-genereate-vouchers-aka-cartrule/page__p__1233268__hl__name%20foreach__fromsearch__1?do=findComment&comment=1233268 as well as http://www.prestashop.com/forums/topic/249088-creating-a-cart-rule-via-php-code/

 

I also spent about 90 minutes coding the module below. While this is hard-coded for the product I need, this works, and just sealed the deal on a customer account. Feel free to use it and add to it.

<?php
/*
* 2007-2012 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <[email protected]>
*  @copyright  2007-2012 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/
if (!defined('_CAN_LOAD_FILES_'))
exit;
class TestModule extends Module
{
private $_merchant_order;
public function __construct()
{
 $this->name = 'testmodule';
 $this->tab = 'administration';
 $this->version = '2.4';
 $this->author = 'PrestaShop';
 $this->need_instance = 0;
 parent::__construct();
 if ($this->id)
  $this->init();
 $this->displayName = $this->l('Test Module');
 $this->description = $this->l('Going to try to hook to order validation');
 $this->confirmUninstall = $this->l('Are you sure you want to delete this test module?');
}
private function init()
{
 $this->_merchant_order = (int)Configuration::get('MA_MERCHANT_ORDER');
}
public function install()
{
 if (!parent::install() ||
  !$this->registerHook('actionValidateOrder'))
  return false;
 Configuration::updateValue('MA_MERCHANT_ORDER', 1);
 return true;
}
public function uninstall()
{
 Configuration::deleteByName('MA_MERCHANT_ORDER');
 return parent::uninstall();
}
public function hookActionValidateOrder($params)
{
 // Getting differents vars
 $id_lang = (int)Context::getContext()->language->id;
 $currency = $params['currency'];
 $order = $params['order'];
 $delivery = new Address((int)$order->id_address_delivery);
 $customer = $params['customer'];
 $order_date_text = Tools::displayDate($order->date_add, (int)$id_lang);
 $carrier = new Carrier((int)$order->id_carrier);
 $products = $params['order']->getProducts();
 $customized_datas = Product::getAllCustomizedDatas((int)$params['cart']->id);
 Product::addCustomizationPrice($products, $customized_datas);
 foreach ($products as $key => $product){
  //Get the order id
  //If the order ID is something we need a gift certificate for, then create the cart rule, else return
  if ($product['product_id'] == '8'){
   $cart_rule = new CartRuleCore();
   /********INFORMATION*************/
   //Get the order title, make this the Name
   $cart_rule_name = $product['product_name'];
   $languages = Language::getLanguages(); 
   foreach ($languages as $key => $language) {$array[$language['id_lang']]= $cart_rule_name;}
   $cart_rule->name=$array; 
   //Cart Rule Description
   $cart_rule->description = $product['product_name'];
   //Get the customer phone number, make this the code
   $cart_rule->code = $delivery->phone;   
   //Set Highlight to No - done by default
   //Set Partial Use to Yes - done by default
   //Set Priority to 1 - done by default
   //Set Status to Yes
   $cart_rule->active = 1;
   /********CONDITIONS*************/
   //Limit to a single customer
   $cart_rule->id_customer = Context::getContext()->customer->id;
   //Valid Dates
   $cart_rule->date_from = date('Y-m-d h:i:s');
   $cart_rule->date_to = date('Y-m-d h:i:s', mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));
   //Minimum Amount
   $cart_rule->minimum_amount = '1';
   $cart_rule->minimum_amount_currency = 1;
   //Total Available
   $cart_rule->quantity = 1;
   //Total Available to each user
   $cart_rule->quantity_per_user = 999;
   /********ACTIONS*************/
   //Free Shipping - No - done by default
   //Apply a Discount - get the order price, make it the discount
   $cart_rule->reduction_amount = $order->total_paid;
   //Apply a discount - Order (without shipping) - done by default
   //Send a free gift - no - done by default
   $cart_rule->add();
  }//End if order id qualifies
 }//End foreach
 return true;
}
}

 

Bottom line to end users: know what something SHOULD cost and if all else fails, if you want it done right, you gotta do it yourself!

Developers trying to make a buck: don't gouge on prices. If you don't understand the requirements, just ask.

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...
  • 1 year later...

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...