{"id":6472,"date":"2021-11-15T08:33:00","date_gmt":"2021-11-15T08:33:00","guid":{"rendered":"https:\/\/musilda.cz\/?p=6472"},"modified":"2021-11-15T08:33:00","modified_gmt":"2021-11-15T08:33:00","slug":"detekce-existence-woocommerce","status":"publish","type":"post","link":"https:\/\/affinite.io\/cs\/detekce-existence-woocommerce\/","title":{"rendered":"Detekce existence WooCommerce"},"content":{"rendered":"\n
Kdykoliv p\u00ed\u0161ete n\u011bjak\u00fd k\u00f3d, u kter\u00e9ho jeho funk\u010dnost z\u00e1vis\u00ed na k\u00f3du t\u0159et\u00ed strany, mus\u00edte po\u010d\u00edtat s t\u00edm, \u017ee nebude dostupn\u00fd. Proto je nezbytn\u00e9 u k\u00f3du pro WooCommerce \u0159e\u0161it to, zda je v\u016fbec plugin nainstalov\u00e1n a aktivn\u00ed. <\/p>\n\n\n\n
Zase nejde o nic slo\u017eit\u00e9ho, ide\u00e1ln\u00ed pro uk\u00e1zku bude n\u011bjak\u00fd plugin, kter\u00fd si vyrob\u00edme. <\/p>\n\n\n\n
Vzal jsem k\u00f3d ze v\u010derej\u0161\u00edho n\u00e1vodu https:\/\/musilda.cz\/jak-vytvorit-vlastni-typ-produktu\/<\/a> a ud\u011blal z n\u011bj mal\u00fd plugin. <\/p>\n\n\n\n Bohu\u017eel, je pln\u011b z\u00e1visl\u00fd na WooCommerce, tak\u017ee p\u0159i pokusu o aktivaci se stane toto:<\/p>\n\n\n\n To je ta lep\u0161\u00ed varianta, plugin nejde aktivovat. V p\u0159\u00edpad\u011b, \u017ee je ji\u017e aktivn\u00ed a n\u011bkdo vypne WooCommerce, chyba sund\u00e1 cel\u00fd web. A tomu te\u010f zabr\u00e1n\u00edme. <\/p>\n\n\n\n M\u016f\u017eeme pou\u017e\u00edt dv\u011b PHP funkce:<\/p>\n\n\n\n Poka\u017ed\u00e9, kdy\u017e se WooCommerce na\u010dte, tak se pou\u017eije t\u0159\u00edda woocommerce a objekt je p\u0159\u00edstupn\u00fd p\u0159es funkci WC().<\/p>\n\n\n\n Tak\u017ee m\u016f\u017eete ps\u00e1t:<\/p>\n\n\n\n nebo<\/p>\n\n\n\n Tento postup se ale hod\u00ed do \u0161ablon, nebo do funkc\u00ed, kter\u00e9 jsou zaregistrov\u00e1ny v n\u011bjak\u00e9m hooku. Nezaru\u010duj\u00ed v\u00e1m toti\u017e, \u017ee v tu chv\u00edli, kdy se WordPress dostane k va\u0161emu k\u00f3du, je ji\u017e WooCommerce na\u010dten\u00e1, i kdy\u017e je na webu aktivn\u00ed.<\/p>\n\n\n\n Na webech dost \u010dasto najdeten\u00e1sleduj\u00edc\u00ed k\u00f3d:<\/p>\n\n\n\n Co\u017e tak\u00e9 funguje (v\u0161imn\u011bte si vlo\u017een\u00ed souboru plugin.php, kter\u00fd je norm\u00e1ln\u011b dostupn\u00fd jen v adminu), ale op\u011bt, je zde jeden probl\u00e9m. <\/p>\n\n\n\n Plugin sice m\u016f\u017ee b\u00fdt aktivn\u00ed, ale WordPress na\u010d\u00edt\u00e1 plugin podle abecedy a kdy\u017e m\u00e1te n\u011bjakou t\u0159\u00eddu, kter\u00e1 je extended t\u0159\u00eddy ve WooCommerce, nemus\u00ed b\u00fdt tato t\u0159\u00edda je\u0161t\u011b na\u010dten\u00e1. <\/p>\n\n\n\n Tak\u017ee tuto funkci pou\u017eijeme pro zobrazen\u00ed notice v adminu, ale pro plugin pot\u0159ebujeme jin\u00fd postup.<\/p>\n\n\n\n WooComerce m\u00e1 akci, kter\u00e1 se jmenuje woocommerce_loaded a tu pou\u017eijeme pro na\u010dten\u00ed na\u0161eho k\u00f3du. <\/p>\n\n\n\n Nejprve tedy informaci o tom, \u017ee WooCommerce nen\u00ed aktivn\u00ed:<\/p>\n\n\n\n Plugin ji\u017e jde activovat, ale v adminu se n\u00e1m zobraz\u00ed kr\u00e1sn\u00e1 hl\u00e1\u0161ka:<\/p>\n\n\n\n Tak\u017ee tohle m\u00e1me vy\u0159e\u0161en\u00e9, ale co s k\u00f3dem, kter\u00fd bude v pluginu?<\/p>\n\n\n\n Uzav\u0159eme jej do takov\u00e9to funkce:<\/p>\n\n\n\n T\u00edm je zaji\u0161t\u011bno, \u017ee v\u0161e co je pot\u0159eba pro fungov\u00e1n\u00ed pluginu bude na\u010dteno a\u017e kdy\u017e bude na\u010dten k\u00f3d WooCommerce a v\u0161e n\u00e1m bude bezpe\u010dn\u011b fungovat:<\/p>\n\n\n\n<\/a><\/figure>\n\n\n\n
<\/a><\/figure>\n\n\n\n
if ( class_exists( 'woocommerce') ) {\n \/\/v\u00e1\u0161 k\u00f3d\n} <\/code><\/pre>\n\n\n\n
\n\nif ( function_exists( 'WC') ) {\n \/\/v\u00e1\u0161 k\u00f3d\n}\n<\/code><\/pre>\n\n\n\n
include_once(ABSPATH . 'wp-admin\/includes\/plugin.php');\nif (is_plugin_active('woocommerce\/woocommerce.php')) {\n \/\/v\u00e1\u0161 k\u00f3d\n}<\/code><\/pre>\n\n\n\n
include_once( ABSPATH . 'wp-admin\/includes\/plugin.php' );\nif ( !is_plugin_active( 'woocommerce\/woocommerce.php' ) ) {\n\n\t\/**\n\t * Admin notice\n\t *\n\t * @since 1.0\n\t *\/\n\tadd_action('admin_notices', 'musilda_admin_notice_wc_not_active');\n \tfunction musilda_admin_notice_wc_not_active() {\n \t$class = 'notice notice-error';\n \tprintf(\n \t\t'<div class=\"error\" style=\"background:red; color:#fff;\"><p>%s<\/p><\/div>',\n \t\t__( 'You Must Install WooCommerce Plugin before activating EventPress ', 'musilda-eventpress' )\n \t);\n \t}\n\n}<\/code><\/pre>\n\n\n\n
<\/a><\/figure>\n\n\n\n
\/**\n * Load plugin code, when WooCommerce is loaded\n *\n * @since 1.0\n *\/\nadd_action( 'woocommerce_loaded', 'load_eventpress' );\nfunction load_eventpress() {\n \/\/v\u00e1s k\u00f3d\n}<\/code><\/pre>\n\n\n\n
<\/a><\/figure>\n\n\n\n