2017-05-16 19 views
0

我有興趣創建基本上填寫WooCommerce結帳模板部分的簡碼。舉例來說,在我的孩子主題的functions.php:您可以從模板部件製作WooCommerce簡碼嗎?

function shortcode_review_order() { 
    //get the template part from woocommerce/templates/checkout/review-order.php 
    wc_get_template_part('checkout/review-order'); 
} 
add_shortcode('custom_review_order', 'shortcode_review_order'); 

...然後在我的網頁...

<div>[custom_review_order]</div> 

當我嘗試這樣做,沒有出現在我結賬頁面。

這甚至可能嗎?

回答

0

有幾件事錯在你的代碼...

首先,你應該使用init鉤添加簡碼。

add_action('init', 'add_shortcodes'); 
function add_shortcodes(){ 
    add_shortcode('custom_review_order', 'shortcode_review_order'); 
} 

那麼你缺少.php模板的一部分。還需要像下面這樣的數組參數。使用wc_get_template可能會得到更準確的結果。

function shortcode_review_order(){ 
    wc_get_template('checkout/review-order.php', array('checkout' => WC()->checkout())); 
} 

要了解更多關於如何正確使用它的模板,搜索插件上的每一個。你會看到它是如何被使用的。你可以得到一個關於你如何使用它的暗示。

+0

謝謝。這工作。實際上,我花了3個小時的時間閱讀和試驗這個和其他方法,但沒有發現任何例子,甚至在WooCommerce自己的文檔和論壇中都沒有包括add_action('init','add_shortcodes') ;'和其他你添加的東西。或者,也許我只是在搜索代碼示例方面運氣不佳。 :p –

+0

好。我在插件中搜索了'add_shortcodes',並看到它在'init'上。 ;)我猜是幸運的。哈哈 – Reigel

+0

說太快了。在Checkout頁面上,但只有在編輯模式下,當我在文本編輯器中包含這個簡碼並保存時,我得到'致命錯誤:未捕獲錯誤:在/ wp-content/plugins /中調用成員函數get_cart() woocommerce/templates/checkout/review-order.php',然後是堆棧跟蹤。我必須錯過一些其他的「成分」,但我似乎無法跟蹤這個模糊的錯誤信息。 :( –

相關問題