Last active
March 28, 2023 23:39
-
-
Save voronkovich/5313992db69a7d1216530b7bc754972f to your computer and use it in GitHub Desktop.
WooCommerce EDI 1C: Сохранение кода товара в метаданных
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
* 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