/var/www/html_us/wp-content/plugins/checkout-for-woocommerce/includes/Features/CartEditing.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

namespace Objectiv\Plugins\Checkout\Features;

use 
Objectiv\Plugins\Checkout\Admin\Pages\PageAbstract;
use 
Objectiv\Plugins\Checkout\Managers\SettingsManager;

/**
 * @link checkoutwc.com
 * @since 5.0.0
 */
class CartEditing extends FeaturesAbstract {
    protected function 
run_if_cfw_is_enabled() {
        
add_action'cfw_update_checkout_after_customer_save', array( $this'handle_update_checkout' ), 10);
        
add_filter'cfw_cart_item_quantity_control', array( $this'get_cart_edit_item_quantity_control' ), 10);
    }

    public function 
init() {
        
parent::init();

        
add_action'cfw_do_plugin_activation', array( $this'run_on_plugin_activation' ) );
        
add_action'cfw_cart_summary_after_admin_page_controls', array( $this'output_admin_fields' ), 10);
    }

    
/**
     * @param array $post_data
     */
    
public function handle_update_checkout( array $post_data ) {
        if ( ! isset( 
$post_data['cart'] ) || 'true' !== $post_data['cfw_update_cart'] ) {
            return;
        }

        
cfw_update_cart$post_data['cart'] );

        
// Check cart has contents.
        
if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters'woocommerce_checkout_redirect_empty_cart'true ) ) {
            
/**
             * Filters whether to suppress checkout is not available message
             * when editing cart results in empty cart
             *
             * @since 3.14.0
             *
             * @param bool $supress_notice Whether to suppress the message
             */
            
if ( false === apply_filters'cfw_cart_edit_redirect_suppress_notice'false ) ) {
                
wc_add_noticecfw__'Checkout is not available whilst your cart is empty.''woocommerce' ), 'notice' );
            }

            
// Allow shortcodes to be used in empty cart redirect URL field
            // This is necessary so that WPML (etc) can swap in a locale specific URL
            
$cart_editing_redirect_url do_shortcode$this->settings_getter->get_setting'cart_edit_empty_cart_redirect' ) );

            
$redirect = empty( $cart_editing_redirect_url ) ? wc_get_cart_url() : $cart_editing_redirect_url;

            
add_filter(
                
'cfw_update_checkout_redirect',
                function() use ( 
$redirect ) {
                    return 
$redirect;
                }
            );
        }
    }

    
/**
     * @param string $output
     * @param array $cart_item
     * @param \WC_Product $product
     * @param string $cart_item_key
     *
     * @return false|string
     */
    
public function get_cart_edit_item_quantity_controlstring $output, array $cart_item\WC_Product $productstring $cart_item_key ) {
        if ( empty( 
$cart_item_key ) ) {
            return 
'';
        }

        
/**
         * Get the output of the cart quantity control to determine if it's being modified
         *
         * Output filtering is required because some very stupid YITH plugins echo on the filter instead of returning something.
         */
        
$product_quantity woocommerce_quantity_input(
            array(
                
'input_name'   => "cart[{$cart_item_key}][qty]",
                
'input_value'  => $cart_item['quantity'],
                
'max_value'    => $product->get_max_purchase_quantity(),
                
'min_value'    => '0',
                
'product_name' => $product->get_name(),
            ),
            
$product,
            
false
        
);

        
ob_start();

        
$woocommerce_core_cart_quantity apply_filters'woocommerce_cart_item_quantity'$product_quantity$cart_item_key$cart_item ); // PHPCS: XSS ok.

        
$filter_output ob_get_clean();

        if ( ! empty( 
$filter_output ) ) {
            
$woocommerce_core_cart_quantity $filter_output;
        }

        
$max_quantity apply_filters'woocommerce_quantity_input_max'$product->get_max_purchase_quantity() > $product->get_max_purchase_quantity() : PHP_INT_MAX$product );
        
$maxed        $cart_item['quantity'] >= $max_quantity || $product->is_sold_individually();

        
/**
         * Filters cart item minimum quantity
         *
         * @since 2.0.0
         *
         * @param int $min_quantity Cart item minimum quantity
         * @param array $cart_item The cart item
         * @param string $cart_item_key The cart item key
         */
        
$min_quantity apply_filters'cfw_cart_item_quantity_min_value'1$cart_item$cart_item_key );

        
/**
         * Filters cart item quantity step
         *
         * Determines how much to increment or decrement by
         *
         * @since 2.0.0
         *
         * @param int $quantity_step Cart item quantity step amount
         * @param array $cart_item The cart item
         * @param string $cart_item_key The cart item key
         */
        
$quantity_step apply_filters'cfw_cart_item_quantity_step'1$cart_item$cart_item_key );

        
ob_start();
        if ( 
$woocommerce_core_cart_quantity === $product_quantity ) {
            
?>
            <div class="cfw-edit-item-quantity-control-wrap">
                <div class="cfw-quantity-stepper">
                    <input type="hidden" data-min-value="<?php echo $min_quantity?>" data-step="<?php echo $quantity_step?>" data-max-quantity="<?php echo $max_quantity?>" class="cfw-edit-item-quantity-value" name="cart[<?php echo $cart_item_key?>][qty]" value="<?php echo $cart_item['quantity']; ?>" />
                    <div class="cfw-quantity-stepper-btn-minus"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M376 232H8c-4.42 0-8 3.58-8 8v32c0 4.42 3.58 8 8 8h368c4.42 0 8-3.58 8-8v-32c0-4.42-3.58-8-8-8z"/></svg></div>
                    <div class="cfw-quantity-stepper-btn-plus <?php echo $maxed 'maxed' ''?>">
                        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M376 232H216V72c0-4.42-3.58-8-8-8h-32c-4.42 0-8 3.58-8 8v160H8c-4.42 0-8 3.58-8 8v32c0 4.42 3.58 8 8 8h160v160c0 4.42 3.58 8 8 8h32c4.42 0 8-3.58 8-8V280h160c4.42 0 8-3.58 8-8v-32c0-4.42-3.58-8-8-8z"/></svg>
                    </div>
                </div>
                <a href="javascript:" data-quantity="<?php echo esc_attr$cart_item['quantity'] ); ?>" class="cfw-quantity-bulk-edit cfw-xtra-small"><?php cfw_e'Edit''woocommerce' ); ?></a>
            </div>

            <?php
            
return ob_get_clean();
        }
        
?>
        <div class="cfw-edit-item-quantity-control-wrap">
            <input type="hidden" data-min-value="<?php echo $min_quantity?>" data-step="<?php echo $quantity_step?>" data-max-quantity="<?php echo $max_quantity?>" class="cfw-edit-item-quantity-value" name="cart[<?php echo $cart_item_key?>][qty]" value="<?php echo $cart_item['quantity']; ?>" />
            <a href="javascript:" data-quantity="<?php echo esc_attr$cart_item['quantity'] ); ?>" class="cfw-quantity-remove-item cfw-xtra-small"><?php cfw_e'Remove''woocommerce' ); ?></a>
        </div>
        <?php
        
return ob_get_clean();
    }

    
/**
     * @param PageAbstract $cart_summary_admin_page
     */
    
public function output_admin_fieldsPageAbstract $cart_summary_admin_page ) {
        if ( ! 
$this->available ) {
            
$notice $cart_summary_admin_page->get_upgrade_required_notice$this->required_plans_list );
        }

        
$cart_summary_admin_page->output_checkbox_row(
            
'enable_cart_editing',
            
cfw__'Cart Editing''checkout-wc' ),
            
cfw__'Enable cart editing.''checkout-wc' ),
            
cfw__'Enable or disable cart editing feature. Allows customer to remove or adjust quantity of cart items.''checkout-wc' ),
            
$this->available,
            
$notice ?? ''
        
);

        
$cart_summary_admin_page->output_text_input_row(
            
'cart_edit_empty_cart_redirect',
            
cfw__'Cart Editing Empty Cart Redirect''checkout-wc' ),
            
cfw__'URL to redirect to when customer empties cart from checkout page.''checkout-wc' ) . '<br/>' cfw__'If left blank, customer will be redirected to the cart page.''checkout-wc' )
        );
    }

    public function 
run_on_plugin_activation() {
        
SettingsManager::instance()->add_setting'enable_cart_editing''no' );
        
SettingsManager::instance()->add_setting'cart_edit_empty_cart_redirect''' );
    }
}