Jump to content

Free module for gift products


Recommended Posts

Basically you may add a sentence in product description (A gift_product_name offers if you buy this product).

 

If you want to add a badge or anything else, you will have to develop the feature by yourself or find a module on PS marketplace)

Link to comment
Share on other sites

  • 1 year later...

Hi guys,

I've made something to achieve that.
Imagine that you have created a cart rule when your customer buy one product you offer another or the same...that info only comes to client in shopping detail forward...so with the following modifications i can show in detail of the product that was an offer or in product list...

In file: classes/product.php

I've added this code:

/* this is for know if the id has a gift */public function getFreeProductWithThisOne($id_product){
        $sql = 'SELECT  `id_item` FROM ps_cart_rule_product_rule_value
            LEFT JOIN ps_cart_rule_product_rule ON ps_cart_rule_product_rule_value.id_product_rule = ps_cart_rule_product_rule.id_product_rule
            LEFT JOIN ps_product ON ps_cart_rule_product_rule_value.id_item = ps_product.id_product
            LEFT JOIN ps_cart_rule_product_rule_group ON ps_cart_rule_product_rule.id_product_rule_group = ps_cart_rule_product_rule_group.id_product_rule_group
            LEFT JOIN ps_cart_rule ON ps_cart_rule.id_cart_rule = ps_cart_rule_product_rule_group.id_cart_rule
            WHERE code = "" AND (ps_cart_rule_product_rule_value.id_item = '.(int)$id_product.' and ps_cart_rule_product_rule.type = "products")';
            $result = Db::getInstance()->getRow($sql, $use_cache = 0);
            return $result['id_item'];
    }

/* this is to know what is the gift, i want reference */
    public function whatIsTheGift($id_product){
        $sql = 'SELECT `reference` from ps_product where `id_product` IN 
        (SELECT gift_product FROM ps_cart_rule_product_rule_value 
        LEFT JOIN ps_cart_rule_product_rule ON ps_cart_rule_product_rule_value.id_product_rule = ps_cart_rule_product_rule.id_product_rule 
        LEFT JOIN ps_product ON ps_cart_rule_product_rule_value.id_item = ps_product.id_product 
        LEFT JOIN ps_cart_rule_product_rule_group ON ps_cart_rule_product_rule.id_product_rule_group = ps_cart_rule_product_rule_group.id_product_rule_group 
        LEFT JOIN ps_cart_rule ON ps_cart_rule.id_cart_rule = ps_cart_rule_product_rule_group.id_cart_rule 
        WHERE code = "" AND (ps_cart_rule_product_rule_value.id_item = '.(int)$id_product.' and ps_cart_rule_product_rule.type = "products"))';
            $result = Db::getInstance()->getRow($sql, $use_cache = 0);
            return $result['reference'];
    }

In tpl file of your theme (product.tpl)
where you want to show add this, and this will call the functions on product.php (above)

{if $product->id == (Product::getFreeProductWithThisOne(Tools::getvalue('id_product')))}
                    <div class="box--gift">
                        <!--i class="fa fa-gift"></i><span>REF: {Product::whatIsTheGift(Tools::getvalue('id_product'))}</span-->
                        <img src="/img/botao_ofertas.png" alt="etiqueta de oferta" title="Na compra de {Product::whatsTheQuantity(Tools::getvalue('id_product'))} unidades, oferta de 1 uni. da Ref. {Product::whatIsTheGift(Tools::getvalue('id_product'))}">
                    </div>
                    {/if}

In tpl file of your theme (product-list.tpl)
{assign var='productGift' value=Product::getFreeProductWithThisOne($product.id_product)}
                       {if isset($productGift) && $productGift > 0}
                       <span class="product-label is_gift_boxzinha" title="Produto com Oferta"> 
                           <i class="fa fa-gift"></i> 
                           <span class="is_gift_label">OFERTA</span> 
                       </span>
                       {/if}

 

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

  • 1 month later...
On 2/25/2019 at 5:39 PM, ruisonika said:

Hi guys,

I've made something to achieve that.

In file: classes/product.php

I've added this code:

/* this is for know if the id has a gift */
public static function getFreeProductWithThisOne($id_product){
        $sql = 'SELECT  `id_item` FROM ps_cart_rule_product_rule_value
            LEFT JOIN ps_cart_rule_product_rule ON ps_cart_rule_product_rule_value.id_product_rule = ps_cart_rule_product_rule.id_product_rule
            LEFT JOIN ps_product ON ps_cart_rule_product_rule_value.id_item = ps_product.id_product
            LEFT JOIN ps_cart_rule_product_rule_group ON ps_cart_rule_product_rule.id_product_rule_group = ps_cart_rule_product_rule_group.id_product_rule_group
            LEFT JOIN ps_cart_rule ON ps_cart_rule.id_cart_rule = ps_cart_rule_product_rule_group.id_cart_rule
            WHERE code = "" AND (ps_cart_rule_product_rule_value.id_item = '.(int)$id_product.' and ps_cart_rule_product_rule.type = "products")';
            $result = Db::getInstance()->getRow($sql, $use_cache = 0);
            return $result['id_item'];
    }

/* this is to know what is the gift, i want reference */
    public static function whatIsTheGift($id_product){
        $sql = 'SELECT `reference` from ps_product where `id_product` IN 
        (SELECT gift_product FROM ps_cart_rule_product_rule_value 
        LEFT JOIN ps_cart_rule_product_rule ON ps_cart_rule_product_rule_value.id_product_rule = ps_cart_rule_product_rule.id_product_rule 
        LEFT JOIN ps_product ON ps_cart_rule_product_rule_value.id_item = ps_product.id_product 
        LEFT JOIN ps_cart_rule_product_rule_group ON ps_cart_rule_product_rule.id_product_rule_group = ps_cart_rule_product_rule_group.id_product_rule_group 
        LEFT JOIN ps_cart_rule ON ps_cart_rule.id_cart_rule = ps_cart_rule_product_rule_group.id_cart_rule 
        WHERE code = "" AND (ps_cart_rule_product_rule_value.id_item = '.(int)$id_product.' and ps_cart_rule_product_rule.type = "products"))';
            $result = Db::getInstance()->getRow($sql, $use_cache = 0);
            return $result['reference'];
    }

In tpl file of your theme (product.tpl)
where you want to show add this, and this will call the functions on product.php (above)

{if $product->id == (Product::getFreeProductWithThisOne(Tools::getvalue('id_product')))}
 <div class="box--gift" title="Oferta da Ref: {Product::whatIsTheGift(Tools::getvalue('id_product'))} na compra deste produto">
  <i class="fa fa-gift"></i><span>REF: {Product::whatIsTheGift(Tools::getvalue('id_product'))}</span>
  </div>
 {/if}
 

And now i'm stucked how to show a small icon on product list whenever there is a gift with the product...
Can someone help me?

Hi ! What you got with that? I'm trying it but nothing show on the product edit. 

Link to comment
Share on other sites

57 minutes ago, Alex Sanchez said:

Hi ! What you got with that? I'm trying it but nothing show on the product edit. 

Hi Alex...
The code above is only to show something if the product himself has a offer (gift product)...the gifts are made in cart rules...
but when you make a cart rule with gift...the gift it's only showed in shopping cart details, and i think that the customer must see the gift in product list or in product detail.
And that's why i made this code (for me and it works)...

Link to comment
Share on other sites

1 hour ago, ruisonika said:

Hi Alex...
The code above is only to show something if the product himself has a offer (gift product)...the gifts are made in cart rules...
but when you make a cart rule with gift...the gift it's only showed in shopping cart details, and i think that the customer must see the gift in product list or in product detail.
And that's why i made this code (for me and it works)...

Hi ruisonika

 

Oh okay! Will test it!! So to understand better:

1- put the code In file: classes/product.php

2- Put in tpl also.

3- add a cart rule for BUY PRODUCT_ID_233 and get PRODUCT_ID_244 as gift (it's only an example)

 

That's right ? So i will see in the product.tpl or product-list.tpl the GIFT for the product ? 

 

Let me know please! 

 

Regards

Link to comment
Share on other sites

1 hour ago, Alex Sanchez said:

Hi ruisonika

 

Oh okay! Will test it!! So to understand better:

1- put the code In file: classes/product.php

2- Put in tpl also.

3- add a cart rule for BUY PRODUCT_ID_233 and get PRODUCT_ID_244 as gift (it's only an example)

 

That's right ? So i will see in the product.tpl or product-list.tpl the GIFT for the product ? 

 

Let me know please! 

 

Regards

yeah that's right...the only perhaps is that you don't see the product gift (you could change the code to see) but my result is in the images attached.

in product detail.png

in product list.png

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