Jump to content

pass $product.condition.label to other tpl files


Moncef Grey

Recommended Posts

I have a task to create some styling in the shopping cart part based on the value of $product.condition.label

The problem is, this variable doesn't work when i added it to themes\new\templates\checkout\_partials\cart-detailed-product-line.tpl

But it work in other files such as: themes\new\templates\catalog\product.tpl 

How can i pass that variable  $product.condition.label to cart-detailed-product-line.tpl file ?

Edited by Moncef Grey
Typo (see edit history)
Link to comment
Share on other sites

I added p.`condition` at the end to sql query in getProducts method like this: 

$sql->select('cp.`id_product_attribute`, cp.`id_product`, cp.`quantity` AS cart_quantity, cp.id_shop, cp.`id_customization`, pl.`name`, p.`is_virtual`,
                        pl.`description_short`, pl.`available_now`, pl.`available_later`, product_shop.`id_category_default`, p.`id_supplier`,
                        p.`id_manufacturer`, m.`name` AS manufacturer_name, product_shop.`on_sale`, product_shop.`ecotax`, product_shop.`additional_shipping_cost`,
                        product_shop.`available_for_order`, product_shop.`show_price`, product_shop.`price`, product_shop.`active`, product_shop.`unity`, product_shop.`unit_price_ratio`,
                        stock.`quantity` AS quantity_available, p.`width`, p.`height`, p.`depth`, stock.`out_of_stock`, p.`weight`,
                        p.`available_date`, p.`date_add`, p.`date_upd`, IFNULL(stock.quantity, 0) as quantity, pl.`link_rewrite`, cl.`link_rewrite` AS category,
                        CONCAT(LPAD(cp.`id_product`, 10, 0), LPAD(IFNULL(cp.`id_product_attribute`, 0), 10, 0), IFNULL(cp.`id_address_delivery`, 0), IFNULL(cp.`id_customization`, 0)) AS unique_id, cp.id_address_delivery,
                        product_shop.advanced_stock_management, ps.product_supplier_reference supplier_reference, p.`condition`');

 

Yet when i run this code in cart-detailed-product-line.tpl file, nothing shows in the console, even i added the condition ( New ) to all the products in the cart from the BO!
 

<script>
  console.log('{$product.condition.label}');
</script>

 

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

8 minutes ago, Moncef Grey said:

I wanted to use $product.condition.label as a condition in if else statement to display a div that contains text and style it using css.

I repeat again that everything in the cart shows the Cart.php class.
Product.php displays on the main page, product categories and product details page.
They are two different classes.
What you think the $product variable is valid everywhere is a mistake.
$product is array !!!
You cannot use fields from Product.php and use them in Cart.php.

  • Like 1
Link to comment
Share on other sites

UPDATE !!!

Add new function to:

./classes/Cart.php

public function getConditions($id_product)
    {
        $product = new Product($id_product, false, Configuration::get('PS_LANG_DEFAULT'), Context::getContext()->shop->id);
        $show_condition = $product->show_condition;
        $condition = $product->condition;

        if ($product->show_condition == '0') {
            return;
        }

        switch ($product->condition) {
            case 'new':
                return  '<li class="product-flag new" style="top: 0px;margin-top: -1rem;margin-left: -1rem;">'.Context::getContext()->getTranslator()->trans('New', array(), 'Shop.Theme.Catalog').'</li>';

            case 'used':
                return '<li class="product-flag used" style="top: 0px;margin-top: -1rem;margin-left: -1rem;">'.Context::getContext()->getTranslator()->trans('Used', array(), 'Shop.Theme.Catalog').'</li>';
                break;

            case 'refurbished':
                return '<li class="product-flag refurbished" style="top: 0px;margin-top: -1rem;margin-left: -1rem;">'.Context::getContext()->getTranslator()->trans('Refurbished', array(), 'Shop.Theme.Catalog').'</li>';
                break;

            default:
                return;
        }

    }

 

Add to ./themes/classic/templates/checkout/_partials/cart-detailed-product-line.tpl

(show standard conditions New, Used, Refurbished)

after:

<div class="product-line-grid-left col-md-3 col-xs-4">

add:

<!-- block conditions -->
      <ul class="product-flags">
        {Cart::getConditions($product.id) nofilter}
      </ul>
    <!-- end block conditions -->

 

obrazek.png.ec6e2f617f2b1f2f7f2159f7b9e344a9.png

result:

obrazek.png.c066ff47315cf4f3b09e58ac4d322613.png

 

  • Like 1
Link to comment
Share on other sites

Add condition label to ./themes/classic/templates/checkout/_partials/cart-summary-product-line.tpl

after:

<a href="{$product.url}" title="{$product.name}">
      <img class="media-object" src="{$product.cover.small.url}" alt="{$product.name}">
    </a>

add:

<!-- block conditions -->
    <div style="display:block;position: relative;bottom: 0px;margin-left: 1.15rem;margin-top: 1rem;">
      <ul class="product-flags">
        {Cart::getConditions($product.id) nofilter}
      </ul>
    </div>
    <!-- end block conditions -->

obrazek.png.217a17a0fd68bc7e27958b3360450194.png

 

result:

obrazek.png.07841fb8ce7a2d167964a88ec9ac092e.png

  • Like 1
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...