1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
<?php
namespace AutomateWoo;
use Exception;
defined( 'ABSPATH' ) || exit;
/** * Variable {{ cart.items }} class. * * @class Variable_Cart_Items */ class Variable_Cart_Items extends Variable_Abstract_Product_Display {
/** * Declare cart table support. * * @var bool */ public $supports_cart_table = true;
/** * Load admin details. * * @return void */ public function load_admin_details() { parent::load_admin_details(); $this->description = __( 'Display a product listing of the items in the cart.', 'automatewoo' ); }
/** * Loop through items in the cart and fetch WC_Product objects. * * @param Cart $cart The customer's Cart object. * @param array $parameters Parameters for this variable. * @param Workflow $workflow The current Workflow. * * @throws Exception If cart doesn't contain any valid products. * * @return mixed */ public function get_value( $cart, $parameters, $workflow ) { $cart_items = $cart->get_items(); $template = isset( $parameters['template'] ) ? $parameters['template'] : false;
$products = array(); $product_ids = array();
if ( is_array( $cart_items ) ) { foreach ( $cart_items as $item ) { $id = ( 0 !== $item->get_variation_id() ) ? $item->get_variation_id() : $item->get_product_id(); if ( $id ) { $product_ids[] = $id; } }
$product_ids = array_unique( $product_ids );
foreach ( $product_ids as $product_id ) { $product = wc_get_product( $product_id );
// Ensure only published products are included. if ( is_a( $product, 'WC_Product' ) && $product->get_status() === 'publish' ) { $products[] = $product; } } }
if ( empty( $products ) ) { // Return an empty string if fallback is set so it can be displayed otherwise throw an exception. if ( isset( $parameters['fallback'] ) ) { return ''; } else { throw new Exception( esc_html__( '{{ cart.items }} returned no products so the workflow was aborted', 'automatewoo' ) ); } }
$args = array_merge( $this->get_default_product_template_args( $workflow, $parameters ), array( 'products' => $products, 'cart_items' => $cart->get_items_raw(), // legacy. 'cart' => $cart, ) );
return $this->get_product_display_html( $template, $args ); } }
|