Skip to content

Commit d48b8d8

Browse files
Change article model
1 parent 5786e92 commit d48b8d8

File tree

1 file changed

+23
-31
lines changed

1 file changed

+23
-31
lines changed

plugin/lib/Aplazame/Aplazame/BusinessModel/Article.php

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,44 +14,36 @@ public static function createFromOrderItem( $item ) {
1414
}
1515

1616
public static function createFromOrderItemArray( array $values ) {
17-
$productId = $values['product_id'];
18-
$product = new WC_Product( $values['product_id'] );
19-
17+
$product = new WC_Product( $values['product_id'] );
18+
$quantity = (int) $values['qty'];
19+
$price = $values['line_subtotal'] / $values['qty'];
2020
$tax_rate = 100 * ( $values['line_tax'] / $values['line_total'] );
2121

22-
$aArticle = new self();
23-
$aArticle->id = $productId;
24-
$aArticle->sku = $product->get_sku();
25-
$aArticle->name = $product->get_title();
26-
$aArticle->url = $product->get_permalink();
27-
$aArticle->quantity = (int) $values['qty'];
28-
$aArticle->price = Aplazame_Sdk_Serializer_Decimal::fromFloat( $values['line_subtotal'] / $values['qty'] );
29-
$aArticle->tax_rate = Aplazame_Sdk_Serializer_Decimal::fromFloat( $tax_rate );
30-
31-
$description = WC_Aplazame::_m_or_m( $product, 'get_post', 'get_post_data' )->post_content;
32-
if ( ! empty( $description ) ) {
33-
$aArticle->description = $description;
34-
}
35-
36-
$imageUrl = wp_get_attachment_url( get_post_thumbnail_id( $productId ) );
37-
if ( ! empty( $imageUrl ) ) {
38-
$aArticle->image_url = $imageUrl;
39-
}
40-
41-
return $aArticle;
22+
return self::createArticle( $product, $quantity, $price, $tax_rate );
4223
}
4324

4425
public static function createFromOrderItemProduct( WC_Order_Item_Product $item_product ) {
45-
$product = $item_product->get_product();
26+
$product = new WC_Product( $item_product->get_product_id() );
27+
$quantity = (int) $item_product->get_quantity();
28+
$price = $item_product->get_subtotal() / $item_product->get_quantity();
4629
$tax_rate = 100 * ( $item_product->get_total_tax() / $item_product->get_total() );
4730

48-
$aArticle = new self();
49-
$aArticle->id = $product->get_id();
50-
$aArticle->sku = $product->get_sku();
51-
$aArticle->name = $product->get_title();
52-
$aArticle->url = $product->get_permalink();
53-
$aArticle->quantity = (int) $item_product->get_quantity();
54-
$aArticle->price = Aplazame_Sdk_Serializer_Decimal::fromFloat( $item_product->get_subtotal() / $item_product->get_quantity() );
31+
return self::createArticle( $product, $quantity, $price, $tax_rate );
32+
}
33+
34+
private static function createArticle( WC_Product $product, $quantity, $price, $tax_rate ) {
35+
$aArticle = new self();
36+
$aArticle->id = $product->get_id() ? $product->get_id() : null;
37+
38+
$sku = $product->get_sku();
39+
if ( ! empty( $sku ) ) {
40+
$aArticle->sku = $sku;
41+
}
42+
43+
$aArticle->name = $product->get_title() ? $product->get_title() : null;
44+
$aArticle->url = $product->get_permalink() ? $product->get_permalink() : null;
45+
$aArticle->quantity = $quantity;
46+
$aArticle->price = Aplazame_Sdk_Serializer_Decimal::fromFloat( $price );
5547
$aArticle->tax_rate = Aplazame_Sdk_Serializer_Decimal::fromFloat( $tax_rate );
5648

5749
$description = WC_Aplazame::_m_or_m( $product, 'get_post', 'get_post_data' )->post_content;

0 commit comments

Comments
 (0)