Plugin Directory

Changeset 3338927


Ignore:
Timestamp:
08/04/2025 11:45:45 AM (8 months ago)
Author:
pikkoloatli
Message:

Enable detection of the delivery date from the order meta data

Location:
pikkolo/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • pikkolo/trunk/inc/functions.php

    r3031882 r3338927  
    11<?php
    2 /*
    3 ** Helper Functions
    4 */
    5 
     2/**
     3 * Helper Functions
     4 *
     5 * @package PikkoloWooUtils
     6 */
    67
    78/*
     
    1112    exit; }
    1213
    13 /**
    14  * Try to get the delivery date from the billing fields.
    15  *
    16  * @param array $billing_fields The billing fields array.
    17  * @return string
    18  */
    19 function pikkolois_get_delivery_date( array $billing_fields ): string {
    20     $delivery_date = $billing_fields['billing_delivery_date'];
    21     $d_m_y         = DateTime::createFromFormat( 'd#m#Y', $delivery_date );
    22     $y_m_d         = DateTime::createFromFormat( 'Y#m#d', $delivery_date );
    23     if ( $d_m_y ) {
    24         return $d_m_y->format( 'Y-m-d' );
    25     }
    26     if ( $y_m_d ) {
    27         return $y_m_d->format( 'Y-m-d' );
    28     }
    29     return '';
    30 }
    3114
    3215/**
     
    8063
    8164        $wc_product = $product->get_product();
    82        
     65
    8366        $weight     = $wc_product ? $wc_product->get_weight() : 0;
    8467        $dimensions = $wc_product ? $wc_product->get_dimensions() : array();
     
    139122    );
    140123}
     124
     125
     126
     127/**
     128 * Prepares the post fields for sending to the Pikkoló API.
     129 *
     130 * @param Pikkolo_Shipping_Method $pikkolo The Pikkoló shipping method instance.
     131 * @param WC_Order                $order The WooCommerce order object.
     132 * @param array                   $product_data The product data array containing items, refrigerated count, frozen count, and age restriction value.
     133 * @param WC_Logger               $log The WooCommerce logger instance for debugging.
     134 */
     135function pikkolo_prepare_post_fields( $pikkolo, $order, $product_data, $log ) {
     136    // Get cookies set in pikkolo.js.
     137    $station_id = isset( $_COOKIE['pikkolo_station_id'] ) ? sanitize_text_field( wp_unslash( $_COOKIE['pikkolo_station_id'] ) ) : '';
     138
     139    // Get delivery date from order meta data if it exists.
     140    $delivery_date_from_meta = pikkolois_get_delivery_date_from_order_meta_data( $order );
     141
     142    // Get delivery date from checkout page if it exists.
     143    $delivery_date_from_checkout = pikkolois_get_delivery_date( WC()->checkout()->get_posted_data() );
     144
     145    if ( 'yes' === ( $pikkolo->debug ) ) {
     146        $log->add( 'pikkolois', "Delivery date from meta {$delivery_date_from_meta}" );
     147        $log->add( 'pikkolois', "Delivery date from checkout $delivery_date_from_checkout" );
     148    }
     149
     150    $vendor_order_id = strval( $order->get_id() );
     151    $customer_phone  = strval( $order->get_billing_phone() );
     152    $customer_email  = strval( $order->get_billing_email() );
     153    $customer_name   = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
     154
     155    $ret = array(
     156        'vendorOrderId'           => $vendor_order_id,
     157        'stationId'               => $station_id,
     158        'customerPhone'           => $customer_phone,
     159        'customerEmail'           => $customer_email,
     160        'customerName'            => $customer_name,
     161        'isSubscription'          => false,
     162        'pickupAuthenticationAge' => intval( $product_data['age_restriction_value'] ) > 0 ? intval( $product_data['age_restriction_value'] ) : 0,
     163        'nrOfRefrigeratedItems'   => $product_data['refrigerated_count'],
     164        'nrOfFreezerItems'        => $product_data['frozen_count'],
     165        'items'                   => $product_data['items'],
     166    );
     167    if ( $delivery_date_from_meta ) {
     168        $ret['deliveryTimeId'] = $station_id . ':' . $delivery_date_from_meta;
     169    }
     170    if ( $delivery_date_from_checkout ) {
     171        $ret['deliveryTimeId'] = $station_id . ':' . $delivery_date_from_checkout;
     172    }
     173
     174    return $ret;
     175}
  • pikkolo/trunk/pikkolo.php

    r3191538 r3338927  
    44Plugin URI: https://pikkolo.is/
    55Description: Shipping method
    6 Version: 1.0.4
     6Version: 1.0.6
    77Author: Pikkoló ehf.
    88Text Domain: pikkolois
     
    1010*/
    1111if ( ! defined( 'ABSPATH' ) ) {
    12     exit; // Exit if accessed directly
     12    exit; // Exit if accessed directly.
    1313}
    1414
     
    1616    require_once __DIR__ . '/inc/functions.php';
    1717}
     18
     19if ( file_exists( __DIR__ . '/inc/dates.php' ) ) {
     20    require_once __DIR__ . '/inc/dates.php';
     21}
     22
    1823/**
    1924 * Check if WooCommerce is active
     
    681686
    682687    /**
    683      * Books a Pikkólo slot when a order is processed.
     688     * Books a Pikkoló slot when a order is processed.
    684689     *
    685690     * @param int $order_id The ID of the processed order.
     
    700705        $products_data = pikkolois_get_products_data( $order );
    701706
    702         $post_fields = pikkolo_prepare_post_fields( $order, $products_data );
     707        $post_fields = pikkolo_prepare_post_fields( $pikkolo, $order, $products_data, $log );
    703708
    704709        $result = pikkolo_send_order_to_api( $pikkolo, $post_fields, $log );
     
    713718    }
    714719    add_action( 'woocommerce_checkout_order_processed', 'pikkolo_process_order', 10, 1 );
    715 
    716     function pikkolo_prepare_post_fields( $order, $product_data ) {
    717         // Get cookies set in pikkolo.js
    718         $station_id = sanitize_text_field( $_COOKIE['pikkolo_station_id'] );
    719         // $delivery_time_id = sanitize_text_field($_COOKIE['pikkolo_delivery_time_id']);
    720 
    721         // Get delivery date from checkout page if it exists.
    722         $delivery_date_from_checkout = pikkolois_get_delivery_date( WC()->checkout()->get_posted_data() );
    723 
    724         $vendor_order_id = strval( $order->get_id() );
    725         $customer_phone  = strval( $order->get_billing_phone() );
    726         $customer_email  = strval( $order->get_billing_email() );
    727         $customer_name   = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
    728 
    729         $ret = array(
    730             'vendorOrderId'           => $vendor_order_id,
    731             'stationId'               => $station_id,
    732             'customerPhone'           => $customer_phone,
    733             'customerEmail'           => $customer_email,
    734             'customerName'            => $customer_name,
    735             'isSubscription'          => false,
    736             'pickupAuthenticationAge' => intval( $product_data['age_restriction_value'] ) > 0 ? intval( $product_data['age_restriction_value'] ) : 0,
    737             'nrOfRefrigeratedItems'   => $product_data['refrigerated_count'],
    738             'nrOfFreezerItems'        => $product_data['frozen_count'],
    739             'items'                   => $product_data['items'],
    740         );
    741         if ( $delivery_date_from_checkout ) {
    742             $ret['deliveryTimeId'] = $station_id . ':' . $delivery_date_from_checkout;
    743         }
    744         return $ret;
    745     }
    746720
    747721    function pikkolo_send_order_to_api( $pikkolo, $post_fields, $log ) {
  • pikkolo/trunk/readme.txt

    r3191538 r3338927  
    88WC requires at least: 3.8.1
    99WC tested up to: 9.4.1
    10 Stable tag: 1.0.5
     10Stable tag: 1.0.6
    1111
    1212== Installation ==
     
    3434== Changelog ==
    3535
     36= 1.0.6 =
     37* Enable detection of the delivery date from the order meta data
     38
    3639= 1.0.5 =
    3740* Documentation updates
Note: See TracChangeset for help on using the changeset viewer.