/var/www/html_uk/wp-content/plugins/automatewoo/includes/Variables/Order_Itemscount.php


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
<?php

namespace AutomateWoo;

use 
AutomateWoo\Fields\Select;

if ( ! 
defined'ABSPATH' ) ) {
    exit;
}

/**
 * @class Variable_Order_Itemscount
 */
class Variable_Order_Itemscount extends Variable {

    public const 
INCLUDE_PARENT_BUNDLE_PRODUCT_OPTION                  'include_parent_bundle_product';
    public const 
EXCLUDE_PARENT_BUNDLE_PRODUCT_OPTION                  'exclude_parent_bundle_product';
    public const 
INCLUDE_PARENT_BUNDLE_PRODUCT_EXCLUDE_CHILDREN_OPTION 'include_parent_bundle_product_exclude_children';


    
/**
     * Load admin details
     */
    
public function load_admin_details() {
        
$this->description __'Displays the number of items in the order.''automatewoo' );

        if ( 
class_exists'WC_Product_Bundle' ) ) {
            
$this->add_parameter_field$this->get_bundle_product_parameter_field() );
        }
    }

    
/**
     * Get date format parameter field.
     *
     * @since 6.0.23
     *
     * @return Select
     */
    
protected function get_bundle_product_parameter_field() {
        
$options = [
            
self::INCLUDE_PARENT_BUNDLE_PRODUCT_OPTION => __'Include Parent Bundle Product and the children products''automatewoo' ),
            
self::INCLUDE_PARENT_BUNDLE_PRODUCT_EXCLUDE_CHILDREN_OPTION => __'Include Parent Bundle Product and exclude the children products''automatewoo' ),
            
self::EXCLUDE_PARENT_BUNDLE_PRODUCT_OPTION => __'Exclude Parent Bundle Product but include the children products''automatewoo' ),
        ];

        return ( new 
Selectfalse ) )
            ->
set_name'bundle_product' )
            ->
set_description__'How to handle counting for bundle products.''automatewoo' ) )
            ->
set_requiredfalse )
            ->
set_options$options );
    }


    
/**
     * @param \WC_Order $order The order object
     * @param array     $parameters The variable parameters
     * @return int The number of items in the order
     */
    
public function get_value$order$parameters ) {

        if ( isset( 
$parameters['bundle_product'] ) ) {
            
$count 0;
            foreach ( 
$order->get_items() as $item ) {
                if ( 
$item->get_meta'_bundle_group_mode' ) === 'parent' && $parameters['bundle_product'] === self::EXCLUDE_PARENT_BUNDLE_PRODUCT_OPTION ) {
                    continue;
                }

                if ( 
$item->get_meta'_bundled_by' ) && $parameters['bundle_product'] === self::INCLUDE_PARENT_BUNDLE_PRODUCT_EXCLUDE_CHILDREN_OPTION ) {
                    continue;
                }

                
$count += $item->get_quantity();
            }

            return 
$count;

        } else {
            return 
$order->get_item_count();
        }
    }
}