/var/www/html_uk/wp-content/plugins/automatewoo/includes/Actions/Subscriptions/UpdateProduct.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace AutomateWoo\Actions\Subscriptions;

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

/**
 * Action to update a chosen product line item to a subscription with a chosen quantity.
 *
 * @since 5.4.0
 */
class UpdateProduct extends \AutomateWoo\Action_Subscription_Edit_Product_Abstract {


    
/**
     * Variable products should not be updateed as a line item to subscriptions, only variations.
     *
     * @var bool
     */
    
protected $allow_variable_products false;


    
/**
     * Flag to define whether the instance of this action requires a name text input field.
     *
     * @var bool
     */
    
protected $load_name_field true;


    
/**
     * Flag to define whether the instance of this action requires a price input field to
     * be displayed on the action's admin UI.
     *
     * @var bool
     */
    
protected $load_cost_field true;


    
/**
     * Do not require the quantity input field.
     *
     * @var bool
     */
    
protected $require_quantity_field false;

    
/**
     * Explain to store admin what this action does via a unique title and description.
     */
    
public function load_admin_details() {
        
parent::load_admin_details();
        
$this->title       __'Update Product''automatewoo' );
        
$this->description __'Update an existing product line item on a subscription. Only the data set on the action will be updated. This action can be used for bulk editing subscriptions, like changing price or product name. Please note that any coupons applied in the subscription will be reapplied for all line items.''automatewoo' );
    }


    
/**
     * Update a given product as a line item to a given subscription.
     *
     * @param \WC_Product      $product Product to update to the subscription.
     * @param \WC_Subscription $subscription Instance of subscription to update the product to.
     *
     * @return bool True if the subscription was edited, false if no change was made.
     */
    
protected function edit_subscription$product$subscription ) {
        
$updated_items_count 0;

        foreach ( 
$subscription->get_items() as $subscription_item ) {
            
// Since $product can not be a variable product there's no need to check a product variation's parent ID
            
$item_product_id $subscription_item->get_variation_id() ? $subscription_item->get_variation_id() : $subscription_item->get_product_id();

            if ( 
$product->get_id() === $item_product_id ) {
                
$this->apply_changes_to_order_line_item$product$subscription_item );
                ++
$updated_items_count;
            }
        }

        if ( ! 
$updated_items_count ) {
            return 
false;
        }

        
// Now we need to refresh the subscription to make sure it has the up-to-date line item then recalculate its totals so taxes etc. are updated
        
$subscription wcs_get_subscription$subscription->get_id() );
        
$this->recalculate_subscription_totals$subscription );

        return 
true;
    }

    
/**
     * Apply action changes to a specific subscription line item.
     *
     * @param \WC_Product            $product The line item product.
     * @param \WC_Order_Item_Product $item    Subscription line item.
     */
    
protected function apply_changes_to_order_line_item\WC_Product $product\WC_Order_Item_Product $item ) {
        
$update_product_args = array();

        if ( 
$this->get_option'line_item_name' ) ) {
            
$update_product_args['name'] = $this->get_option'line_item_name'true );
        }

        if ( 
$this->get_option'line_item_cost' ) || $this->get_option'quantity' ) ) {
            
$update_product_args['quantity'] = ( $this->get_option'quantity' ) ) ? $this->get_option'quantity' ) : $item->get_quantity();

            
$total wc_get_price_excluding_tax(
                
$product,
                array(
                    
'price' => $this->get_option'line_item_cost'true ),
                    
'qty'   => $update_product_args['quantity'],
                )
            );

            
$update_product_args['subtotal'] = $total;
            
$update_product_args['total']    = $total;
        }

        if ( ! empty( 
$update_product_args ) ) {
            
$item->set_props$update_product_args );
            
$item->save();
        }
    }


    
/**
     * Get the description to display on the quantity field for this action
     */
    
protected function get_quantity_field_description() {
        return 
__'Optionally set a new quantity for the product. Defaults to the current quantity set on the subscription.''automatewoo' );
    }


    
/**
     * Get the description to display on the cost field for this action
     */
    
protected function get_cost_field_description() {
        return 
__'Optionally set a custom price to use for the line item\'s cost. Do not include a currency symbol. Total line item cost will be this amount * quantity. Price should be entered the same as it would be on the Edit Product screen - taxes inclusive or exclusive. Defaults to no-change - the current price set on the product line item will remain.''automatewoo' );
    }


    
/**
     * Get a message to update to the subscription to record the product being updateed by this action.
     *
     * Helpful for tracing the history of this action by viewing the subscription's notes.
     *
     * @param \WC_Product $product Product being updateed to the subscription. Required so its name can be updateed to the order note.
     * @return string
     */
    
protected function get_note$product ) {
        
/* translators: %1$s: workflow title, %2$s: product name, %3$d product ID, %4$d workflow ID */
        
return sprintf__'%1$s workflow run: updated %2$s on subscription. (Product ID: %3$d; Workflow ID: %4$d)''automatewoo' ), $this->workflow->get_title(), $product->get_name(), $product->get_id(), $this->workflow->get_id() );
    }
}