Buy X Get Y problem in Prestashop


  • Default avatar
    harald7    
     10 years ago
    0

    Hi,

    I'm running Prestashop 1.5.4.1 and AwoCoupon Pro 1.2.1.
    Trying to get Buy X Get Y working but I run into some issues.

    I created a coupon that should automatically add a free product if a specific product is added to the basket.

    Coupon Details:
    Function Type > Buy X Get Y
    Coupon Code > FREETEST
    Published > Published
    Percent or Amount > Percent
    Value > 100
    Process Type > First found match

    Optional Fields:
    Automatically add to cart "Get Y" product > checked
    Rest of the fields are blank

    Customers:
    Added all three shopper groups Default, Guest, Visitor

    Assets:
    Buy X
    Number > 1
    Type > Product X
    “Include” radio button > Checked

    Get Y
    Number > 1
    Type > Product Y (enough stock, no attributes, no additional fields and min. qty
    “Include” radio button > Checked

    Shop:
    No specific shop selecteed, allthough if I do the problem persists.

    Then I save the coupon and check the 'Detals' for this coupon in the coupon overview.
    There I notice that a product Z is added to the 'Buy X' item list? Where did this come from?

    Then I go to the frontend and add product X to my basket. Product Y is not automatically added.
    When I add the coupon code it is added.

    Then I empty the basket and add product Z (which was not put in the list) to my basket and then product Y is automatically added to the basket.

    So to conclude, there seems to be 2 issues:
    1) The product list in the 'Buy X' list is automatically populated with more articles then at first selected.
    2) Adding the original added 'Buy X' product to my basket does not automatically add the Get Y product to the basket.

    I have some screenprints that I can mail you of the situation I have as an example when needed.

    Kind regards,
    Harald


  • Your avatar
    seyi    
     10 years ago
    0

    Hello,

    So after saving the coupon code you saw Product Z in the Buy X and not Product Y? That is strange. When you go to edit the coupon do you see the new product Z? Have you tried just recreating the coupon code to see if it happens again? The only thing I can think of that could cause that is languages. Do you have a multi lingual shop?
  • Default avatar
    harald7    
     10 years ago
    0

    Yes, when I edit the coupon there are 2 articles in the Buy X list. They have the same ID though.

    I did several retries and recreated new coupons but I keep reproducing this problem.....
  • Your avatar
    seyi    
     10 years ago
    0

    Ok, I think it definitely has something to do with the product_id. How can 2 different products have the same product id? Do you recognize product Z? Is it possible to look directly in the database at the product list?

    If you dont mind sending a private message with temporary admin and ftp access I will be happy to look at it.
  • Default avatar
    harald7    
     10 years ago
    0

    This is asset1 in the database:

    id coupon_id asset_type asset_id order_by
    35 39 product 144 NULL
    33 38 product 146 NULL
    34 39 product 144 NULL
    32 38 product 146 NULL

    So it shows there 2 asset_id's for the same coupon_id.
    In the backend it shows 2 different product descriptions though.....

  • Your avatar
    seyi    
     10 years ago
    0

    Hello,

    Do you have a multi lingual store?

    Can you look for the product_id's in
    - ps_product
    - ps_product_lang

    What do you see? Do they correspond to what you are seeing as the product descriptions in awocoupon?
  • Default avatar
    harald7    
     10 years ago
    0

    Not Multilanguage but Multi Store. Looks like this has something to do with it.

    In the product_lang I see this:

    id_product id_store id_lang product
    144 1 6 product X
    144 3 6 product Z

    and in the product table it shows this:
    id_product id_supplier id_manufacturer id_category_default id_shop_default
    144 0 6 6 1

    Is Awocoupon Pro multi store compatible?
  • Your avatar
    seyi    
     10 years ago
    0

    Hello,

    Of course AwoCoupon is compatible with multi store, but you are right there is a problem.

    I was able to reproduce it when creating a coupon from the non default store. Both languages are displayed for the product. But looking in the database, there is only 1 row added in asset1 table. I went ahead and also tested the coupon code in both store fronts and it works fine. So I think the main problem is the display. Here is how you can fix that.

    In www/modules/awocoupon/lib/awohelper.php, around line 175 is this function:

    <?php
        
    public static function getAwoItem($table,$coupon_id,$order_by=null) {
    ?>


    After that add this code:
    <?php
            $shop_str 
    '';
            if(
    awoHelper::is_multistore()) {
                
    $id_shop = (int) Context::getContext()->shop->id;
                if(!empty(
    $id_shop)) $shop_str ' AND c.id_shop='.$id_shop.' ';
            }
    ?>


    Then within the same function is this line:
    <?php
                     WHERE a
    .asset_type="product" AND c.id_lang='.(int)$cookie->id_lang.' AND a.coupon_id IN ('.$coupon_ids.')
    ?>


    Change it to this
    <?php
                     WHERE a
    .asset_type="product" AND c.id_lang='.(int)$cookie->id_lang.' AND a.coupon_id IN ('.$coupon_ids.''.$shop_str.'
    ?>



    That should fix the display to only show the product name of the current store
  • Default avatar
    harald7    
     10 years ago
    0

    Hi Seyi,

    Just made the changes and this is looking good, thanks a lot for the quick help!
    My other issue is still not solved though....
    I want to add an article to the cart automatically but this will not happen when putting article X to my cart.

    Coupon Details:
    Function Type > Buy X Get Y
    Coupon Code > FREETEST
    Published > Published
    Percent or Amount > Percent
    Value > 100
    Process Type > First found match

    Optional Fields:
    Automatically add to cart "Get Y" product > checked
    Rest of the fields are blank

    Customers:
    Added all three shopper groups Default, Guest, Visitor

    Assets:
    Buy X
    Number > 1
    Type > Product X
    “Include” radio button > Checked

    Get Y
    Number > 1
    Type > Product Y (enough stock, no attributes, no additional fields and min. qty
    “Include” radio button > Checked


    When adding the couponcode manually product Y is added to the cart.
  • Your avatar
    seyi    
     10 years ago
    0

    Hello,

    Glad that worked. A little confused about your other problem. You said it works when you use the coupon code. Is the problem that you want it to work automatically without using a coupon code?
  • Default avatar
    harald7    
     10 years ago
    0

    Hi Seyi,

    Let's explain a little more clear what I expected ;)
    If I set up a coupon for Buy X get Y and I put product X in my cart I was under the impression that product Y was automatically added to my cart.

    And if I raise the quantity of product X before putting it in my cart or raise the quantity of product X in the cart the quantity of product Y will also raise to the same quantity of product X.

    Kind regards,
    Harald
  • Your avatar
    seyi    
     10 years ago
    0

    Hello Harold,

    That is exactly how it should behave as long as you enter the coupon code. If you want it to be added automatically without entering a coupon code, there is a hack you can use in the override directory:

    For prestashop 1.5.x
    in www/override/controllers/front/ParentOrderController.php, towards the top you should see this:
    <?php
            parent
    ::init();
    ?>


    Change it to this
    <?php
            
    # seyi_code automatic discount
                
    if ((int)$this->context->cookie->id_cart) {
                    
    $cart = new Cart($this->context->cookie->id_cart);
                    
    $this->nbProducts $cart->nbProducts();
                    if (
    $this->nbProducts) {
                        
    $discounts $cart->getCartRules();
                        if(empty(
    $discounts)) {
                            
    $_POST['submitAddDiscount'] = 'add';
                            
    $_POST['discount_name'] = '_my_coupon_code_here_';
                            
    $_POST['autodiscount_error'] = true;
                        }
                    }
                }
            }

            
    parent::init();
        
            { 
    # seyi_code automatic discount
                
    foreach($this->errors as $k=>$err) if($err=='__AUTODISCOUNT_ERROR__') unset($this->errors[$k]);
                
    $this->context->smarty->assign(array(
                    
    'errors' => $this->errors,
                    
    'discount_name' => '',
                ));
            }
    ?>


    Then alter _my_coupon_code_here_ to the actual coupon code. It should then process automatically once there are enough qualifying products in the cart.
  • Default avatar
    harald7    
     10 years ago
    0

    Hi Seyi,

    Thanks for the hack!
    I adjusted it and the product Y is now added to the cart but only when I enter the shopping overview.
    It is not added to the Ajax cart block on the top right corner of my shop directly but only (as said) when going to the cart overview page it is added.

    Is this how the hack is supposed to work?

    Also if I want to add more coupons I can just add more lines of this $_POST['discount_name'] = '_my_coupon_code_here_'; and change the coupon code?

    Kind regards,
    Harald
  • Your avatar
    seyi    
     10 years ago
    0

    Hello,

    I guess it does not affect ajax, never tested it.

    For the coupon code, you can use a parent coupon and add all the coupons you want to be automatically used to the parent coupon with a process type of "all that apply" then use the parent coupon.
  • Default avatar
    harald7    
     10 years ago
    0

    Hi Seyi,

    I only have the 'Apply all' and 'Apply only if ALL apply' option for process type of a parent coupon.
    Is one of them doing the same as the option you mentioned 'All that apply'?
  • Your avatar
    seyi    
     10 years ago
    0

    Yes, use 'Apply all'.

    'Apply all only if ALL apply' is for special cases.
  • Default avatar
    harald7    
     10 years ago
    0

    Hi Seyi,

    I reversed the hack because new coupons are not working anymore when this hack is active.
    This goes for coupons made in Awocoupon as normal cart rules from the Prestashop function.
  • Your avatar
    seyi    
     10 years ago
    0

    It might have to do with multiple coupons, do you have them enabled in awocoupon->configuration?

    And prestashop coupons and awocoupon will not mix in the same basket, it is one or the other.
  • Default avatar
    julie0    
     9 years ago
    0

    Hi Seyi,

    Is there a way to adjust this hack to add coupon code automatically to the cart for Buy 1 get one free?

    I tried this one, but it's not working..

    Thanks

    Perstashop version 1.5.3.1
    AWOCoupon Pro 1.3.0
  • Your avatar
    seyi    
     9 years ago
    0

    Hello,

    Below is the updated code which should replace:
    <?php
    parent
    ::init();
    ?>


    In your ParentOrderController

    <?php
            
    # seyi_code automatic discount
                
    if ((int)$this->context->cookie->id_cart) {
                    if(empty(
    $_POST['discount_name'])) {
                        
    $cart = new Cart($this->context->cookie->id_cart);
                        
    $this->nbProducts $cart->nbProducts();
                        if (
    $this->nbProducts) {
                            
    $discount_coupon '_my_coupon_code_here_';
                            
    $discounts $cart->getCartRules();
                            
    $_process_auto false;
                            if(empty(
    $discounts)) $_process_auto true;
                            elseif(
    strpos($discounts[0]['name'],$discount_coupon)===false$_process_auto true;
                            if(
    $_process_auto) {
                                
    // need to check this otherwise can end up in infinite loop or error published to the customer
                                
    $current_date date('Y-m-d H:i:s');
                                
    $sql 'SELECT coupon_code
                                          FROM '
    ._DB_PREFIX_.'awocoupon 
                                         WHERE published=1 
                                           AND coupon_code="'
    .$discount_coupon.'"
                                           AND ( ((startdate IS NULL OR startdate="")     AND (expiration IS NULL OR expiration="")) OR
                                                 ((expiration IS NULL OR expiration="") AND startdate<="'
    .$current_date.'") OR
                                                 ((startdate IS NULL OR startdate="")     AND expiration>="'
    .$current_date.'") OR
                                                 (startdate<="'
    .$current_date.'"        AND expiration>="'.$current_date.'")
                                               )'
    ;
                                
    $coupon_code Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
                                if(empty(
    $coupon_code)) $_process_auto false;
                            }
                            
                            
    //echo '<pre>'; print_r($discounts);exit;
                            
    if($_process_auto) {
                                
    $_POST['submitAddDiscount'] = 'add';
                                
    $_POST['discount_name'] = $discount_coupon;
                                
    $_POST['autodiscount_error'] = true;
                            }
                        }
                    }
                }
            }

            
    parent::init();
        
            { 
    # seyi_code automatic discount
                
    foreach($this->errors as $k=>$err) if($err=='__AUTODISCOUNT_ERROR__') unset($this->errors[$k]);
                
    $this->context->smarty->assign(array(
                    
    'errors' => $this->errors,
                    
    'discount_name' => '',
                ));
            }
    ?>


    Again, need to adjust _my_coupon_code_here_ to the actual coupon code.
  • Default avatar
    julie0    
     9 years ago
    0

    Hi Seyi,

    Thanks for the reply!

    I pasted your hack to replace everything in ParentOrderController.php from the Override folder but it's not working.

    Do i needed to replace only a part of the code?

    I tried emptying Prestahop and browser cache but the coupon is still not automatically applied

    Here is the code present before I replace with your hack :

    <?php

    /**

     * @component AwoCoupon Pro

     * @copyright Copyright (C) Seyi Awofadeju - All rights reserved.

     * @Website : https://awodev.com

     **/



    if (!defined('_PS_VERSION_')) exit;





    class 
    ParentOrderController extends ParentOrderControllerCore {



        public function 
    init() {

        

            
    parent::init();

        

            if (
    $this->nbProducts) {

            

                if (
    CartRule::isFeatureActive()) {

                

                    if (
    $id_cart_rule Tools::getValue('deleteDiscount')) {

                        
    $this->context->cart->removeCartRule($id_cart_rule);

                        
    Tools::redirect('index.php?controller=order-opc');

                    }

                    

                }

                

            }

            



        }



    }


    Thanks for everything, very good service.

    -Julie
  • Your avatar
    seyi    
     9 years ago
    0

    Hello,

    I did not paste the full code in the update code above, I have now fixed it:
    https://awodev.com/forum/awocoupon/help-section/buy-x-get-y-problem-prestashop#comment-8000