Plugin Directory

Changeset 2689611


Ignore:
Timestamp:
03/06/2022 03:58:13 PM (4 years ago)
Author:
timotheemoulin
Message:

0.2.0

  • Allow selecting multiple products with different quantities
  • Allow selecting product variations
Location:
wc-cart-automation/trunk
Files:
3 added
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • wc-cart-automation/trunk/css/admin.css

    r2678573 r2689611  
    1 body.post-type-wcca .wcca-field{display:flex;align-items:start;margin:1rem 0}body.post-type-wcca .wcca-field .wcca-label{font-weight:bold;flex-basis:30%}body.post-type-wcca .wcca-field .wcca-input{flex-grow:1}body.post-type-wcca .wcca-field .wcca-input>*{width:100%}body.post-type-wcca .wcca-field .wcca-input-checkbox,body.post-type-wcca .wcca-field .wcca-input-radio{display:block;overflow:auto;padding:.5em 0}body.post-type-wcca .wcca-field .wcca-input-checkbox>label,body.post-type-wcca .wcca-field .wcca-input-radio>label{display:block}body.post-type-wcca .wcca-field .wcca-input-checkbox>label+label,body.post-type-wcca .wcca-field .wcca-input-radio>label+label{margin-top:.5em}body.post-type-wcca .wcca-field .wcca-input-textarea{flex-basis:100%;flex-shrink:0}body.post-type-wcca p.warning{padding:.5em;color:#c00;background-color:#ffe6e6}/*# sourceMappingURL=admin.css.map */
     1body.post-type-wcca .wcca-field {
     2  display: flex;
     3  align-items: start;
     4  margin: 1rem 0;
     5}
     6body.post-type-wcca .wcca-field .wcca-label {
     7  font-weight: bold;
     8  flex-basis: 30%;
     9}
     10body.post-type-wcca .wcca-field .wcca-input {
     11  flex-grow: 1;
     12}
     13body.post-type-wcca .wcca-field .wcca-input > * {
     14  width: 100%;
     15}
     16body.post-type-wcca .wcca-field .wcca-input-checkbox, body.post-type-wcca .wcca-field .wcca-input-radio {
     17  display: block;
     18  overflow: auto;
     19  padding: 0.5em 0;
     20}
     21body.post-type-wcca .wcca-field .wcca-input-checkbox > label, body.post-type-wcca .wcca-field .wcca-input-radio > label {
     22  display: block;
     23}
     24body.post-type-wcca .wcca-field .wcca-input-checkbox > label + label, body.post-type-wcca .wcca-field .wcca-input-radio > label + label {
     25  margin-top: 0.5em;
     26}
     27body.post-type-wcca .wcca-field .wcca-input-textarea {
     28  flex-basis: 100%;
     29  flex-shrink: 0;
     30}
     31body.post-type-wcca p.warning {
     32  padding: 0.5em;
     33  color: #cc0000;
     34  background-color: #ffe6e6;
     35}
     36
     37/*# sourceMappingURL=admin.css.map */
  • wc-cart-automation/trunk/css/admin.css.map

    r2678573 r2689611  
    1 {"version":3,"sourceRoot":"","sources":["admin.scss"],"names":[],"mappings":"AACC,gCACC,aACA,kBACA,cAEA,4CACC,iBACA,eAGD,4CACC,YAEA,8CACC,WAGD,uGACC,cACA,cACA,eAEA,mHACC,cAEA,+HACC,gBAKH,qDACC,gBACA,cAKH,8BACC,aACA,WACA","file":"admin.css"}
     1{"version":3,"sourceRoot":"","sources":["admin.scss"],"names":[],"mappings":"AACC;EACC;EACA;EACA;;AAEA;EACC;EACA;;AAGD;EACC;;AAEA;EACC;;AAGD;EACC;EACA;EACA;;AAEA;EACC;;AAEA;EACC;;AAKH;EACC;EACA;;AAKH;EACC;EACA;EACA","file":"admin.css"}
  • wc-cart-automation/trunk/includes/class-cpt-automation.php

    r2678573 r2689611  
    4949        ] );
    5050
    51         static::add_field( 'products', __( 'Products to add', WCCA_PLUGIN_NAME ), 'select2', [
    52             'post_type' => 'product',
    53             'single'    => false,
     51        static::add_field( 'products', __( 'Products to add', WCCA_PLUGIN_NAME ), 'repeater', [
     52            'sub_fields' => [
     53                [
     54                    'option' => 'quantity',
     55                    'label'  => __( 'Quantity', WCCA_PLUGIN_NAME ),
     56                    'type'   => 'number',
     57                ],
     58                [
     59                    'option' => 'product',
     60                    'label'  => __( 'Product', WCCA_PLUGIN_NAME ),
     61                    'type'   => 'select2',
     62                    'args'   => [
     63                        'post_type' => 'product',
     64                        'single'    => true,
     65                    ],
     66                ],
     67            ],
    5468        ] );
    5569
     
    6478     * @param string $label
    6579     * @param string $type
    66      * @param array  $args
     80     * @param array $args
    6781     */
    6882    private static function add_field( string $option, string $label, string $type = 'text', array $args = [] ): void {
     
    98112            // ensure that the cart is loaded
    99113            WC()->cart->get_cart();
    100             foreach ( $cart[ 'content' ] as $product ) {
     114            foreach ( $cart['content'] as $product ) {
    101115                WC()->cart->add_to_cart( $product );
    102116            }
    103117
    104             foreach ( $cart[ 'coupons' ] as $coupon ) {
     118            foreach ( $cart['coupons'] as $coupon ) {
    105119                if ( $the_coupon = new WC_Coupon( $coupon ) ) {
    106120                    WC()->cart->apply_coupon( $the_coupon->get_code() );
     
    116130     */
    117131    public static function woocommerce_init(): void {
    118         if ( ! ( $wcca = $_REQUEST[ 'wcca' ] ?? null ) ) {
     132        if ( ! ( $wcca = $_REQUEST['wcca'] ?? null ) ) {
    119133            // bail early if there is no wcca code in the URL
    120134            return;
     
    168182            $wcca_ID = $wcca_ID->ID;
    169183        }
    170 
    171         $wcca = get_post_meta( $wcca_ID );
    172184
    173185        // ensure that the cart is loaded
     
    192204        wcca()->add_customer_wcca_opening( $wcca_ID );
    193205
    194         foreach ( $wcca[ 'wcca_products' ] as $product ) {
    195             WC()->cart->add_to_cart( $product );
    196         }
    197 
    198         foreach ( $wcca[ 'wcca_coupons' ] as $coupon ) {
     206        foreach ( get_post_meta( $wcca_ID, 'wcca_products', true ) as $product ) {
     207            if ( is_scalar( $product ) ) {
     208                // store only the product with quantity 1 (early version of the plugin)
     209                WC()->cart->add_to_cart( $product );
     210            } else {
     211                $wc_product = wc_get_product( $product['product'] );
     212                if ( $wc_product->is_type( 'simple' ) ) {
     213                    WC()->cart->add_to_cart( $wc_product->get_id(), $product['quantity'] ?? 1 );
     214                } elseif ( $wc_product->is_type( 'variation' ) ) {
     215                    WC()->cart->add_to_cart( $wc_product->get_parent_id(), $product['quantity'] ?? 1, $wc_product->get_id() );
     216                }
     217            }
     218        }
     219
     220        foreach ( get_post_meta( $wcca_ID, 'wcca_coupons' ) as $coupon ) {
    199221            if ( $the_coupon = new WC_Coupon( $coupon ) ) {
    200222                WC()->cart->apply_coupon( $the_coupon->get_code() );
     
    218240    public static function save_post( int $wcca_ID ): void {
    219241        foreach ( static::$fields as $field => $data ) {
    220             if ( $data[ 'args' ][ 'single' ] ?? true ) {
     242            if ( ! empty( $data['args']['sub_fields'] ?? [] ) ) {
     243                /*$sub_fields = [];
     244                foreach ( $_REQUEST[ 'wcca_' . $field ] ?? [] as $row_index => $row_values ) {
     245                    foreach ( $data['args']['sub_fields'] as $key => $sub_field ) {
     246                        $a = 0;
     247                    }
     248                }*/
     249                // store all the repeater values in the same repeater meta
     250                $sanitized_values = [];
     251                foreach ( $_REQUEST[ 'wcca_' . $field ] ?? [] as $row_id => $row ) {
     252                    if ( $row_id < 0 ) {
     253                        continue;
     254                    }
     255                    foreach ( $row as $key => $value ) {
     256                        if ( is_array( $value ) ) {
     257                            $row[ $key ] = array_map( 'sanitize_text_field', $value );
     258                        } else {
     259                            $row[ $key ] = sanitize_text_field( $value );
     260                        }
     261                    }
     262                    $sanitized_values[] = $row;
     263                }
     264
     265                update_post_meta( $wcca_ID, 'wcca_' . $field, $sanitized_values );
     266            } elseif ( $data['args']['single'] ?? true ) {
    221267                update_post_meta( $wcca_ID, 'wcca_' . $field, sanitize_text_field( $_REQUEST[ 'wcca_' . $field ] ?? null ) );
    222268            } else {
     
    274320    public static function render_meta_box_fields() {
    275321        foreach ( static::$fields as $field ) {
    276             WCCA_Admin::the_custom_field_admin( $field[ 'name' ], $field[ 'label' ], $field[ 'type' ], $field[ 'args' ] );
     322            WCCA_Admin::the_custom_field_admin( $field['name'], $field['label'], $field['type'], $field['args'] );
    277323        }
    278324    }
  • wc-cart-automation/trunk/includes/class-wcca-admin.php

    r2678573 r2689611  
    44
    55use Exception;
     6use WP_Post;
    67use WP_Query;
    78
     
    3031            $count = count( $openings, COUNT_RECURSIVE );
    3132            printf( _x( '<span title="all %s">a : %s</span>', 'all', WCCA_PLUGIN_NAME ), $count, $count );
    32         } else if ( 'orders' === $column ) {
     33        } elseif ( 'orders' === $column ) {
    3334            $orders = get_option( 'wcca_orders_' . $post_id, [] );
    3435            echo count( $orders );
     
    3738
    3839    public static function manage_wcca_posts_columns( array $columns ): array {
    39         $columns[ 'openings' ] = __( 'Openings', WCCA_PLUGIN_NAME );
    40         $columns[ 'orders' ]   = __( 'Orders', WCCA_PLUGIN_NAME );
     40        $columns['openings'] = __( 'Openings', WCCA_PLUGIN_NAME );
     41        $columns['orders']   = __( 'Orders', WCCA_PLUGIN_NAME );
    4142
    4243        return $columns;
    4344    }
    44 
    4545
    4646    /**
     
    7272
    7373        $stati = get_post_stati();
    74         unset( $stati[ 'auto-draft' ], $stati[ 'revision' ], $stati[ 'publish' ] );
     74        unset( $stati['auto-draft'], $stati['revision'], $stati['publish'] );
    7575        $inactive = ( new WP_Query( [ 'post_type' => 'wcca', 'post_status' => $stati ] ) )->post_count;
    7676
     
    8484     * @param string $label
    8585     * @param string $type
    86      * @param array  $args
     86     * @param array $args
    8787     *
    8888     * @throws Exception
    8989     */
    9090    public static function the_custom_field_admin( string $option, string $label, string $type = 'text', array $args = [] ): void {
    91         $required = $args[ 'required' ] ?? false;
     91        $required      = $args['required'] ?? false;
     92        $single        = $args['single'] ?? true;
     93        $default_value = $args['default_value'] ?? $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, $single ) ?: null;
    9294
    9395        switch ( $type ) {
     96            case 'repeater':
     97                // register the repeater JS
     98                wp_enqueue_script( WCCA_PLUGIN_NAME . '-admin', plugin_dir_url( WCCA_PLUGIN_FILE ) . '/js/wcca_admin.js' );
     99
     100                // get the repeated values
     101                $repeater_values = get_post_meta( get_the_ID(), 'wcca_' . $option, true );
     102                $repeater_count  = count( $repeater_values ) ?: 1;
     103                ob_start();
     104                // create every repeater row
     105                for ( $repeater_key = - 1; $repeater_key < $repeater_count; $repeater_key ++ ) {
     106                    printf( '<div class="repeater-row" style="display:%s;">', $repeater_key === - 1 ? 'none' : 'block' );
     107                    foreach ( $args['sub_fields'] as $sub_field ) {
     108                        $sub_field['args']                  = $sub_field['args'] ?? [];
     109                        $sub_field['args']['default_value'] = $repeater_values[ $repeater_key ][ $sub_field['option'] ] ?? null;
     110                        WCCA_Admin::the_custom_field_admin( $option . '[' . $repeater_key . ']' . '[' . $sub_field['option'] . ']', $sub_field['label'], $sub_field['type'], $sub_field['args'] );
     111                    }
     112                    printf( '<a href="#" class="delete_current_row">%s</a>', __( 'Delete', WCCA_PLUGIN_NAME ) );
     113                    echo '<hr>';
     114                    echo '</div>';
     115                }
     116
     117                // insert the "add row" button
     118                printf( '<a href="#" class="add_row">%s</a>', __( 'Add', WCCA_PLUGIN_NAME ) );
     119
     120                $html = ob_get_clean();
     121
     122                break;
    94123            case 'text':
    95124            case 'url':
     
    97126            case 'email':
    98127            case 'number':
    99                 $default_value = $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, true ) ?: null;
    100128                $default_value = esc_attr( $default_value );
    101129
     
    110138                break;
    111139            case 'textarea':
    112                 $default_value = $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, true ) ?: null;
    113140                $default_value = esc_attr( $default_value );
    114141
     
    119146            case 'select':
    120147            case 'select2':
    121                 $single        = $args[ 'single' ] ?? true;
    122                 $default_value = $_REQUEST[ 'wcca_' . $option ] ?? get_post_meta( get_the_ID(), 'wcca_' . $option, $single );
    123                 $default_value = esc_attr( $default_value );
    124 
    125                 if ( $post_type = $args[ 'post_type' ] ?? null ) {
     148                if ( is_array( $default_value ) ) {
     149                    $default_value = array_map( 'esc_attr', $default_value );
     150                } else {
     151                    $default_value = esc_attr( $default_value );
     152                }
     153
     154                $opt_groups = [];
     155
     156                if ( $post_type = $args['post_type'] ?? null ) {
    126157                    $query   = new WP_Query( [
    127158                        'post_type'           => $post_type,
    128                         'limit'               => - 1,
     159                        'posts_per_page'      => - 1,
    129160                        'post_status__not_in' => [ 'trash' ],
     161                        'order'               => 'ASC',
     162                        'orderby'             => 'title',
    130163                    ] );
    131164                    $choices = [];
    132                     foreach ( $query->posts as $product ) {
    133                         $choices[ $product->ID ] = $product->post_title;
     165
     166                    foreach ( $query->posts as $post ) {
     167                        if ( 'product' === $post->post_type ) {
     168                            $wc_product = wc_get_product( $post );
     169                            if ( $wc_product->is_type( 'simple' ) ) {
     170                                $choices[ $wc_product->get_id() ] = $wc_product->get_title();
     171                            } elseif ( $wc_product->is_type( 'variable' ) ) {
     172                                $choices[ $wc_product->get_id() ] = $wc_product->get_name();
     173
     174                                foreach ( $wc_product->get_available_variations() as $variation ) {
     175                                    $variation                          = wc_get_product( $variation['variation_id'] );
     176                                    $opt_groups[ $variation->get_id() ] = $wc_product->get_id();
     177                                    $choices[ $variation->get_id() ]    = $variation->get_name();
     178                                }
     179                            }
     180                        } elseif ( $post instanceof WP_Post ) {
     181                            $choices[ $post->ID ] = $post->post_title;
     182                        }
    134183                    }
    135184                } else {
    136                     $choices = $args[ 'choices' ] ?? [];
     185                    $choices = $args['choices'] ?? [];
    137186                }
    138187
     
    167216                        $single ? '' : '[]',
    168217                        $option,
    169                         ( $args[ 'required' ] ?? false ) ? 'required' : '',
     218                        ( $args['required'] ?? false ) ? 'required' : '',
    170219                        $single ? '' : 'multiple'
    171220                    );
     
    177226                    foreach ( $choices as $key => $value ) {
    178227                        $selected = in_array( $key, (array) $default_value );
    179                         $html     .= sprintf(
    180                             '<option value="%s" %s>%s</option>',
    181                             $key,
    182                             ( $selected ? 'selected' : '' ),
    183                             $value
    184                         );
     228                        if ( in_array( $key, $opt_groups ) ) {
     229                            // open the option group
     230                            $html .= sprintf(
     231                                '<optgroup label="%s">',
     232                                $value,
     233                            );
     234                        } else {
     235                            // close the option group if the current option is not in a group
     236                            if ( ! array_key_exists( $key, $opt_groups ) ) {
     237                                $html .= '</optgroup>';
     238                            }
     239
     240                            $html .= sprintf(
     241                                '<option value="%s" %s>%s</option>',
     242                                $key,
     243                                ( $selected ? 'selected' : '' ),
     244                                $value
     245                            );
     246                        }
    185247                    }
    186248
  • wc-cart-automation/trunk/includes/class-wcca.php

    r2678573 r2689611  
    7272    public function add_customer_wcca_opening( ?int $wcca_ID = null ) {
    7373        if ( empty( $wcca_ID ) ) {
    74             if ( empty( $wcca_ID = ( $_COOKIE[ 'wcca' ] ?? null ) ) ) {
     74            if ( empty( $wcca_ID = ( $_COOKIE['wcca'] ?? null ) ) ) {
    7575                // something wrong happened and the trace of the user has been lost
    7676                return;
     
    7979
    8080        $openings = get_option( 'wcca_openings_' . $wcca_ID, [] );
    81         if ( empty( $current_user = WC()->session->get_customer_unique_id() ) ) {
     81
     82        if ( method_exists( WC()->session, 'get_customer_unique_id' ) ) {
     83            $current_user = WC()->session->get_customer_unique_id();
     84        } else {
     85            $current_user = WC()->session->get_customer_id();
     86        }
     87
     88        if ( empty( $current_user ) ) {
    8289            // we should not get here, but just in case, consider that is some anonymous visitor
    8390            $current_user = 0;
     
    8895
    8996        setcookie( 'wcca', $wcca_ID );
    90         $_COOKIE[ 'wcca' ] = $wcca_ID;
     97        $_COOKIE['wcca'] = $wcca_ID;
    9198
    9299        update_option( 'wcca_openings_' . $wcca_ID, $openings );
     
    97104     *
    98105     * @param WC_Order|null $order
    99      * @param ?int          $wcca_ID
     106     * @param ?int $wcca_ID
    100107     */
    101108    public function add_order_to_wcca( WC_Order $order = null, ?int $wcca_ID = null ) {
    102109        if ( empty( $wcca_ID ) ) {
    103             if ( empty( $wcca_ID = ( $_COOKIE[ 'wcca' ] ?? null ) ) ) {
     110            if ( empty( $wcca_ID = ( $_COOKIE['wcca'] ?? null ) ) ) {
    104111                // something wrong happened and the trace of the user has been lost
    105112                return;
     
    108115
    109116        setcookie( 'wcca', null );
    110         $_COOKIE[ 'wcca' ] = null;
     117        $_COOKIE['wcca'] = null;
    111118
    112119        $orders = get_option( 'wcca_orders_' . $wcca_ID, [] );
  • wc-cart-automation/trunk/wc-cart-automation.php

    r2678573 r2689611  
    88 * Text Domain:     wc-cart-automation
    99 * Domain Path:     /languages
    10  * Version:         0.1.2
     10 * Version:         0.2.0
    1111 *
    1212 * @package         Wc_Promo_Cart
Note: See TracChangeset for help on using the changeset viewer.