PHP code example of pickhero / craft-commerce

1. Go to this page and download the library: Download pickhero/craft-commerce library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pickhero / craft-commerce example snippets




return [
    'apiBaseUrl' => getenv('PICKHERO_API_URL'),
    'apiToken' => getenv('PICKHERO_API_TOKEN'),
];

use pickhero\commerce\services\PickHeroApi;
use pickhero\commerce\events\OrderPayloadEvent;
use yii\base\Event;

Event::on(
    PickHeroApi::class,
    PickHeroApi::EVENT_MODIFY_ORDER_PAYLOAD,
    function(OrderPayloadEvent $event) {
        $order = $event->order;
        
        // Add custom remarks from a field
        if ($order->packingInstructions) {
            $event->payload['internal_remarks'] = $order->packingInstructions;
        }
        
        // Add priority flag for express shipping
        if ($order->shippingMethodHandle === 'express') {
            $event->payload['priority'] = true;
        }
        
        // Modify delivery date
        $event->payload['delivery_date'] = $order->dateOrdered
            ->modify('+3 days')
            ->format('Y-m-d');
    }
);

use pickhero\commerce\services\PickHeroApi;
use pickhero\commerce\events\OrderLineItemsEvent;
use yii\base\Event;

Event::on(
    PickHeroApi::class,
    PickHeroApi::EVENT_MODIFY_ORDER_LINE_ITEMS,
    function(OrderLineItemsEvent $event) {
        // Exclude digital products
        $event->lineItems = array_filter($event->lineItems, function($item) {
            $purchasable = $item->getPurchasable();
            return $purchasable && !$purchasable->isDigital;
        });
        
        // Exclude free samples
        $event->lineItems = array_filter($event->lineItems, function($item) {
            return $item->getSku() !== 'FREE-SAMPLE';
        });
    }
);

use pickhero\commerce\services\PickHeroApi;
use pickhero\commerce\events\AddressEvent;
use yii\base\Event;

Event::on(
    PickHeroApi::class,
    PickHeroApi::EVENT_TRANSFORM_ADDRESS,
    function(AddressEvent $event) {
        $address = $event->address;
        
        // Add phone number from custom field
        if ($address->phoneNumber) {
            $event->payload['telephone'] = $address->phoneNumber;
        }
        
        // Add delivery instructions for delivery addresses
        if ($event->type === AddressEvent::TYPE_DELIVERY && $address->deliveryNotes) {
            $event->payload['remarks'] = $address->deliveryNotes;
        }
    }
);



return [
    // Required: API connection
    'apiBaseUrl' => getenv('PICKHERO_API_URL'),
    'apiToken' => getenv('PICKHERO_API_TOKEN'),
    
    // Order synchronization
    'pushOrders' => true,
    'orderStatusToPush' => ['processing'],      // Statuses that trigger order creation in PickHero
    'orderStatusToProcess' => ['paid'],         // Statuses that trigger stock allocation
    
    // Product handling
    'createMissingProducts' => true,            // Auto-create products that don't exist in PickHero
    'pushPrices' => true,                       // Include line item prices in orders
    
    // Webhook synchronization
    'syncOrderStatus' => true,                  // Enable webhook for status updates
    'syncStock' => true,                        // Enable stock synchronization
    
    // Status mapping (PickHero status => Craft order status handle)
    'orderStatusMapping' => [
        'shipped' => 'shipped',
        'delivered' => 'completed',
    ],
    
    // Product field mapping (PickHero field => Craft variant field handle)
    'productFieldMapping' => [
        'gtin' => 'ean',
        'brand' => 'brandName',
        'image_url' => 'productImage',
    ],
    
    // UI customization
    'displayName' => 'Warehouse',               // Custom name in Control Panel navigation
];