Skip to content

Instantly share code, notes, and snippets.

@dennisnissle
Last active January 25, 2021 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennisnissle/eba9beeb21ade9c98539e06f95907649 to your computer and use it in GitHub Desktop.
Save dennisnissle/eba9beeb21ade9c98539e06f95907649 to your computer and use it in GitHub Desktop.
Sync a custom product attribute with an invoice item
<?php
add_filter( 'storeabill_woo_order_item_line_item_sync_props', 'my_child_sync_line_item_props', 10, 3 );
/**
* @param $props
* @param \Vendidero\StoreaBill\WooCommerce\OrderItemProduct $item
* @param $args
*
* @return mixed
*/
function my_child_sync_line_item_props( $props, $item, $args ) {
if ( $order_item = $item->get_order_item() ) {
if ( $product = $order_item->get_product() ) {
$attribute_key = 'marke';
$attribute_label = 'Marke';
if ( $attribute_value = $product->get_attribute( $attribute_key ) ) {
$props['attributes'][] = new \Vendidero\StoreaBill\Document\Attribute( array(
'key' => $attribute_key,
'value' => $attribute_value,
'label' => $attribute_label
) );
} elseif ( $product->get_parent_id() > 0 ) {
/*
* In case the product is a child product - lets check the parent product
* for the attribute data in case it was not found for the child.
*/
if ( $parent = wc_get_product( $product->get_parent_id() ) ) {
if ( $attribute_value = $parent->get_attribute( $attribute_key ) ) {
$props['attributes'][] = new \Vendidero\StoreaBill\Document\Attribute( array(
'key' => $attribute_key,
'value' => $attribute_value,
'label' => $attribute_label
) );
}
}
}
}
}
return $props;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment