Woocommerce add Account and Basket in menu - php

Having recently worked on a project requiring an icon for the connections page and also the cart,
I haven't found much on this problem.

Here is how to activate it.
First, go to woocommerce.php and look for this line.
/**
* Sample implementation of the WooCommerce Mini Cart.
*
* You can add the WooCommerce Mini Cart to header.php like so ...
*
*<?php
* if ( function_exists( 'godault_metal_woocommerce_header_cart' ) ) {
* godault_metal_woocommerce_header_cart();
* }
*?>
*/
Then you must uncomment it and your cart will be activated.
To display it, just add your corresponding function in your header.php
<div class="panier">
<?php
if ( function_exists( 'godault_metal_woocommerce_header_cart' ) ) {
godault_metal_woocommerce_header_cart();
}
?>
</div><!--#cart -->
Then finally to activate put a link to the user account do the same.
Go to woocommerce.php add this by changing the name of the function.
if (! function_exists('godault_metal_woocommerce_header_account')) {
/**
* Display Header Account.
*
* #return void
*/
function godault_metal_woocommerce_header_account() {
?>
<div class="header-my-account">
<div class="header-login">
<a href="<?php echo esc_url(get_permalink(get_option('woocommerce_myaccount_page_id'))); ?>" title="Account">
<i class="fas fa-user"></i>
</a>
</div>
</div>
<?php
}
}
Add it in the header.php.
<div class="account">
<?php
if ( function_exists('godault_metal_woocommerce_header_account')) {
godault_metal_woocommerce_header_account();
}
?>
</div><!--#account -->

Related

Template page not showing data in WordPress

I created a template page on WordPress Admin and just added a h2 heading which does not appear with associated link for the page but it's working if admin logged in wordpress login. I also made the page public on page setting but still the problem is not remove page data shown only to admin. I have searched many forums but didn't find any solution.
<?php
/*
Template Name: Contact Us
*/
get_header(); ?>
<div class="contact-box1">
<h2 >
<br><br>
YOUR ENQUIRY
</h2>
<?php get_footer(); ?>
its working fine in here its not problem with the templating i guess you have not selected the template name right side of the dashboard and only put one contact us to the page as http://amplifiedantennas.com.au/contact-us doesnt have anything assigned over there
how to create template in WordPress?
<?php
/**
Template Name: Contact Us
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site may use a
* different template.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage Amplified_Antennas
* #since 1.0
* #version 1.0
*/
get_header();
?>
<?php
while ( have_posts() ) : the_post();
?>
<div class="contact-box1">
<h2>Contact ENQUIRY</h2>
</div>
<?php
endwhile; // End of the loop.
?>
<?php
get_footer(); ?>
EDITED (I included the full code now):
It's not enough to include the header and the footer, you also need to include the loop in order to fetch the content which was for that particular page in the text editor (in the backend), where you should add/create your content.
So after creating that template, you need to create a "new page" in the backend, selecting that page template in the right sidebar.
In my example below, you would write your header into the title field in the backend, then it would be displayed as an h2 in the real page. And the whole content (inluding that title h2) is wrapped in the .contact-box1 DIV, as you had it in your original code.
example:
<?php
/*
* Template Name: Contact Us
* #package WordPress
* #subpackage Amplified_Antennas
*/
get_header(); ?>
<div class="contact-box1">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
} // end while
} // end if
?>
</div>
<?php get_footer(); ?>
P.S.: In your posted code, you are not closing this DIV tag: <div class="contact-box1">

Output custom code In a specific WooCommerce product page if out of stock

I'm trying to check if the user is on a specific product page, then if the product is out of stock. If the product is not in stock, I want to display an optional promo image that adds another product to cart.
With the current code I get an error and the page stops rendering when it reaches this snippet.
Right now my code is as follows:
<?php if (! $product->is_in_stock() && is_single('12005') ) { ?>
<div id="oos-promo">
<a href="https://example.com/?add-to-cart=11820&quantity=1">
<img src="https://example.com/wp-content/uploads/2017/07/product.jpg" alt="Promo" class="img-responsive">
</a>
</div>
<?php } ?>
I'm putting this code in the content-single-product.php file nested immediately inside the "entry-summary" element on this template file.
Thoughts?
Figured it out! My mistake was not pulling in the global $product variable prior to the if statement, see final code below:
<?php global $product; if (! $product->is_in_stock() && is_single('12005') ) { ?>
<div id="oos-promo">
<a href="https://example.com/?add-to-cart=11820&quantity=1">
<img src="https://example.com/wp-content/uploads/2017/07/product.jpg" alt="Promo" class="img-responsive">
</a>
</div>
<?php } ?>
Instead of overriding WooCommerce templates you should embed your code in a function hooked in woocommerce_single_product_summary action hook, this way:
add_action( 'woocommerce_single_product_summary', 'out_of_stock_custom_code', 3 );
function out_of_stock_custom_code() {
// Including the WC_Product object
global $product;
if ( ! $product->is_in_stock() && $product->get_id() == 12005 ) {
?>
<div id="oos-promo">
<a href="?add-to-cart=11820&quantity=1">
<img src="https://example.com/wp-content/uploads/2017/07/product.jpg" alt="Promo" class="img-responsive">
</a>
</div>
<?php
}
}
Code goes in any php file of your active child theme (or theme) or also in any plugin php file.
This code Is tested and works.

Data not saved Wordpress Custom Admin Page

I try to create an option page for a Wordpress theme. I manage to create the page, the Metaboxes , but when I send the form, the data are not saved. Before I show you my code , I specify that I use a class to generate the page more easily. Maybe this can guide you to the problem.
In this code I would like to save the "slide" option. This option is registred with register_setting() and visible in the slideshow_metabox() method that displays the form with fields that are named "slide [...]".
add_action('admin_menu', 'add_custom_admin_page');
function add_custom_admin_page(){
add_menu_page('CYBER Auto Pièces', 'CYBER Auto Pièces', 'manage_options', 'cyberautopieces', '' );
add_action( 'admin_init', 'cyberautopieces_register_settings' );
}
function cyberautopieces_register_settings() {
//register our settings
register_setting( 'cyberautopieces-settings', 'slide' );
}
new GeneratePageAdmin ('cyberautopieces','CYBER Auto Pièces','CYBER Auto Pièces', 'manage_options','cyberautopieces');
//Add some metaboxes to the page
add_action('add_meta_boxes','cyberautopieces_admin_metaboxes');
function cyberautopieces_admin_metaboxes(){
add_meta_box('slideshow_box','Slideshow','slideshow_metabox','toplevel_page_cyberautopieces','normal','high');
add_meta_box('save_box','Enregistrer','save_metabox','toplevel_page_cyberautopieces','side','high');
}
//Define the insides of the metabox
function slideshow_metabox(){
var_dump(get_option('slide')); // DISPLAY bool(false) :(
?>
<ul id="slideshow_box">
<li>
<a class="add-image-slider" href="#">
<div class="images half">
Choisir une image
<img src="" alt="">
<input type="hidden" name="slide[img][]">
</div>
</a>
<div class="text half">
<label><strong>Titre : </strong></label>
<input type="text" name="slide[title][]" value="" />
<label><strong>Slogan : </strong></label>
<input type="text" name="slide[slogan][]" value="" />
<strong>Situation : </strong>
<select id="slide_situation" name="slide[text_position][]">
<option value="left" selected>Gauche</option>
<option value="right">Droite</option>
</select>
</div>
<div class="add-delete">
<span class="add">+</span><span class="delete">-</span>
</div>
</li>
</ul>
<?php
}
//Define the insides of the metabox
function save_metabox(){
submit_button();
}
Here are the contents of my class GeneratePageAdmin :
class GeneratePageAdmin
{
var $hook;
var $title;
var $menu;
var $permissions;
var $slug;
var $page;
/**
* Constructor class for the Simple Admin Metabox
*#param $hook - (string) parent page hook
*#param $title - (string) the browser window title of the page
*#param $menu - (string) the page title as it appears in the menuk
*#param $permissions - (string) the capability a user requires to see the page
*#param $slug - (string) a slug identifier for this page
*#param $body_content_cb - (callback) (optional) a callback that prints to the page, above the metaboxes. See the tutorial for more details.
*/
function __construct($hook, $title, $menu, $permissions, $slug, $body_content_cb='__return_true'){
$this->hook = $hook;
$this->title = $title;
$this->menu = $menu;
$this->permissions = $permissions;
$this->slug = $slug;
$this->body_content_cb = $body_content_cb;
/* Add the page */
add_action('admin_menu', array($this,'add_page'));
}
/**
* Adds the custom page.
* Adds callbacks to the load-* and admin_footer-* hooks
*/
function add_page(){
/* Add the page */
$this->page = add_submenu_page($this->hook,$this->title, $this->menu, $this->permissions,$this->slug, array($this,'render_page'),10);
//var_dump($this->page);
/* Add callbacks for this screen only */
add_action('load-'.$this->page, array($this,'page_actions'),9);
add_action('admin_footer-'.$this->page,array($this,'footer_scripts'));
}
/**
* Prints the jQuery script to initiliase the metaboxes
* Called on admin_footer-*
*/
function footer_scripts(){
?>
<script> postboxes.add_postbox_toggles(pagenow);</script>
<?php
}
/*
* Actions to be taken prior to page loading. This is after headers have been set.
* call on load-$hook
* This calls the add_meta_boxes hooks, adds screen options and enqueues the postbox.js script.
*/
function page_actions(){
do_action('add_meta_boxes_'.$this->page, null);
do_action('add_meta_boxes', $this->page, null);
/* User can choose between 1 or 2 columns (default 2) */
add_screen_option('layout_columns', array('max' => 2, 'default' => 2) );
/* Enqueue WordPress' script for handling the metaboxes */
wp_enqueue_script('postbox');
}
/**
* Renders the page
*/
function render_page(){
?>
<div class="wrap" id="<?php echo $this->slug; ?>">
<?php screen_icon(); ?>
<h2> <?php echo esc_html($this->title);?> </h2>
<form method="post">
<?php settings_fields( $this->slug.'-settings' ); ?>
<?php do_settings_sections( $this->slug.'-settings' ); ?>
<input type="hidden" name="action" value="some-action">
<?php wp_nonce_field( 'some-action-nonce' );
/* Used to save closed metaboxes and their order */
wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); ?>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-<?php echo 1 == get_current_screen()->get_columns() ? '1' : '2'; ?>">
<div id="post-body-content">
<?php call_user_func($this->body_content_cb); ?>
</div>
<div id="postbox-container-1" class="postbox-container">
<?php do_meta_boxes('','side',null); ?>
</div>
<div id="postbox-container-2" class="postbox-container">
<?php do_meta_boxes('','normal',null); ?>
<?php do_meta_boxes('','advanced',null); ?>
</div>
</div> <!-- #post-body -->
</div> <!-- #poststuff -->
</form>
</div><!-- .wrap -->
<?php
}
}
Sorry for the long code , but I really do not know where the problem is.
Thanks you for your help ;-)
I'd say the problem lies within the way you're instantiating your class.
Use a hook to instantiate it like:
add_action( 'after_setup_theme', array( 'GeneratePageAdmin', 'init' ) );
Create the new method init of course, and do your work there.
If you're creating an options page, then the hook is likely an admin one, and not after_setup_theme, but you should get the idea.
What I would do is something like this:
public static function get_instance() {
// create an object
NULL === self::$instance and self::$instance = new self;
// return the object
return self::$instance;
}
Then, to instantiate it via Wordpress hook:
add_action( 'after_setup_theme', array( GeneratePageAdmin::get_instance(), 'init' ) );
Then, you should be able to use $this in your class
Finally, I've just had to add "action=options.php" in the header of the form and remove this section automatically handled by the settings_fields function.
<input type="hidden" name="action" value="some-action">
<?php wp_nonce_field( 'some-action-nonce' ); ?>

How to generate my menu in primary location in wordpress php site

to avoid installing my menu in each pages (sidebar.php, index.php, category.php, etc.), i create a template part (a php.file) for my menu, for example :
<?php
/**
* The template for displaying menu in Twenty Eleven Child Theme
*
* #package WordPress
* #subpackage My Twenty_Eleven Child Theme
* #since My Twenty Eleven Child Theme 1.0
*/
?>
<div id="menu">
<ul>
<li>
home
</li>
<li>
about
</li>
<li>
contact
</li>
</ul>
</div>
Then i install the <?php get_menu(); ?> after the <div id="primary"> in each page where i want to show the menu...
I guess i must also make some codes in the function.php (register the menu), how should i do ???
What else should i do still ?
Please note that i do not know much about php !
Thanks for your help in advance !
Thought I would push my answer out of the comments:
function get_menu(){
$menu = "<div class='menu'><ul><li><a href='/link'>Link</a></li></ul></div>";
return $menu;
}
Then on the page:
<?php $get_menu = get_menu(); echo $get_menu; ?>
Unless you wanted to register your get_menu as a global variable in that case you would just need to <?php echo get_menu(); ?>

WooCommerce: How do you add text before the price and before sale price?

I have managed to successfully get text to show up before the price and before the sale price, but the text is being considered part of the price not separate from it.
Placing:
ins:before{
content: "Betty's price: ";
color: #000;
font-size: 14px;
}
in my custom CSS file successfully puts "Betty's price: " before the new "sale" price. The problem is that Betty's Price: is now underlined and clickable and its part of the same link as the price text.
I'm trying to get this text inserted before the price, and not be part of the price. I don't want this text to be formatted along with the price, either.
The same situation applies to the original price:
del:before{
content: "Retail: ";
color: #000;
font-size: 14px;
}
PS: I'm also hoping to add a new line character in addition with the "Betty's price " bit so that that line can go below the retail price, if that changes anything.
Method 1
You have the woocommerce plugin installed. In order to overwrite the woocommerce page templates you need to create a folder called "woocommerce" in your theme's folder. Go to your website's "wp-content/plugins/templates" and copy the file content-product.php to "wp-content/themes/your-theme/woocommerce/". If you're theme is called "mytheme" you will have this path: "wp-content/themes/mytheme/woocommerce/content-product.php".
You can edit the file content-product.php as you want. This file is the one that displays the products on the category pages. You can see that your product is displayed inside a < li >.
Right now, the content-product.php looks like this:
<li <?php post_class( $classes ); ?>>
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?>
<h3><?php the_title(); ?></h3>
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
</a>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
</li>
You can see that after the title it's included this function: do_action( 'woocommerce_after_shop_loop_item_title' );
That function is defined in: plugins/woocommerce\includes\wc-template-hooks.php, so we won't touch it, I consider it a core file. You should edit only the templates files, because, if there will be an update to the plugin, you will loose your settings.
So I suggest you to replace the above code with this one:
<li <?php post_class( $classes ); ?>>
<?php do_action( 'woocommerce_before_shop_loop_item' ); ?>
<a href="<?php the_permalink(); ?>">
<?php
/**
* woocommerce_before_shop_loop_item_title hook
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
?>
<h3><?php the_title(); ?></h3>
<span>Enter Your Text Here!</span>
<?php
/**
* woocommerce_after_shop_loop_item_title hook
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
?>
</a>
<?php do_action( 'woocommerce_after_shop_loop_item' ); ?>
</li>
You can see that I've added a span after the title. You can add a class or a style attribute to that span, or you can change the span to a div in order to modify it as you wish. But the solution will work.
Method 2
When you want to add some texts/images after or before a Woocommerce zone, you need to add a hook. First of all, you need to know how hooks works, so please read about Wordpress actions and filters here.
Now that you know how to add an action or a filter, read about Woocommerce hooks here.
You will have to create a filter in your theme's functions.php file and there you'll add your text before or after the price.
Next time when you have a question regarding the Wordpress CMS, please post it here: https://wordpress.stackexchange.com/

Categories