single-product-order-overview.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. if (!defined('WPINC')) {
  3. die;
  4. }
  5. /** @var WC_Cart|null $cart */
  6. /** @var int $product_id */
  7. // Collect cart lines that belong to this product and carry an FPP file reference.
  8. $lines = array();
  9. $subtotal = 0.0;
  10. if ($cart && $cart instanceof WC_Cart) {
  11. foreach ($cart->get_cart() as $cart_item_key => $item) {
  12. if (empty($item['studiou_fpp_file_record_id'])) {
  13. continue;
  14. }
  15. $line_product_id = (int) ($item['product_id'] ?? 0);
  16. if ($line_product_id !== (int) $product_id) {
  17. continue;
  18. }
  19. $lines[] = array(
  20. 'key' => $cart_item_key,
  21. 'item' => $item,
  22. );
  23. $subtotal += (float) ($item['line_total'] ?? 0.0) + (float) ($item['line_tax'] ?? 0.0);
  24. }
  25. }
  26. ?>
  27. <div class="studiou-fpp-overview" id="studiou-fpp-overview"
  28. data-product-id="<?php echo esc_attr($product_id); ?>"
  29. <?php echo empty($lines) ? ' style="display:none;"' : ''; ?>>
  30. <h3 class="studiou-fpp-overview-title"><?php esc_html_e('Your order', 'studiou-wc-free-photo-product'); ?></h3>
  31. <div class="studiou-fpp-overview-lines" id="studiou-fpp-overview-lines">
  32. <?php if (!empty($lines)) : ?>
  33. <?php foreach ($lines as $line) :
  34. $item = $line['item'];
  35. $key = $line['key'];
  36. $thumb = $item['studiou_fpp_thumb_url'] ?? '';
  37. $preview = '';
  38. if (!empty($item['studiou_fpp_attachment_id'])) {
  39. $pv = wp_get_attachment_image_src((int) $item['studiou_fpp_attachment_id'], 'woocommerce_single');
  40. if ($pv) { $preview = $pv[0]; }
  41. }
  42. if (!$preview) { $preview = $thumb; }
  43. $name = $item['studiou_fpp_file_name'] ?? '';
  44. $qty = (int) ($item['quantity'] ?? 0);
  45. $variation = wc_get_product($item['variation_id'] ?? 0);
  46. $variant_label = '';
  47. if ($variation) {
  48. $bits = array();
  49. foreach ($variation->get_attributes() as $k => $v) {
  50. if ($v === '') continue;
  51. $tax = str_replace('attribute_', '', $k);
  52. $term = taxonomy_exists($tax) ? get_term_by('slug', $v, $tax) : null;
  53. $bits[] = $term ? $term->name : $v;
  54. }
  55. $variant_label = implode(' / ', $bits);
  56. }
  57. $unit_price = $qty > 0 ? (float) $item['line_subtotal'] / $qty : 0.0;
  58. $line_total = (float) ($item['line_total'] ?? 0.0);
  59. $base_price = $variation ? (float) get_post_meta($variation->get_id(), '_price', true) : 0.0;
  60. $discount_pct = ($base_price > 0 && $unit_price < $base_price)
  61. ? (int) round((1 - ($unit_price / $base_price)) * 100)
  62. : 0;
  63. $remove_url = wp_nonce_url(
  64. add_query_arg('studiou-fpp-remove-line', $key, get_permalink($product_id)),
  65. 'studiou-fpp-remove-line'
  66. );
  67. ?>
  68. <div class="studiou-fpp-overview-line" data-cart-item-key="<?php echo esc_attr($key); ?>">
  69. <?php if ($thumb) : ?>
  70. <img class="studiou-fpp-overview-thumb" src="<?php echo esc_url($thumb); ?>" alt="<?php echo esc_attr($name); ?>" data-preview-url="<?php echo esc_url($preview); ?>" data-file-name="<?php echo esc_attr($name); ?>" />
  71. <?php endif; ?>
  72. <div class="studiou-fpp-overview-body">
  73. <div class="studiou-fpp-overview-name-row">
  74. <div class="studiou-fpp-overview-name"><?php echo esc_html($name); ?></div>
  75. <button type="button" class="studiou-fpp-overview-enlarge"><?php esc_html_e('Enlarge', 'studiou-wc-free-photo-product'); ?></button>
  76. </div>
  77. <div class="studiou-fpp-overview-variant"><?php echo esc_html($variant_label); ?></div>
  78. <div class="studiou-fpp-overview-meta">
  79. <span class="studiou-fpp-overview-qty"><?php echo esc_html(sprintf(__('Qty %d', 'studiou-wc-free-photo-product'), $qty)); ?></span>
  80. <span class="studiou-fpp-overview-price"><?php echo wp_kses_post(wc_price($line_total)); ?></span>
  81. <?php if ($discount_pct > 0) : ?>
  82. <span class="studiou-fpp-overview-badge">&minus;<?php echo esc_html($discount_pct); ?>%</span>
  83. <?php endif; ?>
  84. </div>
  85. </div>
  86. <a class="studiou-fpp-overview-remove" href="<?php echo esc_url($remove_url); ?>" title="<?php esc_attr_e('Remove', 'studiou-wc-free-photo-product'); ?>">&times;</a>
  87. </div>
  88. <?php endforeach; ?>
  89. <?php endif; ?>
  90. </div>
  91. <div class="studiou-fpp-overview-footer">
  92. <div class="studiou-fpp-overview-subtotal">
  93. <span class="studiou-fpp-overview-subtotal-label"><?php esc_html_e('Subtotal:', 'studiou-wc-free-photo-product'); ?></span>
  94. <span class="studiou-fpp-overview-subtotal-value"><?php echo wp_kses_post(wc_price($subtotal)); ?></span>
  95. </div>
  96. <a class="button studiou-fpp-overview-checkout" href="<?php echo esc_url(wc_get_checkout_url()); ?>">
  97. <?php esc_html_e('Go to checkout', 'studiou-wc-free-photo-product'); ?>
  98. </a>
  99. </div>
  100. </div>