/home/ejrndhmu/.trash/wp-content/themes/instaorder/template-parts/product.php
<?php

/**
 * Template part for displaying single product
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package InstaGram
 */

$thumb = get_the_post_thumbnail_url(get_the_ID(), 'full');
$regular_price = intval(get_post_meta(get_the_ID(), 'product_price', true));
$weight = intval(get_post_meta(get_the_ID(), 'product_weight', true));
if (empty($weight)) {
    $weight = 1000;
}
$promo = get_post_meta(get_the_ID(), 'product_promo', true);
$promo_price = intval(get_post_meta(get_the_ID(), 'product_promo_price', true));
$is_out_of_stock = get_post_meta(get_the_ID(), 'product_is_out_of_stock', true);
if ($promo == 'on') {
    $prices = sprintf(
        '<span class="price">Rp %s</span> <span class="price_slik">Rp %s</span>',
        number_format($promo_price, 0, ',', '.'),
        number_format($regular_price, 0, ',', '.')
    );
} else {
    $prices = sprintf(
        '<span class="price">Rp %s</span>',
        number_format($regular_price, 0, ',', '.')
    );
}

$price = ($promo == 'on') ? $promo_price : $regular_price;

$marketplaces = array();
if (get_post_meta(get_the_ID(), 'product_mp_tokopedia', true)) {
    $marketplaces['tokopedia'] = get_post_meta(get_the_ID(), 'product_mp_tokopedia', true);
}
if (get_post_meta(get_the_ID(), 'product_mp_bukalapak', true)) {
    $marketplaces['bukalapak'] = get_post_meta(get_the_ID(), 'product_mp_bukalapak', true);
}
if (get_post_meta(get_the_ID(), 'product_mp_lazada', true)) {
    $marketplaces['lazada'] = get_post_meta(get_the_ID(), 'product_mp_lazada', true);
}
if (get_post_meta(get_the_ID(), 'product_mp_shoppe', true)) {
    $marketplaces['shoppe'] = get_post_meta(get_the_ID(), 'product_mp_shoppe', true);
}

$colors = get_post_meta(get_the_ID(), 'product_color', true);
$custom_variations = get_post_meta(get_the_ID(), 'product_custom_variation', true);
$custom_variation_name = get_post_meta(get_the_ID(), 'product_custom_variation_name', true);
$custom_variation_values = get_post_meta(get_the_ID(), 'product_custom_variation_value', true);
?>

<article id="post-<?php the_ID(); ?>" <?php post_class('product clear'); ?>>
    <input type="hidden" name="item_id" value="<?php echo get_the_ID(); ?>">
    <div class="content">
        <div class="image">
            <img class="lazy" data-src="<?php echo $thumb; ?>"
                onload="this.parentNode.style.height = this.offsetWidth+'px';" />
            <input type="hidden" name="item_image" value="<?php echo $thumb; ?>" />
            <?php if ($promo == 'on') : ?>
            <span class="ribbon">Promo</span>
            <?php endif; ?>
        </div>

        <div class="detail clear">
            <?php
            the_title('<h1>', '</h1>');
            ?>
            <input type="hidden" name="item_name" value="<?php echo get_the_title(); ?>">
            <input type="hidden" name="item_weight"
                value="<?php echo get_post_meta(get_the_ID(), 'product_weight', true); ?>">
            <div class="pricing">
                <?php echo $prices; ?>
                <input type="hidden" name="item_price" value="<?php echo $price; ?>" />
            </div>
            <?php if ($colors || $custom_variations == 'on') : ?>
            <div class="variations clear">
                <?php if ($colors) : ?>
                <div class="variation variation-color">
                    <label><?php echo __('Warna', 'instaorder'); ?></label>
                    <input type="hidden" name="item_color" value="<?php echo $colors[0]; ?>" />
                    <select>
                        <?php foreach ((array) $colors as $key => $val) : ?>
                        <option value="<?php echo $val; ?>"><?php echo $val; ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <?php endif; ?>
                <?php if ($custom_variations && isset($custom_variation_values[0]['custom_variation_id'])) : ?>
                <div class="variation variation-custom">
                    <label><?php echo $custom_variation_name; ?></label>
                    <input type="hidden" name="item_custom_variation_name"
                        value="<?php echo $custom_variation_name; ?>" />
                    <input type="hidden" name="item_custom_variation_value"
                        value="<?php echo $custom_variation_values[0]['custom_variation_id']; ?>" />
                    <input type="hidden" name="item_custom_variation_key" value="0" />
                    <select>
                        <?php foreach ((array) $custom_variation_values as $key => $val) : ?>
                        <?php
                                    $ngprice = intval($val['custom_variation_price']) > 0 ? intval($val['custom_variation_price']) : $price;
                                    ?>
                        <option value="<?php echo $val['custom_variation_id']; ?>"
                            data-price="<?php echo intval($ngprice); ?>" data-key="<?php echo $key; ?>">
                            <?php echo $val['custom_variation_id']; ?>
                        </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <?php endif; ?>
            </div>
            <?php endif; ?>
            <div class="actions clear">
                <div class="action">
                    <div class="quantity-chooser clear">
                        <div class="minus">-</div>
                        <input min="1" type="number" value="1" name="item_quantity">
                        <div class="plus">+</div>
                    </div>
                </div>
                <div class="action">
                    <button type="button" id="add-to-cart">
                        Tambah ke keranjang
                    </button>
                </div>
            </div>
            <?php if ($marketplaces) : ?>
            <div class="order-mp-label"><?php echo __('Anda juga dapat belanja melalui marketplace', 'instaorder'); ?>
            </div>
            <div class="order-mp">
                <?php foreach ($marketplaces as $key => $val) : ?>
                <a target="_blank" rel="nofollow" href="<?php echo esc_url($val); ?>" class="lazy"
                    data-bg="url('<?php echo instaorder_get_image_source($key . '-16.png'); ?>">&nbsp;</a>
                <?php endforeach; ?>
            </div>
            <?php endif; ?>
            <div class="desc-label">
                Keterangan
            </div>
            <div class="desc">
                <?php the_content(); ?>
            </div>
        </div>
        <?php

        $categories = get_the_terms(get_the_ID(), 'product-category');

        if ($categories) :
            $category_ids = array();
            foreach ($categories as $category) :
                $category_ids[] = $category->term_id;
            endforeach;

            $argss = array(
                'post_type'      => 'instaorder-product',
                'posts_per_page' => 6,
                'post__not_in'   => array(get_the_ID()),
                'post_status'    => 'publish',
                'tax_query'      => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product-category',
                        'field' => 'term_id',
                        'terms' => $category_ids,
                    )
                )
            );

            $relateds = new WP_Query($argss);
        ?>
        <?php if ($relateds->have_posts()) : ?>
        <div class="loop clear" style="margin-top: 50px;">
            <?php
                    while ($relateds->have_posts()) :
                        $relateds->the_post();
                        get_template_part('template-parts/productbox', get_theme_mod('homepage_style', 'shop'));
                    endwhile;
                    ?>
        </div>
        <?php endif; ?>
        <?php endif; ?>
</article>