<?php

// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) {
    exit;
}

// YITH Cost of Goods for WooCommerce
// https://yithemes.com/themes/plugins/yith-woocommerce-cost-of-goods/

if ( !defined( 'YITH_COG_VERSION' ) ) {
    return;
}

if ( ! class_exists( 'PWBE_YITH_COG' ) ) :

final class PWBE_YITH_COG {

    function __construct() {
        add_filter( 'pwbe_product_columns', array( $this, 'pwbe_product_columns' ) );
    }

    function pwbe_product_columns( $columns ) {
        $insert_before_column = __( 'Sale end date', 'woocommerce' );

        $new_columns[] = array(
            'name' => apply_filters( 'yith_cog_add_cost_field_label', esc_html__( 'YITH Cost of Good', 'yith-cost-of-goods-for-woocommerce' ) ) . ' (' . get_woocommerce_currency_symbol() . ')',
            'type' => 'currency',
            'table' => 'meta',
            'field' => 'yith_cog_cost',
            'readonly' => 'false',
            'visibility' => 'parent_variation',
            'sortable' => 'true',
            'views' => array( 'all', 'standard' )
        );

        $insert_index = count( $columns );
        for ( $x = 0; $x < count( $columns ); $x++ ) {
            if ( $columns[ $x ]['name'] === $insert_before_column ) {
                $insert_index = $x + 1;
                break;
             }
        }

        array_splice( $columns, $insert_index, 0, $new_columns );

        return $columns;
    }
}

new PWBE_YITH_COG();

endif;
