single-product-order-overview.php 4.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $name = $item['studiou_fpp_file_name'] ?? '';
  38. $qty = (int) ($item['quantity'] ?? 0);
  39. $variation = wc_get_product($item['variation_id'] ?? 0);
  40. $variant_label = '';
  41. if ($variation) {
  42. $bits = array();
  43. foreach ($variation->get_attributes() as $k => $v) {
  44. if ($v === '') continue;
  45. $tax = str_replace('attribute_', '', $k);
  46. $term = taxonomy_exists($tax) ? get_term_by('slug', $v, $tax) : null;
  47. $bits[] = $term ? $term->name : $v;
  48. }
  49. $variant_label = implode(' / ', $bits);
  50. }
  51. $unit_price = $qty > 0 ? (float) $item['line_subtotal'] / $qty : 0.0;
  52. $line_total = (float) ($item['line_total'] ?? 0.0);
  53. $base_price = $variation ? (float) get_post_meta($variation->get_id(), '_price', true) : 0.0;
  54. $discount_pct = ($base_price > 0 && $unit_price < $base_price)
  55. ? (int) round((1 - ($unit_price / $base_price)) * 100)
  56. : 0;
  57. $remove_url = wp_nonce_url(
  58. add_query_arg('studiou-fpp-remove-line', $key, get_permalink($product_id)),
  59. 'studiou-fpp-remove-line'
  60. );
  61. ?>
  62. <div class="studiou-fpp-overview-line" data-cart-item-key="<?php echo esc_attr($key); ?>">
  63. <?php if ($thumb) : ?>
  64. <img class="studiou-fpp-overview-thumb" src="<?php echo esc_url($thumb); ?>" alt="" />
  65. <?php endif; ?>
  66. <div class="studiou-fpp-overview-body">
  67. <div class="studiou-fpp-overview-name"><?php echo esc_html($name); ?></div>
  68. <div class="studiou-fpp-overview-variant"><?php echo esc_html($variant_label); ?></div>
  69. <div class="studiou-fpp-overview-meta">
  70. <span class="studiou-fpp-overview-qty"><?php echo esc_html(sprintf(__('Qty %d', 'studiou-wc-free-photo-product'), $qty)); ?></span>
  71. <span class="studiou-fpp-overview-price"><?php echo wp_kses_post(wc_price($line_total)); ?></span>
  72. <?php if ($discount_pct > 0) : ?>
  73. <span class="studiou-fpp-overview-badge">&minus;<?php echo esc_html($discount_pct); ?>%</span>
  74. <?php endif; ?>
  75. </div>
  76. </div>
  77. <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>
  78. </div>
  79. <?php endforeach; ?>
  80. <?php endif; ?>
  81. </div>
  82. <div class="studiou-fpp-overview-footer">
  83. <div class="studiou-fpp-overview-subtotal">
  84. <span class="studiou-fpp-overview-subtotal-label"><?php esc_html_e('Subtotal:', 'studiou-wc-free-photo-product'); ?></span>
  85. <span class="studiou-fpp-overview-subtotal-value"><?php echo wp_kses_post(wc_price($subtotal)); ?></span>
  86. </div>
  87. <a class="button studiou-fpp-overview-checkout" href="<?php echo esc_url(wc_get_checkout_url()); ?>">
  88. <?php esc_html_e('Go to checkout', 'studiou-wc-free-photo-product'); ?>
  89. </a>
  90. </div>
  91. </div>