woocommerce pre vývojárov - 2016.bratislava.wordcamp.org · modul pre woocommerce klasický...

28
WooCommerce WooCommerce pre vývojárov pre vývojárov Ján Bočínec Ján Bočínec

Upload: others

Post on 04-Jul-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

WooCommerceWooCommercepre vývojárovpre vývojárov

Ján BočínecJán Bočínec

Page 2: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Modul pre WooCommerceModul pre WooCommerce

Klasický WordPress modul

I18nPost types, taxonomies, meta, optionsTransients a WP cacheNepoužívajte "super" triedy/objekty

Coding standards

/** * Check if WooCommerce is active **/if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { // Put your plugin code here}

Page 3: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

woocommerce_loadedwoocommerce_loadedWC() //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();

docs.woothemes.com/wc-apidocs

Page 4: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

global $woocommerce;

$woocommerce->cart->get_cart_subtotal();

global $woocommerce;

$woocommerce->cart->get_cart_subtotal();

Page 5: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

FunkcieFunkcie

wc-admin-functions.phpwc-attribute-functions.phpwc-cart-functions.phpwc-order-functions.phpwc-conditional-functions.phpwc-core-functions.phpwc-coupon-functions.phpwc-formatting-functions.phpwc-meta-box-functions.php

wc-notice-functions.phpwc-order-functions.phpwc-page-functions.phpwc-product-functions.phpwc-template-functions.phpwc-term-functions.phpwc-user-functions.phpwc-webhook-functions.phpwc-widget-functions.php

Page 6: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Platobná bránaPlatobná 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' );

Page 7: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

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' => '' ); }}

Page 8: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Spôsob dopravySpô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' );

Page 9: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

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 );}

Page 10: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Settings APISettings 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()

Page 11: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Stránky nastaveníStránky nastavení

accountgeneralcheckoutproducts

taxshippingemailapi

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 );

Page 12: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Vlastná stránka nastaveníVlastná stránka nastavení

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

IntegrationsIntegrations

WooCommerce -> Settings -> IntegrationsWC_Integration class

Page 13: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Vlastný typ produktuVlastný typ produktu//Register the simple_custom product typeclass 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' );

Page 14: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

/** * 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!

Page 15: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

add_action( 'after_setup_theme', 'my_theme_woocommerce_support' );

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

Téma podporujeTéma podporujeWooCoommerce WooCoommerce

Page 16: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

WooCommerce podstránkyWooCommerce podstránky

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

Page 17: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

WooCommerce šablónyWooCommerce šablóny

/woocommerce/templates/

/custom-theme/woocommerce/

Page 18: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

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

WooCommerce REST API v3WooCommerce 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", ... }}

woothemes.github.io/woocommerce-rest-api-docs

Page 19: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

WooCommerce iPhone appWooCommerce iPhone app

Page 20: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

$ wp wcusage: 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 CLIWooCommerce CLI

wp wc tool clear_transients

github.com/woothemes/woocommerce/wiki/WP-CLI-commands

wp-cli.org

Page 21: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Cache a optimilizáciaCache 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 cacheovaniaPomôže AJAX

Alternatíva je "sledovať " správanie:Je návštevník e-shopu už zákazníkom?

Angular/React?

Page 22: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Chcete naozaj rýchlosť?Chcete naozaj rýchlosť?

WordPress Object CacheRedis/MemcachedAplikuje sa aj na Transients API

PHP 7ElasticSearch - Databáza spomaľuje

SSD bez kompromisovRAM > veľkosť databázyČím viac CPU tým lepšie :)

ElasticPress WooCommerce

Page 23: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Ladenie a testovanieLadenie 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á.

Page 24: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

System Status ReportJavaScript chyby?WP_DEBUGWC_Logger

WC_LOG_DIR.../wp-content/uploads/wc-logs/

Google

Page 25: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

System Status ReportSystem Status Report

docs.woothemes.com/document/understanding-the-woocommerce-system-status-report

Základné informácie o WordPressNastavenie servera a aktuálny stav databázyZákladné nastavenia WooCommerceVerzia APIAktívne modulyInformácie o aktívnej témePoužívané šablóny

Export (markdown)

Page 26: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

Rozšírenia a nástrojeRozšírenia a nástroje

Smart ManagerProduct CSV Import Suite

WP All ImportStore ToolkitWPML (+multi-currency)

Query Monitor (slow queries log)MySQLTuner

toret.czplatobnebrany.sk

Page 27: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

WooCommerce 2.6 (Beta 1)WooCommerce 2.6 (Beta 1)“Zipping Zebra”“Zipping Zebra”

Zóny pre dopravuZáložky na podstránky "môj účet"AJAX v košíku Payment Tokens API - štandardizovanie platiebNové WooCommerce REST API

/wc-api/v3/ => /wp-json/wc/v1/Zmeny v niekoľkých šablonách

woocommerce.wordpress.com/2016/04/22/woocommerce-2-6-beta-1-is-here

Page 28: WooCommerce pre vývojárov - 2016.bratislava.wordcamp.org · Modul pre WooCommerce Klasický WordPress modul I18n Post types, taxonomies, meta, options Transients a WP cache Nepoužívajte

@JohnnyPea@JohnnyPea

webikon.skwpguru.sk

wp.sk