Add url & image_url to Item

This commit is contained in:
Narek Zakarian 2023-08-31 17:40:06 +04:00
parent 2571d31ad8
commit f869f98ac1
No known key found for this signature in database
GPG key ID: 07AFD7E7A9C164A7
4 changed files with 96 additions and 4 deletions

View file

@ -66,6 +66,20 @@ class Item {
*/
private $category;
/**
* The product url.
*
* @var string
*/
protected $url;
/**
* The product image url.
*
* @var string
*/
protected $image_url;
/**
* The tax rate.
*
@ -90,6 +104,8 @@ class Item {
* @param Money|null $tax The tax.
* @param string $sku The SKU.
* @param string $category The category.
* @param string $url The product url.
* @param string $image_url The product image url.
* @param float $tax_rate The tax rate.
* @param ?string $cart_item_key The cart key for this item.
*/
@ -101,6 +117,8 @@ class Item {
Money $tax = null,
string $sku = '',
string $category = 'PHYSICAL_GOODS',
string $url = '',
string $image_url = '',
float $tax_rate = 0,
string $cart_item_key = null
) {
@ -111,8 +129,9 @@ class Item {
$this->description = $description;
$this->tax = $tax;
$this->sku = $sku;
$this->category = ( self::DIGITAL_GOODS === $category ) ? self::DIGITAL_GOODS : self::PHYSICAL_GOODS;
$this->category = $category;
$this->url = $url;
$this->image_url = $image_url;
$this->tax_rate = $tax_rate;
$this->cart_item_key = $cart_item_key;
}
@ -180,6 +199,24 @@ class Item {
return $this->category;
}
/**
* Returns the url.
*
* @return string
*/
public function url():string {
return $this->url;
}
/**
* Returns the image url.
*
* @return string
*/
public function image_url():string {
return $this->image_url;
}
/**
* Returns the tax rate.
*
@ -211,6 +248,8 @@ class Item {
'description' => $this->description(),
'sku' => $this->sku(),
'category' => $this->category(),
'url' => $this->url(),
'image_url' => $this->image_url(),
);
if ( $this->tax() ) {

View file

@ -53,6 +53,7 @@ class ItemFactory {
* @var \WC_Product $product
*/
$quantity = (int) $item['quantity'];
$image = wp_get_attachment_image_src( $product->get_image_id(), 'full' );
$price = (float) $item['line_subtotal'] / (float) $item['quantity'];
return new Item(
@ -63,6 +64,8 @@ class ItemFactory {
null,
$product->get_sku(),
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
$product->get_permalink(),
$image[0] ?? '',
0,
$cart_item_key
);
@ -128,6 +131,7 @@ class ItemFactory {
$quantity = (int) $item->get_quantity();
$price_without_tax = (float) $order->get_item_subtotal( $item, false );
$price_without_tax_rounded = round( $price_without_tax, 2 );
$image = wp_get_attachment_image_src( $product->get_image_id(), 'full' );
return new Item(
mb_substr( $item->get_name(), 0, 127 ),
@ -136,7 +140,9 @@ class ItemFactory {
$product instanceof WC_Product ? $this->prepare_description( $product->get_description() ) : '',
null,
$product instanceof WC_Product ? $product->get_sku() : '',
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
$product->get_permalink(),
$image[0] ?? ''
);
}
@ -190,6 +196,8 @@ class ItemFactory {
: null;
$sku = ( isset( $data->sku ) ) ? $data->sku : '';
$category = ( isset( $data->category ) ) ? $data->category : 'PHYSICAL_GOODS';
$url = ( isset( $data->url ) ) ? $data->url : '';
$image_url = ( isset( $data->image_url ) ) ? $data->image_url : '';
return new Item(
$data->name,
@ -198,7 +206,9 @@ class ItemFactory {
$description,
$tax,
$sku,
$category
$category,
$url,
$image_url
);
}