Skip to content

Instantly share code, notes, and snippets.

@PechenkiUA
Last active July 26, 2024 18:18
Show Gist options
  • Select an option

  • Save PechenkiUA/d34ccc8b3f759b40756d2c3a06f64318 to your computer and use it in GitHub Desktop.

Select an option

Save PechenkiUA/d34ccc8b3f759b40756d2c3a06f64318 to your computer and use it in GitHub Desktop.
woocommerce-tm-extra-product-options
<?php
function get_epo_options_for_product_in_order($order_id, $product_id = null) {
$order = wc_get_order($order_id);
if (!$order) return false;
$epo_options = array();
foreach ($order->get_items() as $item_id => $item) {
$item_product_id = $item->get_product_id();
// Перевіряємо, чи співпадає ID товару
if ($item_product_id == $product_id) {
// Отримуємо мета-дані EPO елемента замовлення
$epo_data = themecomplete_get_order_item_meta($item_id, '_tmcartepo_data', true);
if ($epo_data) {
$epo_data = themecomplete_maybe_unserialize($epo_data);
if (is_array($epo_data)) {
foreach ($epo_data as $epo) {
$epo_options[] = array(
'name' => isset($epo['name']) ? $epo['name'] : '',
'value' => isset($epo['value']) ? $epo['value'] : '',
'price' => isset($epo['price']) ? $epo['price'] : 0,
'section' => isset($epo['section']) ? $epo['section'] : '',
'element' => isset($epo['element']) ? $epo['element'] : '',
'quantity' => isset($epo['quantity']) ? $epo['quantity'] : 1
);
}
}
}
// Оскільки ми знайшли потрібний товар, можемо завершити пошук
break;
}
}
return $epo_options;
}
function tmcartepo_fun($list, $order_id)
{
$order = wc_get_order($order_id);
$items = $order->get_items();
$curents = get_woocommerce_currency_symbol();
$product_v3 = '';
foreach ($items as $item) {
$metaProduct = $item->get_formatted_meta_data();
$metaText = '';
/*
foreach ($metaProduct as $key => $value) {
$metaText .= $value->display_key . ' : ' . $value->value . chr(10);
}
*/
$product_item = $item->get_product();
if ($product_item) {
$sku = $product_item->get_sku();
$product_v3 .= $item['name'] . ' x' . $item['quantity'] . ' ' . $item['total'] . $curents . ' ' . $metaText . chr(10);
$selected_options = get_epo_options_for_product_in_order($order_id, $product_item->parent_id);
if (!$selected_options) continue;
foreach ($selected_options as $option) {
if (is_array($option['value'])) {
$value_option = implode(', ', $option['value']);
} else {
$value_option = $option['value'];
}
$product_v3 .= $option['name'] . ' : ' . $value_option.' x' . $option['quantity'] . ' ' . $option['price'] . $curents . chr(10);
}
}
}
$list['{product_opt}'] = $product_v3;
return $list;
}
add_filter('tscf_filter_codetemplate', 'tmcartepo_fun', 20, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment