Skip to content

Commit

Permalink
✨ Handle variations
Browse files Browse the repository at this point in the history
  • Loading branch information
larasmorningtrain committed Feb 1, 2024
1 parent 5bda599 commit 5e357f0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/Woocommerce/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,15 +199,14 @@ private static function createAndBookInvoice($paymentMethod, DraftInvoice $invoi
$invoice->create();
}

private static function getEconomicProduct($item, DraftInvoice $invoice): ?Product
private static function getEconomicProduct(\WC_Order_Item_Product $item, DraftInvoice $invoice): ?Product
{
$product = apply_filters('woocommerce_economic_invoice_get_economic_product_line', null, $item, $invoice);

if (! $product) {
$productEconomicId = get_post_meta($item->get_product_id(), 'economic_product_id', true);
$productEconomicId = get_post_meta($item->get_variation_id() ?? $item->get_product_id(), 'economic_product_id', true);

$product = Product::find($productEconomicId);
ray($product);
}

return $product;
Expand Down
25 changes: 21 additions & 4 deletions src/Woocommerce/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,42 @@ class ProductService
public static function addEconomicProductFieldWithWrapper(): void
{
echo '<div class="options_group pricing show_if_simple hidden">';
self::addEconomicProductField();
woocommerce_wp_text_input([
'id' => 'economic_product_id',
'label' => __('E-conomic produkt id', 'mt-wc-economic'),
'placeholder' => __('E-conomic produkt id', 'mt-wc-economic'),
'desc_tip' => 'true',
'description' => __('Udfyld varenummerer fra e-conomic', 'mt-wc-economic'),
'type' => 'number',
'value' => get_post_meta(get_the_ID(), 'economic_product_id', true),
]);
echo '</div>';
}

public static function addEconomicProductField(): void
public static function addEconomicProductField( $loop, $variation_data, $variation): void
{
woocommerce_wp_text_input([
'id' => 'economic_product_id',
'id' => 'economic_product_id_'."[{$loop}]",
'label' => __('E-conomic produkt id', 'mt-wc-economic'),
'placeholder' => __('E-conomic produkt id', 'mt-wc-economic'),
'desc_tip' => 'true',
'description' => __('Udfyld varenummerer fra e-conomic', 'mt-wc-economic'),
'type' => 'number',
'value' => get_post_meta($variation->ID, 'economic_product_id', true),
]);
}

public static function saveEconomicProductField($post_id): void
{
$economic_product_id = $_POST['economic_product_id'] ?? '';
update_post_meta($post_id, 'economic_product_id', sanitize_text_field($economic_product_id));

update_post_meta($post_id, 'economic_product_id', $economic_product_id);
}

public static function saveVariationEconomicProductField($post_id, $id): void
{
$economic_product_id = $_POST['economic_product_id_'][$id] ?? '';

update_post_meta($post_id, 'economic_product_id', $economic_product_id);
}
}
6 changes: 3 additions & 3 deletions src/WoocommerceEconomic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public static function init(): void
add_filter('woocommerce_payment_gateways', [self::class, 'registerGateway'], 10, 1);
add_action('init', [self::class, 'requireGateway'], 1);
add_action('woocommerce_product_options_general_product_data', [ProductService::class, 'addEconomicProductFieldWithWrapper']);
add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField']);
add_action('woocommerce_save_product_variation', [ProductService::class, 'saveEconomicProductField'], 10, 2);
add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 2);
add_action('woocommerce_variation_options', [ProductService::class, 'addEconomicProductField'],10,3);
add_action('woocommerce_save_product_variation', [ProductService::class, 'saveVariationEconomicProductField'], 10, 2);
add_action('woocommerce_process_product_meta', [ProductService::class, 'saveEconomicProductField'], 10, 1);
add_action(ActionScheduleService::CREATE_INVOICE, [ActionScheduleService::class, 'handleCreateInvoiceJob'], 10, 1);
}

Expand Down

0 comments on commit 5e357f0

Please sign in to comment.