Include custom template file issue in Woocommerce my account page - php

I am trying to add an include a template file located in my active child theme:
childtheme/woocommerce/myaccount/order-a-kit.php
The function use also echo "Hello World" which is displayed successfully, but not the included php template file.
I have tried those:
include($_SERVER['DOCUMENT_ROOT']."twentyseventeen-child/woocommerce/myaccount/order-a-kit.php");
include($get_stylesheet_directory_uri()."twentyseventeen-child/woocommerce/myaccount/order-a-kit.php");
include 'twentyseventeen-child/woocommerce/myaccount/order-a-kit.php';
The content of the order-a-kit.php is super simple, I am just trying to include that file:
<?php
?>
<div>
<p>
Look at me
</p>
</div>
This is my function.php section and everything is doing as it should except the include function towards the bottom:
add_filter( 'woocommerce_account_menu_items', 'add_my_menu_items', 99, 1 );
function add_my_menu_items( $items ) {
$my_items = array(
// endpoint => label
'order-a-kit' => __( 'Order A Kit', 'woocommerce'),
'orders' => __( 'Order History', 'my_plugin' ),
);
$my_items = array_slice( $items, 0, 1, true ) +
$my_items +
array_slice( $items, 1, count( $items ), true );
return $my_items;
}
//adding custom endpoint
function my_custom_endpoints() {
add_rewrite_endpoint( 'order-a-kit', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'my_custom_endpoints' );
function my_custom_query_vars( $vars ) {
$vars[] = 'order-a-kit';
return $vars;
}
add_filter( 'query_vars', 'my_custom_query_vars', 0 );
function my_custom_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'my_custom_flush_rewrite_rules' );
//including custom endpoint
function my_custom_endpoint_content() {
include 'twentyseventeen-child/woocommerce/myaccount/order-a-kit.php';
echo '<p>Hello World!</p>';
}
add_action( 'woocommerce_account_order-a-kit_endpoint', 'my_custom_endpoint_content' );
?>
Any help is greatly appreciated.

As the "woocommerce" folder is inside your theme as the function.php file you just need to use:
include 'woocommerce/myaccount/order-a-kit.php';
See this related answer: WooCommerce: Adding custom template to customer account pages

Try this
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'woocommerce/myaccount/order-a-kit.php';

You can add this code to your theme's function.php:
class My_Custom_My_Account_Endpoint {
/**
* Custom endpoint name.
*
* #var string
*/
public static $endpoint = 'Your Desired Link';
/**
* Plugin actions.
*/
public function __construct() {
// Actions used to insert a new endpoint in the WordPress.
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
// Change the My Accout page title.
add_filter( 'the_title', array( $this, 'endpoint_title' ) );
// Insering your new tab/page into the My Account page.
add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
add_action( 'woocommerce_account_' . self::$endpoint . '_endpoint', array( $this, 'endpoint_content' ) );
}
/**
* Register new endpoint to use inside My Account page.
*
* #see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
public function add_endpoints() {
add_rewrite_endpoint( self::$endpoint, EP_ROOT | EP_PAGES );
}
/**
* Add new query var.
*
* #param array $vars
* #return array
*/
public function add_query_vars( $vars ) {
$vars[] = self::$endpoint;
return $vars;
}
/**
* Set endpoint title.
*
* #param string $title
* #return string
*/
public function endpoint_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars[ self::$endpoint ] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.
$title = __( 'Your Item Name', 'woocommerce' );
remove_filter( 'the_title', array( $this, 'endpoint_title' ) );
}
return $title;
}
/**
* Insert the new endpoint into the My Account menu.
*
* #param array $items
* #return array
*/
public function new_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items[ self::$endpoint ] = __( 'Your Item Name', 'woocommerce' );
// Insert back the logout item.
$items['customer-logout'] = $logout;
return $items;
}
/**
* Endpoint HTML content.
*/
public function endpoint_content() {
//example include('woocommerce/myaccount/Your-File.php');
include('Path-To-Your-File.php');
}
/**
* Plugin install action.
* Flush rewrite rules to make our custom endpoint available.
*/
public static function install() {
flush_rewrite_rules();
}
}
new My_Custom_My_Account_Endpoint();
// Flush rewrite rules on plugin activation.
register_activation_hook( __FILE__, array( 'My_Custom_My_Account_Endpoint', 'install' ) );
You have to simply set "Your Desired Link"x1 and "Your Item Name"x2 and "Path-To-Your-File.php"x1 in this source.
If you don't know where is your theme's function.php:
1.Log in to the WordPress Admin interface
2.In the left sidebar, hover over Appearances, then click Theme Editor
3.In the right sidebar, click functions.php

Related

Extending WooCommerce COD payment gateway in a plugin

I would like to understand the sequence various classes are loaded in Wordpress.
There are many plugins available in Wordpress, then who will be loaded earlier than another.
Consider I would like to develop a plugin that will use some existing class of Woocommerce. Basically my custom plugin will extend some class of Woocommerce (for example : WC_Gateway_COD)
How I can ensure the existing class ‘WC_Gateway_COD’ is already defined & loaded before it execute the below statement ?
if (class_exists('WC_Gateway_COD')) {
class WC_my_custom_class extends WC_Gateway_COD {
…..
….
}
}
To make a custom gateway based on an existing WooCommerce payment method as COD, It's recommended to copy the source code from WC_Gateway_COD Class in a plugin (adapting the code for your needs) like:
defined( 'ABSPATH' ) or exit;
// Make sure WooCommerce is active
if ( ! in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
return;
}
add_filter( 'woocommerce_payment_gateways', 'wc_custom_add_to_gateways' );
function wc_custom_add_to_gateways( $gateways ) {
$gateways[] = 'WC_Gateway_COD2';
return $gateways;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'wc_gateway_custom_plugin_links' );
function wc_gateway_custom_plugin_links( $links ) {
$plugin_links = array(
'' . __( 'Configure', 'payment_cod2' ) . ''
);
return array_merge( $plugin_links, $links );
}
add_action( 'plugins_loaded', 'wc_gateway_cod2_init', 11 );
function wc_gateway_cod2_init() {
class WC_Gateway_COD2 extends WC_Payment_Gateway {
public $domain; // The text domain (optional)
/**
* Constructor for the gateway.
*/
public function __construct() {
$this->domain = 'payment_cod2'; // text domain name (for translations)
// Setup general properties.
$this->setup_properties();
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Get settings.
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
$this->enable_for_methods = $this->get_option( 'enable_for_methods', array() );
$this->enable_for_virtual = $this->get_option( 'enable_for_virtual', 'yes' ) === 'yes';
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
add_filter( 'woocommerce_payment_complete_order_status', array( $this, 'change_payment_complete_order_status' ), 10, 3 );
// Customer Emails.
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
}
/**
* Setup general properties for the gateway.
*/
protected function setup_properties() {
$this->id = 'cod2';
$this->icon = apply_filters( 'woocommerce_cod2_icon', '' );
$this->method_title = __( 'Cash on delivery', 'woocommerce' );
$this->method_description = __( 'Have your customers pay with cash (or by other means) upon delivery.', 'woocommerce' );
$this->has_fields = false;
}
// and so on (the rest of the code)…
}
}
You can unset / remove original COD payment gateway changing the 1st function like:
add_filter( 'woocommerce_payment_gateways', 'wc_custom_add_to_gateways' );
function wc_custom_add_to_gateways( $gateways ) {
$gateways[] = 'WC_Gateway_COD2';
unset($gateways['WC_Gateway_COD']; // Remove COD gateway
return $gateways;
}

Wordpress custom endpoint template doesn’t load

I’m trying to add custom template account-details.php to my new endpoint in my-account area.
I have added the new account-details endpoint first:
add_action( 'init', 'co_add_my_account_endpoint' );
function co_add_my_account_endpoint() {
add_rewrite_endpoint( 'account-details', EP_ROOT | EP_PAGES );
}
and here I’m adding the custom template:
add_filter( 'wc_get_template', 'co_custom_endpoint', 10, 5 );
/**
* Add account details custom template
*
* #param $located
* #param $template_name
* #param $args
* #param $template_path
* #param $default_path
* #since 2.0
* #return string $located
*/
function co_custom_endpoint($located, $template_name, $args, $template_path, $default_path) {
global $wp;
if( 'myaccount/my-account.php' == $template_name ) {
$located = wc_locate_template( 'myaccount/account-details.php', $template_path, JGTB_PATH . 'templates/' );
}
return $located;
}
At the end I flush rewrite rules manually, but my template is still not loading on frontend. Anyone can see what am I doing wrong?
I have found other posts on stack-overflow regarding this, but if I replicate exactly the same it doesn’t work for me either.. any ideas?
Any help is very appreciated!
use below code to your active theme's function.php.
function custom_account_details_page_endpoints() {
add_rewrite_endpoint( 'account-details', EP_ROOT | EP_PAGES );
}
add_action( 'init', 'custom_account_details_page_endpoints' );
function custom_account_details_query_vars( $vars ) {
$vars[] = 'account-details';
return $vars;
}
add_filter( 'query_vars', 'custom_account_details_query_vars', 0 );
function custom_account_details_query_vars_flush_rewrite_rules() {
flush_rewrite_rules();
}
add_action( 'wp_loaded', 'custom_account_details_query_vars_flush_rewrite_rules' );
Be sure to place the account-details.php file in the myaccount folder.
function custom_account_details_endpoint_content() {
include 'woocommerce/myaccount/account-details.php';
}
add_action( 'woocommerce_account_account-details_endpoint', 'custom_account_details_endpoint_content' );
By doing this make sure to update permalinks by going to Dashboard -> settings -> permalinks and clik on save settings

WooCommerce 2.6 Custom Endpoint 404 Not Found Error

It seems with version 2.6 WooCommerce have changed the way endpoints and custom profile tabs are made. More infor here https://woocommerce.wordpress.com/2016/04/21/tabbed-my-account-pages-in-2-6/ and and https://github.com/woothemes/woocommerce/wiki/2.6-Tabbed-My-Account-page
My code bellow is almost similar to the one in these links but clicking on the new profile tab keeps showing the 404 Not Found error. I tried Permalinks refresh and flush_rewrite_rules() but othing seems to work....
if ( !class_exists('My_WC_User_Company') ) {
class My_WC_User_Company {
/**
* Custom endpoint name.
*
* #var string
*/
public static $endpoint = 'my-company';
/**
* Plugin actions.
*/
public function __construct() {
// Actions used to insert a new endpoint in the WordPress.
add_action( 'init', array( $this, 'add_endpoints' ) );
add_filter( 'query_vars', array( $this, 'add_query_vars' ), 0 );
// Change the My Accout page title.
add_filter( 'the_title', array( $this, 'endpoint_title' ) );
// Insering your new tab/page into the My Account page.
add_filter( 'woocommerce_account_menu_items', array( $this, 'new_menu_items' ) );
add_action( 'woocommerce_account_' . self::$endpoint . '_endpoint', array( $this, 'endpoint_content' ) );
}
/**
* Register new endpoint to use inside My Account page.
*
* #see https://developer.wordpress.org/reference/functions/add_rewrite_endpoint/
*/
public function add_endpoints() {
add_rewrite_endpoint( self::$endpoint, EP_ROOT | EP_PAGES );
}
/**
* Add new query var.
*
* #param array $vars
* #return array
*/
public function add_query_vars( $vars ) {
$vars[] = self::$endpoint;
return $vars;
}
/**
* Set endpoint title.
*
* #param string $title
* #return string
*/
public function endpoint_title( $title ) {
global $wp_query;
$is_endpoint = isset( $wp_query->query_vars[ self::$endpoint ] );
if ( $is_endpoint && ! is_admin() && is_main_query() && in_the_loop() && is_account_page() ) {
// New page title.
$title = __( 'My Company', 'domain' );
remove_filter( 'the_title', array( $this, 'endpoint_title' ) );
}
return $title;
}
/**
* Insert the new endpoint into the My Account menu.
*
* #param array $items
* #return array
*/
public function new_menu_items( $items ) {
// Remove the logout menu item.
$logout = $items['customer-logout'];
unset( $items['customer-logout'] );
// Insert your custom endpoint.
$items[ self::$endpoint ] = __( 'My Company', 'domain' );
// Insert back the logout item.
$items['customer-logout'] = $logout;
return $items;
}
/**
* Endpoint HTML content.
*/
public function endpoint_content() {
ob_start();
// here is some content ?>
<?php
$output = ob_get_clean();
echo $output;
}
}
}
add_action('init', '_action_ssd_wp_user_company_init');
if( !( function_exists('_action_ssd_wp_user_company_init')) ){
function _action_ssd_wp_user_company_init(){
if ( get_current_user_id() && get_user_meta( get_current_user_id(), 'user_company', true ) == 'yes' ) {
new My_WC_User_Company();
}
}
}
Does anyone have any ideas what couldbe the issue?
there's something wrong with your hook... wrong timings...
these works... with 0 priority or using woocommerce_init
add_action('init', '_action_ssd_wp_user_company_init', 0 );
or
add_action('woocommerce_init', '_action_ssd_wp_user_company_init');
instead of add_action('init', '_action_ssd_wp_user_company_init');
you need to refresh permalink settings for this to work.

Add custom product type in woocommerce

I am using Woocommerce, I wanted to create a custom product type but I didn't get the way
In Woocommerce there are four product type like
Simple product
variable product
grouped product and
External/Affiliate product
I need another custom product type there. is there any way to create a custom product type?
I'm also trying this answer helped.
Add the following to your admin scripts:
add_filter( 'product_type_selector' , array( $this, 'wpa_120215_add_product_type' ) );
function wpa_120215_add_product_type( $types ){
$types[ 'your_type' ] = __( 'Your Product Type' );
return $types;
}
This will show "Your Product Type" in the product data select box in the admin dashboard.
Next create a file with this to actually create the product:
class WC_Product_Your_Type extends WC_Product{
/**
* __construct function.
*
* #access public
* #param mixed $product
*/
public function __construct( $product ) {
$this->product_type = 'your_type';
parent::__construct( $product );
}
}
And to display your custom template in the front end add this:
define( 'YOUR_TEMPLATE_PATH', untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/' );
add_action('woocommerce_your_type_add_to_cart', array($this, 'add_to_cart'),30);
public function add_to_cart() {
wc_get_template( 'single-product/add-to-cart/your_type.php',$args = array(), $template_path = '', YOUR_TEMPLATE_PATH);
}
for comment answer...
your-plugin/
includes/
admin/
In the admin folder: class-your-type-admin.php
<?php
class Your_Type_Admin {
public function __construct() {
add_filter( 'product_type_selector' , array( $this, 'product_type_selector_one'));
}
public function product_type_selector_one( $types ) {
$types[ 'your_type' ] = __( 'Your Type product' );
return $types;
}}
new Your_Type_Admin();
then include this file on you plugin __construct file

Wordpress admin settings page - extracting array

I am looking at building a plugin with a admin settings page. Looking at the codex, I have the following code:
<?php
class MyNewPlugin
{
/**
* Holds the values to be used in the fields callbacks
*/
private $options;
/**
* Start up
*/
public function __construct()
{
add_action( 'admin_menu', array( $this, 'add_plugin_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/**
* Add options page
*/
public function add_plugin_page()
{
// This page will be under "Settings"
add_options_page(
'Settings Admin',
'My New Plugin',
'manage_options',
'my-new-plugin-admin',
array( $this, 'create_admin_page' )
);
}
/**
* Options page callback
*/
public function create_admin_page()
{
// Set class property
$this->options = get_option( 'my_new_plugin_option' );
?>
<div class="wrap">
<?php screen_icon(); ?>
<h2>My New Plugin Settings</h2>
<form method="post" action="options.php">
<?php
// This prints out all hidden setting fields
settings_fields( 'my_new_plugin_option_group' );
do_settings_sections( 'my-new-plugin-admin' );
submit_button();
?>
</form>
</div>
<?php
}
/**
* Register and add settings
*/
public function page_init()
{
register_setting(
'my_new_plugin_option_group', // Option group
'my_new_plugin_option', // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'setting_section_id', // ID
'Settings', // Title
array( $this, 'print_section_info' ), // Callback
'my-new-plugin-admin' // Page
);
add_settings_field(
'title',
'Title',
array( $this, 'title_callback' ),
'my-new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslineone',
'Address Line One',
array( $this, 'addresslineone_callback' ),
'my-new-plugin-admin',
'setting_section_id'
);
add_settings_field(
'addresslinetwo',
'Address Line Two',
array( $this, 'addresslinetwo_callback' ),
'my-new-plugin-admin',
'setting_section_id'
);
}
/**
* Sanitize each setting field as needed
*
* #param array $input Contains all settings fields as array keys
*/
public function sanitize( $input )
{
if( isset( $input['title'] ) )
$new_input['title'] = sanitize_text_field( $input['title'] );
if( isset( $input['addresslineone'] ) )
$new_input['addresslineone'] = sanitize_text_field( $input['addresslineone'] );
if( isset( $input['addresslinetwo'] ) )
$new_input['addresslinetwo'] = sanitize_text_field( $input['addresslinetwo'] );
return $new_input;
}
/**
* Print the Section text
*/
public function print_section_info()
{
print 'Enter your settings below:';
}
/**
* Get the settings option array and print one of its values
*/
public function title_callback()
{
printf(
'<input type="text" id="title" name="my_new_plugin_option[title]" value="%s" />',
isset( $this->options['title'] ) ? esc_attr( $this->options['title']) : ''
);
}
/**
* Get the settings option array and print one of its values
*/
public function addresslineone_callback()
{
printf(
'<input type="text" id="addresslineone" name="my_new_plugin_option[addresslineone]" value="%s" />',
isset( $this->options['addresslineone'] ) ? esc_attr( $this->options['addresslineone']) : ''
);
}
public function addresslinetwo_callback()
{
printf(
'<input type="text" id="addresslinetwo" name="my_new_plugin_option[addresslinetwo]" value="%s" />',
isset( $this->options['addresslinetwo'] ) ? esc_attr( $this->options['addresslinetwo']) : ''
);
}
}
if( is_admin() )
$my_new_plugin_settings = new MyNewPlugin();
include_once "mynewpluginwidget.php";
A simple plugin to start off with, the user enters the settings on the setting page and it displays in the widget area (user has to add the widget where they want it to be).
The question is, how do I extract the settings and display them? I presume they are in the array $this, but cannot get them to display..

Categories