<?php


namespace ABR\Controller;


class ProductDetails
{
    public $product;
    public $products;



    public function __construct($product)
    {
        $this->products =$product;

    }

    public function get_product_id($item)
    {
        $variation_id =  $item["variation_id"];
        $product_id =  $item["product_id"];
        if($variation_id == "0" && $product_id=="0"){
            return "error";
        }
        elseif ($variation_id == "0") {
            $product_id =  $item["product_id"];
        } else {
            $product_id = $item["variation_id"];
        }

        return $product_id;
    }
    public function get_product_name($product_id)
    {
        $product=$this->get_product($product_id);
        $product_name=$product->get_data()['name'];

        return $product_name;
    }
    public function get_product($product_id)
    {
        $product=wc_get_product($product_id);

        return $product;
    }

    public function get_product_price($product_id)
    {

        $product=$this->get_product($product_id);

        if ($product->is_on_sale()) {
            $price = (float)$product->get_regular_price();
        } else {
            $price = (float)$product->get_price();
        }
        return $price;
    }
    public function get_product_discount($product_id,$item)
    {
        $product=$this->get_product($product_id);

        if (!$product->is_on_sale()) {
            return 0;
        }
        $regular_price = (float)$product->get_regular_price();
        $sale_price = (float)$product->get_price();
        $price=$regular_price - $sale_price;
        $price=$price*$item['quantity'];

        if ($item->get_subtotal() !== $item->get_total()){
            $price=$price+($item->get_subtotal() - $item->get_total());
        }
        return $price;
    }
    public function virual($product_id)
    {
        $product=$this->get_product($product_id);


        if ($product->is_virtual()||$product->is_downloadable())
        {
            return 2;
        }
        else
        {
            return 1;
        }

    }

}