Plugin Directory

Changeset 2307562


Ignore:
Timestamp:
05/19/2020 12:13:21 AM (6 years ago)
Author:
rnlab
Message:

Update cart

Location:
mobile-builder
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • mobile-builder/trunk/api/class-mobile-builder-cart.php

    r2303204 r2307562  
    3333    private $version;
    3434
     35    private $namespace;
     36
    3537    /**
    3638     * Initialize the class and set its properties.
     
    4547        $this->plugin_name = $plugin_name;
    4648        $this->version     = $version;
     49        $this->namespace   = $plugin_name . '/v' . intval( $version );
    4750
    4851    }
     
    5457     */
    5558    public function add_api_routes() {
    56         $namespace = $this->plugin_name . '/v' . intval( $this->version );
    57 
    58         register_rest_route( $namespace, 'cart', array(
     59
     60        register_rest_route( $this->namespace, 'cart', array(
    5961            array(
    6062                'methods'  => WP_REST_Server::READABLE,
     
    6365            array(
    6466                'methods'  => WP_REST_Server::CREATABLE,
    65                 'callback' => array( $this, 'add_to_cart' ),
     67                'callback' => array( $this, 'mobile_builder_add_to_cart' ),
    6668            )
    6769        ) );
    6870
    69         register_rest_route( $namespace, 'update-shipping', array(
     71        register_rest_route( $this->namespace, 'update-shipping', array(
    7072            'methods'  => WP_REST_Server::CREATABLE,
    7173            'callback' => array( $this, 'update_shipping' ),
    7274        ) );
    7375
    74         register_rest_route( $namespace, 'cart-total', array(
     76        register_rest_route( $this->namespace, 'update-order-review', array(
     77            'methods'  => WP_REST_Server::CREATABLE,
     78            'callback' => array( $this, 'mobile_builder_update_order_review' ),
     79        ) );
     80
     81        register_rest_route( $this->namespace, 'checkout', array(
     82            'methods'  => WP_REST_Server::CREATABLE,
     83            'callback' => array( $this, 'mobile_builder_checkout' ),
     84        ) );
     85
     86        register_rest_route( $this->namespace, 'cart-total', array(
    7587            'methods'  => WP_REST_Server::READABLE,
    7688            'callback' => array( $this, 'get_total' ),
    7789        ) );
    7890
    79         register_rest_route( $namespace, 'shipping-methods', array(
     91        register_rest_route( $this->namespace, 'shipping-methods', array(
    8092            'methods'  => WP_REST_Server::READABLE,
    8193            'callback' => array( $this, 'shipping_methods' ),
    8294        ) );
    8395
    84         register_rest_route( $namespace, 'set-quantity', array(
     96        register_rest_route( $this->namespace, 'set-quantity', array(
    8597            'methods'  => WP_REST_Server::CREATABLE,
    8698            'callback' => array( $this, 'set_quantity' ),
    8799        ) );
    88100
    89         register_rest_route( $namespace, 'remove-cart-item', array(
     101        register_rest_route( $this->namespace, 'remove-cart-item', array(
    90102            'methods'  => WP_REST_Server::CREATABLE,
    91103            'callback' => array( $this, 'remove_cart_item' ),
    92104        ) );
    93105
    94         register_rest_route( $namespace, 'add-discount', array(
     106        register_rest_route( $this->namespace, 'add-discount', array(
    95107            'methods'  => WP_REST_Server::CREATABLE,
    96108            'callback' => array( $this, 'add_discount' ),
    97109        ) );
    98110
    99         register_rest_route( $namespace, 'remove-coupon', array(
     111        register_rest_route( $this->namespace, 'remove-coupon', array(
    100112            'methods'  => WP_REST_Server::CREATABLE,
    101113            'callback' => array( $this, 'remove_coupon' ),
     
    104116    }
    105117
     118    public function simulate_as_not_rest( $is_rest_api_request ) {
     119
     120        if ( empty( $_SERVER['REQUEST_URI'] ) ) {
     121            return $is_rest_api_request;
     122        }
     123
     124        if ( false === strpos( $_SERVER['REQUEST_URI'], $this->namespace ) ) {
     125            return $is_rest_api_request;
     126        }
     127
     128        return false;
     129    }
     130
     131    public function mobile_builder_woocommerce_persistent_cart_enabled() {
     132        return false;
     133    }
     134
    106135    /**
    107136     * @throws Exception
    108137     * @since    1.0.0
    109138     */
    110     public function rnlab_pre_car_rest_api() {
     139    public function mobile_builder_pre_car_rest_api() {
     140
    111141        if ( defined( 'WC_VERSION' ) && version_compare( WC_VERSION, '3.6.0', '>=' ) && WC()->is_rest_api_request() ) {
    112142            require_once( WC_ABSPATH . 'includes/wc-cart-functions.php' );
     
    114144
    115145            // Disable cookie authentication REST check and only if site is secure.
    116             // Todo: Check only our api
    117             if ( is_ssl() && true ) {
     146            if ( is_ssl() ) {
    118147                remove_filter( 'rest_authentication_errors', 'rest_cookie_check_errors', 100 );
    119148            }
     
    134163             */
    135164            if ( is_null( WC()->customer ) ) {
     165
    136166                $customer_id = strval( get_current_user_id() );
    137167
     
    163193        $items = WC()->cart->get_cart();
    164194
    165 
    166195        foreach ( $items as $cart_item_key => $cart_item ) {
    167             $_product = $cart_item['data'];
     196            $_product  = $cart_item['data'];
     197            $vendor_id = wcfm_get_vendor_id_by_post( $_product->get_id() );
    168198
    169199            $image = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->get_id() ), 'single-post-thumbnail' );
     
    174204                $items[ $cart_item_key ]['name']                 = $_product->get_name();
    175205                $items[ $cart_item_key ]['price']                = WC()->cart->get_product_price( $_product );
     206                $items[ $cart_item_key ]['vendor_id']            = $vendor_id;
     207                $items[ $cart_item_key ]['store']                = $vendor_id ? $store_user = get_user_meta( $vendor_id, 'wcfmmp_profile_settings', true ) : null;
    176208            }
    177209
     
    193225     * @since    1.0.0
    194226     */
    195     public function add_to_cart( $request ) {
     227    public function mobile_builder_add_to_cart( $request ) {
     228
     229        // Login before add to cart
     230        if ( get_current_user_id() == 0 ) {
     231            return new WP_Error(
     232                'mobile_builder_add_to_cart',
     233                'Login to add to cart!',
     234                array(
     235                    'status' => 403,
     236                )
     237            );
     238        }
     239
    196240        try {
    197241            $product_id     = $request->get_param( 'product_id' );
     
    215259                'status' => 403,
    216260            ) );
    217         } catch ( \Throwable $e ) {
    218             //do something when Throwable is thrown
    219             return new WP_Error( 'add_to_cart', $e->getMessage(), array(
    220                 'status' => 403,
    221             ) );
    222261        }
    223262    }
     
    262301    }
    263302
     303    public function mobile_builder_update_order_review( $request ) {
     304//      check_ajax_referer( 'update-order-review', 'security' );
     305
     306        wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
     307
     308        if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_update_order_review_expired', true ) ) {
     309            return new WP_Error( 404, 'Sorry, your session has expired.' );
     310        }
     311
     312//      do_action( 'woocommerce_checkout_update_order_review', $request->get_param( 'post_data') ) ? wp_unslash( $request->get_param( 'post_data') ) : '' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
     313
     314        $chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' );
     315        $posted_shipping_methods = $request->get_param( 'shipping_method' ) ? wc_clean( wp_unslash( $request->get_param( 'shipping_method' ) ) ) : array();
     316
     317        if ( is_array( $posted_shipping_methods ) ) {
     318            foreach ( $posted_shipping_methods as $i => $value ) {
     319                $chosen_shipping_methods[ $i ] = $value;
     320            }
     321        }
     322
     323        WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
     324        WC()->session->set( 'chosen_payment_method', empty( $request->get_param( 'shipping_method' ) ) ? '' : wc_clean( wp_unslash( $request->get_param( 'shipping_method' ) ) ) );
     325        WC()->customer->set_props(
     326            array(
     327                'billing_country'   => $request->get_param( 'country' ) ? wc_clean( wp_unslash( $request->get_param( 'country' ) ) ) : null,
     328                'billing_state'     => $request->get_param( 'state' ) ? wc_clean( wp_unslash( $request->get_param( 'state' ) ) ) : null,
     329                'billing_postcode'  => $request->get_param( 'postcode' ) ? wc_clean( wp_unslash( $request->get_param( 'postcode' ) ) ) : null,
     330                'billing_city'      => $request->get_param( 'city' ) ? wc_clean( wp_unslash( $request->get_param( 'city' ) ) ) : null,
     331                'billing_address_1' => $request->get_param( 'address' ) ? wc_clean( wp_unslash( $request->get_param( 'address' ) ) ) : null,
     332                'billing_address_2' => $request->get_param( 'address_2' ) ? wc_clean( wp_unslash( $request->get_param( 'address_2' ) ) ) : null,
     333                'billing_company'   => $request->get_param( 'company' ) ? wc_clean( wp_unslash( $request->get_param( 'company' ) ) ) : null,
     334            )
     335        );
     336
     337        if ( wc_ship_to_billing_address_only() ) {
     338            WC()->customer->set_props(
     339                array(
     340                    'shipping_country'   => $request->get_param( 'country' ) ? wc_clean( wp_unslash( $request->get_param( 'country' ) ) ) : null,
     341                    'shipping_state'     => $request->get_param( 'state' ) ? wc_clean( wp_unslash( $request->get_param( 'state' ) ) ) : null,
     342                    'shipping_postcode'  => $request->get_param( 'postcode' ) ? wc_clean( wp_unslash( $request->get_param( 'postcode' ) ) ) : null,
     343                    'shipping_city'      => $request->get_param( 'city' ) ? wc_clean( wp_unslash( $request->get_param( 'city' ) ) ) : null,
     344                    'shipping_address_1' => $request->get_param( 'address' ) ? wc_clean( wp_unslash( $request->get_param( 'address' ) ) ) : null,
     345                    'shipping_address_2' => $request->get_param( 'address_2' ) ? wc_clean( wp_unslash( $request->get_param( 'address_2' ) ) ) : null,
     346                    'shipping_company'   => $request->get_param( 'company' ) ? wc_clean( wp_unslash( $request->get_param( 'company' ) ) ) : null,
     347                )
     348            );
     349        } else {
     350            WC()->customer->set_props(
     351                array(
     352                    'shipping_country'   => $request->get_param( 's_country' ) ? wc_clean( wp_unslash( $request->get_param( 's_country' ) ) ) : null,
     353                    'shipping_state'     => $request->get_param( 's_state' ) ? wc_clean( wp_unslash( $request->get_param( 's_state' ) ) ) : null,
     354                    'shipping_postcode'  => $request->get_param( 's_postcode' ) ? wc_clean( wp_unslash( $request->get_param( 's_postcode' ) ) ) : null,
     355                    'shipping_city'      => $request->get_param( 's_city' ) ? wc_clean( wp_unslash( $request->get_param( 's_city' ) ) ) : null,
     356                    'shipping_address_1' => $request->get_param( 's_address' ) ? wc_clean( wp_unslash( $request->get_param( 's_address' ) ) ) : null,
     357                    'shipping_address_2' => $request->get_param( 's_address_2' ) ? wc_clean( wp_unslash( $request->get_param( 's_address_2' ) ) ) : null,
     358                    'shipping_company'   => $request->get_param( 's_company' ) ? wc_clean( wp_unslash( $request->get_param( 's_company' ) ) ) : null,
     359                )
     360            );
     361        }
     362
     363        if ( $request->get_param( 'has_full_address' ) && wc_string_to_bool( wc_clean( wp_unslash( $request->get_param( 'has_full_address' ) ) ) ) ) {
     364            WC()->customer->set_calculated_shipping( true );
     365        } else {
     366            WC()->customer->set_calculated_shipping( false );
     367        }
     368
     369        WC()->customer->save();
     370
     371        // Calculate shipping before totals. This will ensure any shipping methods that affect things like taxes are chosen prior to final totals being calculated. Ref: #22708.
     372        WC()->cart->calculate_shipping();
     373        WC()->cart->calculate_totals();
     374
     375        // Get order review fragment.
     376        ob_start();
     377        woocommerce_order_review();
     378        $woocommerce_order_review = ob_get_clean();
     379
     380        // Get checkout payment fragment.
     381        ob_start();
     382        woocommerce_checkout_payment();
     383        $woocommerce_checkout_payment = ob_get_clean();
     384
     385        // Get messages if reload checkout is not true.
     386        $reload_checkout = isset( WC()->session->reload_checkout ) ? true : false;
     387        if ( ! $reload_checkout ) {
     388            $messages = wc_print_notices( true );
     389        } else {
     390            $messages = '';
     391        }
     392
     393        unset( WC()->session->refresh_totals, WC()->session->reload_checkout );
     394
     395        wp_send_json(
     396            array(
     397                'result'    => empty( $messages ) ? 'success' : 'failure',
     398                'messages'  => $messages,
     399                'reload'    => $reload_checkout,
     400                'nonce'     => wp_create_nonce( 'woocommerce-process_checkout' ),
     401                'fragments' => apply_filters(
     402                    'woocommerce_update_order_review_fragments',
     403                    array(
     404                        '.woocommerce-checkout-review-order-table' => $woocommerce_order_review,
     405                        '.woocommerce-checkout-payment'            => $woocommerce_checkout_payment,
     406                    )
     407                ),
     408            )
     409        );
     410    }
     411
     412    public function mobile_builder_checkout() {
     413
     414        wc_maybe_define_constant( 'WOOCOMMERCE_CHECKOUT', true );
     415        WC()->checkout()->process_checkout();
     416        wp_die( 0 );
     417    }
     418
    264419    /**
    265420     * Get shipping methods.
     
    274429//          array(
    275430//              'shipping_country'   => 'VE',
    276 //              'shipping_state'     => isset( $_POST['state'] ) ? wc_clean( wp_unslash( $_POST['state'] ) ) : null,
    277 //              'shipping_postcode'  => isset( $_POST['postcode'] ) ? wc_clean( wp_unslash( $_POST['postcode'] ) ) : null,
    278 //              'shipping_city'      => isset( $_POST['city'] ) ? wc_clean( wp_unslash( $_POST['city'] ) ) : null,
    279 //              'shipping_address_1' => isset( $_POST['address'] ) ? wc_clean( wp_unslash( $_POST['address'] ) ) : null,
    280 //              'shipping_address_2' => isset( $_POST['address_2'] ) ? wc_clean( wp_unslash( $_POST['address_2'] ) ) : null,
     431//              'shipping_state'     => $request->get_param( 'state') ) ? wc_clean( wp_unslash( $request->get_param( 'state') ) ) : null,
     432//              'shipping_postcode'  => $request->get_param( 'postcode') ) ? wc_clean( wp_unslash( $request->get_param( 'postcode') ) ) : null,
     433//              'shipping_city'      => $request->get_param( 'city') ) ? wc_clean( wp_unslash( $request->get_param( 'city') ) ) : null,
     434//              'shipping_address_1' => $request->get_param( 'address') ) ? wc_clean( wp_unslash( $request->get_param( 'address') ) ) : null,
     435//              'shipping_address_2' => $request->get_param( 'address_2') ) ? wc_clean( wp_unslash( $request->get_param( 'address_2') ) ) : null,
    281436//          )
    282437//      );
  • mobile-builder/trunk/api/class-mobile-builder-vendor.php

    r2304807 r2307562  
    149149            // Mobile Banner URL
    150150            $mobile_banner_url = $store['mobile_banner'] ? wp_get_attachment_url( $store['mobile_banner'] ) : '';
     151
     152            $shipping_methods = WCFMmp_Shipping_Zone::get_shipping_methods( 0, $key );
    151153
    152154            $store_user = wcfmmp_get_store( $key );
     
    165167                    'count'  => $store_user->get_total_review_count(),
    166168                    'avg'    => $store_user->get_avg_review_rating(),
    167                 )
     169                ),
     170                'shipping_methods'    => array_column( $shipping_methods, 'id' ),
    168171            ) );
    169172        }
     
    193196     */
    194197    public function vendor( $request ) {
    195         $id         = $request->get_param( 'id' );
     198
     199        $params = $request->get_params();
     200
     201        $id                = $params['id'];
     202        $wcfmmp_radius_lat = $params['wcfmmp_radius_lat'];
     203        $wcfmmp_radius_lng = $params['wcfmmp_radius_lng'];
     204
    196205        $store      = get_user_meta( $id, 'wcfmmp_profile_settings', true );
    197206        $store_user = wcfmmp_get_store( $id );
     
    209218        $mobile_banner_url = $store['mobile_banner'] ? wp_get_attachment_url( $store['mobile_banner'] ) : '';
    210219
     220        $shipping_methods = WCFMmp_Shipping_Zone::get_shipping_methods( 0, $id );
     221
     222        $distance_matrix = array();
     223
     224        if ( $wcfmmp_radius_lat && $wcfmmp_radius_lng && $store['store_lat'] && $store['store_lng'] ) {
     225            $origin_string       = $store['store_lat'] . ',' . $store['store_lng'];
     226            $destinations_string = "$wcfmmp_radius_lat,$wcfmmp_radius_lng";
     227            $key                 = MBD_GOOGLE_API_KEY;
     228            $distance_matrix     = mobile_builder_distance_matrix( $origin_string, $destinations_string, $key );
     229        }
     230
    211231        return array_merge( $store, array(
     232            'id'                  => $id,
    212233            'gravatar'            => $gravatar_url,
    213234            'list_banner_url'     => $list_banner_url,
     
    217238            'total_review_rating' => $store_user->get_total_review_rating(),
    218239            'total_review_count'  => $store_user->get_total_review_count(),
     240            'shipping_methods'    => array_column( $shipping_methods, 'id' ),
     241            'matrix'              => $distance_matrix[0]->elements,
    219242        ) );
    220243
     
    256279        global $wpdb;
    257280
    258         $lat = $_GET['lat'];
    259         $lng = $_GET['lng'];
     281        $lat      = $_GET['lat'];
     282        $lng      = $_GET['lng'];
    260283        $distance = ! empty( $_GET['radius'] ) ? esc_sql( $_GET['radius'] ) : 50;
    261284
     
    270293
    271294            $args['fields'] .= ", ROUND( {$earth_radius} * acos( cos( radians( {$lat} ) ) * cos( radians( gmw_locations.latitude ) ) * cos( radians( gmw_locations.longitude ) - radians( {$lng} ) ) + sin( radians( {$lat} ) ) * sin( radians( gmw_locations.latitude ) ) ),1 ) AS distance";
    272             $args['join']  .= " INNER JOIN {$wpdb->base_prefix}gmw_locations gmw_locations ON $wpdb->posts.ID = gmw_locations.object_id ";
     295            $args['join']   .= " INNER JOIN {$wpdb->base_prefix}gmw_locations gmw_locations ON $wpdb->posts.ID = gmw_locations.object_id ";
    273296
    274297            // calculate the between point.
  • mobile-builder/trunk/includes/class-mobile-builder.php

    r2304807 r2307562  
    208208        // Cart
    209209        $plugin_cart = new Mobile_Builder_Cart( $this->get_plugin_name(), $this->get_version() );
    210         $this->loader->add_action( 'wp_loaded', $plugin_cart, 'rnlab_pre_car_rest_api', 5 );
     210        $this->loader->add_action( 'wp_loaded', $plugin_cart, 'mobile_builder_pre_car_rest_api', 5 );
    211211        $this->loader->add_action( 'rest_api_init', $plugin_cart, 'add_api_routes', 10 );
     212//      $this->loader->add_filter( 'woocommerce_is_rest_api_request', $plugin_cart, 'simulate_as_not_rest', 10 );
     213        $this->loader->add_filter( 'woocommerce_persistent_cart_enabled', $plugin_cart, 'mobile_builder_woocommerce_persistent_cart_enabled' );
    212214
    213215        // Vendor
  • mobile-builder/trunk/public/class-mobile-builder-public.php

    r2304807 r2307562  
    126126            'methods'  => WP_REST_Server::CREATABLE,
    127127            'callback' => array( $this, 'login' ),
     128        ) );
     129
     130        register_rest_route( $namespace, 'logout', array(
     131            'methods'  => WP_REST_Server::READABLE,
     132            'callback' => array( $this, 'logout' ),
    128133        ) );
    129134
     
    14841489
    14851490    /**
     1491     *
     1492     * Log out user
     1493     *
     1494     * @return array
     1495     * @since 1.0.0
     1496     */
     1497    public function logout() {
     1498        wp_logout();
     1499
     1500        return array( "success" => true );
     1501    }
     1502
     1503    /**
    14861504     * Do login with with otp
    14871505     */
Note: See TracChangeset for help on using the changeset viewer.