Skip to content

Instantly share code, notes, and snippets.

@voronkovich
Last active March 28, 2023 23:39
Show Gist options
  • Save voronkovich/5313992db69a7d1216530b7bc754972f to your computer and use it in GitHub Desktop.
Save voronkovich/5313992db69a7d1216530b7bc754972f to your computer and use it in GitHub Desktop.
WooCommerce EDI 1C: Сохранение кода товара в метаданных
<?php
/**
* Plugin Name: WooCommerce EDI 1C: product code
* Description: Сохраняет код товара из 1С в метаданных товара
* Version: 0.0.1
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* Author: PopArtDesign <info@popartdesign.ru>
* Author URI: https://popartdesign.ru/
*/
defined( 'ABSPATH' ) || exit;
add_action( 'add_meta_boxes_product', function () {
add_meta_box(
'woocommerce-edi-1c-product-code',
'Код товара в 1С',
function ( $product ) {
$product_code = get_post_meta( $product->ID, '_1c_product_code', true );
printf( '<p>%s</p>', $product_code ?: 'Не указан' );
},
'product'
);
} );
add_filter( 'woocommerce_order_item_get_formatted_meta_data', function( $formatted_meta, $item ) {
$product = $item->get_product();
$product_code = get_post_meta( $product->ID, '_1c_product_code', true );
$formatted_meta['_1c_product_code'] = (object) [
'key' => '_1c_product_code',
'value' => $product_code,
'display_key' => is_admin() ? 'Код товара в 1С' : 'Код товара',
'display_value' => $product_code ?: 'Не указан',
];
return $formatted_meta;
}, null, 2 );
add_action( 'edi_loaded', function () {
add_filter( 'edi_parse_product_xml_object', function ( $product_data, $xml_data ) {
$product_data['_1c_product_code'] = $xml_data['Код'][0]['#'] ?? '';
return $product_data;
}, null, 2 );
add_action( 'edi_product_before_save', function ( $product, $product_data ) {
$product->update_meta_data( '_1c_product_code', $product_data['_1c_product_code'] ?? '' );
}, null, 2 );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment