WooCommerce

pre vývojárov

Ján Bočínec

Modul pre WooCommerce

  • Klasický WordPress modul
    • Coding standards
    • I18n
    • Post types, taxonomies, meta, options
    • Transients a WP cache
    • Nepoužívajte "super" triedy/objekty
/**
 * Check if WooCommerce is active
 **/
if ( in_array( 
        'woocommerce/woocommerce.php', 
        apply_filters( 'active_plugins', get_option( 'active_plugins' ) )
     ) ) {
    // Put your plugin code here
}

woocommerce_loaded

WC() //WooCommerce

WC()->session //WC_Session

WC()->query //WC_Query

WC()->countries​ //WC_Countries

WC()->cart //WC_Cart

WC()->customer //WC_Customer

WC()->checkout() //WC_Checkout

WC()->payment_gateways() //WC_Payment_Gateways

WC()->shipping() //WC_Shipping

WC()->mailer() //WC_Emails
$customer_country = WC()->customer->get_country();
global $woocommerce;

$woocommerce->cart->get_cart_subtotal();
global $woocommerce;

$woocommerce->cart->get_cart_subtotal();

Funkcie

wc-admin-functions.php

wc-attribute-functions.php
wc-cart-functions.php

wc-order-functions.php

wc-conditional-functions.php

wc-core-functions.php

wc-coupon-functions.php

wc-formatting-functions.php

wc-meta-box-functions.php

 

wc-notice-functions.php

wc-order-functions.php

wc-page-functions.php

wc-product-functions.php

wc-template-functions.php

wc-term-functions.php

wc-user-functions.php

wc-webhook-functions.php

wc-widget-functions.php

Platobná brána

class WC_Custom_Gateway extends WC_Payment_Gateway {

    public function __construct() {
	$this->id                 = 'custom_gateway';
	$this->icon               = 'url_to_image';
	$this->has_fields         = false;
	$this->method_title       = __( 'Custom Gateway', 'wc-custom-gateway' );
	$this->method_description = __( 'Gateway description.', 'wc-custom-gateway' );
    }

}


function webikon_woocommerce_payment_gateways( $methods ) {
    $methods[] = 'WC_Custom_Gateway'; 
    return $methods;
}

add_filter( 'woocommerce_payment_gateways', 'webikon_woocommerce_payment_gateways' );
function process_payment( $order_id ) {
    $order = new WC_Order( $order_id );

    // Mark order as on-hold
    $order->update_status('on-hold', __( 'Awaiting payment', 'wc-custom-gateway' ));

    // Add order note
    $order->add_order_note( __('Payment on-hold', 'wc-custom-gateway') );

    $payment_complete = new Call_Custom_Payment_API( $order_id, $params );

    if ( $payment_complete ) {
        // Reduce stock levels
        $order->reduce_order_stock();
    
        // Remove cart
        WC()->cart->empty_cart();

        // Change order status to completed
        $order->payment_complete();

        return array(
            'result' => 'success',
            'redirect' => 'redirect_url'
        );
    } else {
        //Add error message on the checkout page
        wc_add_notice( __('Payment error:', 'wc-custom-gateway') . $error_message, 'error' );

	return array(
	    'result'   => 'fail',
	    'redirect' => ''
	);
    }
}

Spôsob dopravy

class WC_Custom_Shipping_Method extends WC_Shipping_Method {

    public function __construct() {
        $this->id                 = 'custom_shipping';
	$this->title              = __( 'Your Shipping Method', 'wc-custom-shipping' );
	$this->method_description = __( 'Shipping description', 'wc-custom-shipping' );
	$this->init();
    }
}


function webikon_woocommerce_shipping_methods( $methods ) {
    $methods[] = 'WC_Custom_Gateway'; 
    return $methods;
}

add_filter( 'woocommerce_shipping_methods', 'webikon_woocommerce_shipping_methods' );
public function calculate_shipping( $package ) {
    $rate = array(
        // ID for the rate
        'id' => $this->id,
        // Label for the rate
        'label' => $this->title,
        // Amount for shipping or an array of costs (for per item shipping)
        'cost' => '10.99',
        // Pass an array of taxes, or pass nothing to have it calculated for you, 
        // or pass 'false' to calculate no tax for this method
        'taxes' => '',
        // Calc tax per_order or per_item. Per item needs an array of costs passed via 'cost'
        'calc_tax' => 'per_item'
      );

    // Register the rate
    $this->add_rate( $rate );
}

Settings API

/**
 * Initialise Gateway Settings Form Fields
 */
 function init_form_fields() {
     $this->form_fields = array(
        'option_name' => array(
             'title' => 'Title for your option shown on the settings page',
             'description' => 'Description for your option shown on the settings page',
             'type' => 'text|password|textarea|checkbox|select|multiselect',
             'default' => 'Default value for the option',
             'class' => 'Class for the input',
             'css' => 'CSS rules added line to the input',
             'label' => 'Label', // checkbox only
             'options' => array(
                  'key' => 'value'
             ) // array of options for select/multiselects only
        )
     );
} // End init_form_fields()

Stránky nastavení

  • account
  • general
  • checkout

  • products
  • tax
  • shipping
  • email
  • api
add_filter( 'woocommerce_get_sections_' . SETTINGS_PAGE,
    function( $sections ) { return $sections; } );




add_filter( 'woocommerce_get_settings_' . SETTINGS_PAGE, 
    function( $settings, $current_section ) { return $settings },
    10, 2 );

Vlastná stránka nastavení

add_filter( 'woocommerce_get_settings_pages',
    function( $settings ) {
        $settings[] = new WC_Settings_Custom; //instance of WC_Settings_Page
        return $settings;
    });

Integrations

  • WooCommerce -> Settings -> Integrations
  • WC_Integration class

Vlastný typ produktu

//Register the simple_custom product type
class WC_Product_Simple_Custom extends WC_Product {

    public function __construct( $product ) {

        $this->product_type = 'simple_custom';
    
        parent::__construct( $product );

    }

}


//Add a simple_custom product tab.
add_filter( 'woocommerce_product_data_tabs', 'custom_product_tabs' );


//Contents of the simple_custom options product tab.
add_action( 'woocommerce_product_data_panels', 'custom_options_tab_content' );


// Save the simple_custom fields.
add_action( 'woocommerce_process_product_meta_simple_custom', 'save_rental_option_field' );
add_action( 'woocommerce_process_product_meta_variable_custom', 'save_rental_option_field' );
/**
 * Show pricing fields for simple_custom product.
 */
function simple_custom_js() {

    if ( 'product' != get_post_type() )
        return;

    ?><script type='text/javascript'>
    	jQuery( document ).ready( function() {
    		jQuery( '.options_group.pricing' ).addClass( 'show_if_simple_custom' ).show();
    	});
    
    </script><?php

}
add_action( 'admin_footer', 'simple_custom_js' );

Používať  wp_enqueue_script a wp_enqueue_style!

add_action( 'after_setup_theme', 'my_theme_woocommerce_support' );


function my_theme_woocommerce_support() {
 
   add_theme_support( 'woocommerce' );
 
}

Téma podporuje WooCoommerce 

WooCommerce podstránky

  • wc_get_cart_url()
  • wc_get_checkout_url()
     
  • wc_get_page_permalink( $page )
  • wc_get_page_id( $page )

 

WooCommerce šablóny

/woocommerce/templates/
/custom-theme/woocommerce/
curl https://example.com/wc-api/v3/orders/645 \
    -u consumer_key:consumer_secret

WooCommerce REST API v3

JSON

{
  "order": {
    "id": 645,
    "order_number": 645,
    "created_at": "2015-01-26T20:00:21Z",
    "updated_at": "2015-01-26T20:00:21Z",
    "completed_at": "2015-01-26T20:00:21Z",
    "status": "processing",
    "currency": "USD",
    "total": "79.87",
    "subtotal": "63.97",
    "total_line_items_quantity": 3,
    "total_tax": "5.90",
    "total_shipping": "10.00",
    "cart_tax": "5.40",
    "shipping_tax": "0.50",
    ...
  }
}

WooCommerce iPhone app

$ wp wc
usage: wp wc coupon <command>
   or: wp wc customer <command>
   or: wp wc order <command>
   or: wp wc product <command>
   or: wp wc report <command>
   or: wp wc tax <command>
   or: wp wc tool <command>

See 'wp help wc <command>' for more information on a specific command.

WooCommerce CLI

wp wc tool clear_transients

Cache a optimilizácia

  • Obsah špecifický pre konkrétneho zákazníka
    • /cart
    • /checkout
    • /my-account
    • /* (bloky s informáciami)
       
  • Vylúčiť  tieto podstránky z cacheovania
  • Pomôže AJAX
    • Alternatíva je "sledovať " správanie:
      Je návštevník e-shopu už zákazníkom? 
  • Angular/React?

Chcete naozaj rýchlosť?

  • WordPress Object Cache
    • Redis/Memcached
    • Aplikuje sa aj na Transients API
  • PHP 7
  • ElasticSearch - ElasticPress WooCommerce
     
  • Databáza spomaľuje
    • SSD bez kompromisov
    • RAM > veľkosť databázy
    • Čím viac CPU tým lepšie :)

Ladenie a testovanie

  • Váš e-shop žije!
  • Nezabúdajte na zálohy.
  • Všetko deaktivujte.
  • Použite nejakú základnú tému (Storefront).
  • Pokazte čo sa dá. 
  • System Status Report
  • JavaScript chyby?
  • WP_DEBUG
  • WC_Logger
    • WC_LOG_DIR
      .../wp-content/uploads/wc-logs/
  • Google

System Status Report

  • Základné informácie o WordPress
  • Nastavenie servera a aktuálny stav databázy
  • Základné nastavenia WooCommerce
  • Verzia API
  • Aktívne moduly
  • Informácie o aktívnej téme
  • Používané šablóny
  • Export (markdown)

Rozšírenia a nástroje

  • Smart Manager
  • Product CSV Import Suite
    • WP All Import
  • Store Toolkit
  • WPML (+multi-currency)
  • toret.cz
  • platobnebrany.sk
     
  • Query Monitor (slow queries log)
  • MySQLTuner

WooCommerce 2.6 (Beta 1)

“Zipping Zebra”

  • Zóny pre dopravu
  • Záložky na podstránky "môj účet"
  • AJAX v košíku 
  • Payment Tokens API - štandardizovanie platieb
  • Nové WooCommerce REST API
    • /wc-api/v3/ => /wp-json/wc/v1/
  • Zmeny v niekoľkých šablonách

@JohnnyPea

WooCommerce pre vývojárov

By Ján Bočínec

WooCommerce pre vývojárov

Náhľad do vývoja WooCommerce rozšírení a možností prispôsobenia momentálne jedného z najrozšírenejších open source e-shopových riešení. WooCommerce ponúka vývojárom veľké množstvo vstavaných funkcií, ktoré je dobré poznať. V prednáške sa tiež povenujeme WooCommerce REST API a ukážeme si zopár zaujímavých nástrojov, ktoré nám môžu pri vývoji a správe e-shopu na WordPress pomôcť.

  • 896