I rephrase my question in this new post to which I have not had any answer. These two functions create a page on the user's account endpoint. A table is published on this page. I would like this table to be published in a new page / post of the site accessible to all. Any suggestions? Thank you
1 add_action( 'init', array(__CLASS__, 'wsc_add_my_account_endpoint') );
2 public static function wsc_add_my_account_endpoint() { add_rewrite_endpoint( 'wsc-share-cart', EP_ROOT | EP_PAGES ); }
3 The content that I would to print in a wordpress page:
if ( !isset($_GET['wc-wsc']) || !isset($_GET['nounce']) ) {
return;
}
if ( ! wp_verify_nonce( wc_clean($_GET['nounce']), 'wsc_save_share_cart' ) ) {
return;
}
$posted_fields = wc_clean($_POST);
$fields = get_option('wsc_form_fields');
$errors = array();
$form_submission = array();
$form_submission_html = '';
$customer_email = '';
if ( !empty($fields) ) {
$form_submission_html .= '<table>';
foreach ( $fields as $key => $field ) {
if ( true == $field['required'] && empty($posted_fields['wsc_form_field_' . esc_attr($key)]) ) {
$errors['wsc_form_field_' . esc_attr($key)] = wp_sprintf( '%s %s', esc_html__($field['label'], 'wc-wsc'), esc_html__('is required.', 'wc-wsc') );
}
$label = !empty($field['label']) ? $field['label'] : 'wsc_form_field_' . esc_attr($key);
$field_value = wc_clean($posted_fields['wsc_form_field_' . esc_attr($key)]);
if ( 'email' == $field['type'] ) {
if ( !is_email($field_value) ) {
$errors['wsc_form_field_' . esc_attr($key)] = wp_sprintf( '%s %s', esc_html__($field['label'], 'wc-wsc'), esc_html__('must be an email.', 'wc-wsc') );
}
$customer_email = $field_value;
}
if ( isset($posted_fields['wsc_form_field_' . esc_attr($key)]) ) {
$form_submission[$label] = $field_value;
}
$form_submission_html .= '<tr><th>' . esc_html($label) . '</th><td>' . esc_html($field_value) . '</td></tr>';
}
$form_submission_html .= '</table>';
} ```
Related
I'm trying to add validation to some checkout fields when editing an order from admin panel, but I haven't found the hook for that. I want to prevent saving post if validation fails and also show a notice. I've tried woocommerce_process_shop_order_meta hook but post is always saved and notices are not shown.
This is the code I used in my functions.php file:
$meta_box_errors[] = array();
add_action( 'woocommerce_process_shop_order_meta', 'save_order_custom_field_meta_data', 12, 2 );
public function save_order_custom_field_meta_data( $post_id, $post ){
$field_name = 'billing_nif';
$nif_value = '';
if( isset( $_POST[ '_' . $field_name ] ) && !empty($_POST[ '_' . $field_name ]) ) {
$nif_value = sanitize_text_field( $_POST[ '_' . $field_name ] );
if (!empty ($nif_value)) {
if (! validate_nif( $nif_value )) {
$meta_box_errors["invalid_nif"] = 'Invalid NIF';
}
} else {
$meta_box_errors["empty_nif"] = 'Empty NIF';
}
} else {
$meta_box_errors["empty_nif"] = 'Empty NIF';
}
if( ! empty( $this->meta_box_errors ) ) {
add_action( 'admin_notices', 'output_errors' );
}
}
public function validate_nif( $nif ){
$nif = strtoupper($nif);
if (strlen($nif) != 9) {
return false;
}
$nifExp = "^((\d{8})([A-Z]{1}))$^";
return preg_match($nifExp, $nif));
}
public function output_errors() {
echo '<div id="woocommerce_errors" class="error notice is-dismissible">';
foreach ( $meta_box_errors as $error ) {
echo '<p>' . wp_kses_post( $error ) . '</p>';
}
echo '</div>';
unset($meta_box_errors);
}
Based on Get product custom attributes to display them in WooCommerce product loop answer code.
I am displaying specific product attributes on the single product page with this:
add_action('woocommerce_single_product_summary', 'display_custom_attributes', 36 );
function display_custom_attributes() {
global $product;
$attributes_names = array('Brand', 'Color', 'Size');
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
if ( ! empty($attributes_data) ) {
echo '<h4>Details</h4><ul><li>' . implode( '</li><li>', $attributes_data ) . '</ul>';
}
Now I also need to add two custom meta fields ('Serial_Number' and 'MPN') to this list.
How can I add these?
To add two custom meta fields:
Serial_Number
MPN
to this list, you can use:
function action_woocommerce_single_product_summary() {
global $product;
$attributes_names = array( 'Brand', 'Color', 'Size' );
$attributes_data = array();
foreach ( $attributes_names as $attribute_name ) {
if ( $value = $product->get_attribute($attribute_name) ) {
$attributes_data[] = $attribute_name . ': ' . $value;
}
}
// NOT empty
if ( ! empty($attributes_data) ) {
echo '<h4>' . __( 'Details', 'woocommerce' ) . '</h4><ul><li>' . implode( '</li><li>', $attributes_data );
}
// Get meta
$sn = $product->get_meta( 'Serial_Number' );
$mpn = $product->get_meta( 'MPN' );
// NOT empty
if ( ! empty ( $sn ) ) {
echo '<li>' . __( 'My label 1: ', 'woocommerce' ) . $sn . '</li>';
}
// NOT empty
if ( ! empty ( $mpn ) ) {
echo '<li>' . __( 'My label 2: ', 'woocommerce' ) . $mpn . '</li>';
}
echo '</ul>';
}
add_action( 'woocommerce_single_product_summary', 'action_woocommerce_single_product_summary', 36, 0 );
Add this code on the function.php theme file
function call_product_meta_data()
{
global $post;
if ($post) {
$post_meta_value = get_post_meta($post->ID, 'post_meta_key', true);
if ($post_meta_value != null) {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: $post_meta_value</p></div>";
} else {
echo "<div class='pr-meta'><p><strong>Meta Title</strong>: N/A</p></div>";
}
}
}
add_action('woocommerce_single_variation', 'call_product_meta_data');
I have done a change in the class-wc-product-cat-list-walker.php file and following is the changes in the code
public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
$cat_id = intval( $cat->term_id );
$output .= '<li class="cat-item cat-item-' . $cat_id;
if ( $args['current_category'] === $cat_id ) {
$output .= ' current-cat';
}
if ( $args['has_children'] && $args['hierarchical'] && ( empty( $args['max_depth'] ) || $args['max_depth'] > $depth + 1 ) ) {
$output .= ' cat-parent';
}
if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat_id, $args['current_category_ancestors'], true ) ) {
$output .= ' current-cat-parent';
}
$pageurl = wp_get_referer();
$template = basename($pageurl);
$post_id = get_the_ID();
$catpage = get_field( 'catelog_page', $post_id );
$catalogpage = 'no';
if (isset($_GET['catalog'])) {
$catalogpage = 'yes';
}
if ( $catpage == TRUE || $catalogpage=='yes' ) {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
else {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
if ( $args['show_count'] ) {
$output .= ' <span class="count">(' . $cat->count . ')</span>';
}
}
I want to keep it in the child theme and make it functional. Is there any way? I have tried to copy the file and paste it in the child theme WooCommerce folder. but it is not working.
please help me to know the way to change it. Thanks in advance.
You can create your own custom class and try to override WC_Product_Cat_List_Walker.
So you can do something like this
class Custom_WC_Product_Cat_List_Walker extends WC_Product_Cat_List_Walker {
/**
* What the class handles.
*
* #var string
*/
public $tree_type = 'product_cat';
/**
* DB fields to use.
*
* #var array
*/
public $db_fields = array(
'parent' => 'parent',
'id' => 'term_id',
'slug' => 'slug',
);
public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) {
$cat_id = intval( $cat->term_id );
$output .= '<li class="cat-item cat-item-' . $cat_id;
if ( $args['current_category'] === $cat_id ) {
$output .= ' current-cat';
}
if ( $args['has_children'] && $args['hierarchical'] && ( empty( $args['max_depth'] ) || $args['max_depth'] > $depth + 1 ) ) {
$output .= ' cat-parent';
}
if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat_id, $args['current_category_ancestors'], true ) ) {
$output .= ' current-cat-parent';
}
$pageurl = wp_get_referer();
$template = basename($pageurl);
$post_id = get_the_ID();
$catpage = get_field( 'catelog_page', $post_id );
$catalogpage = 'no';
if (isset($_GET['catalog'])) {
$catalogpage = 'yes';
}
if ( $catpage == TRUE || $catalogpage=='yes' ) {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
else {
$output .= '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '';
}
if ( $args['show_count'] ) {
$output .= ' <span class="count">(' . $cat->count . ')</span>';
}
}
}
Then you can call that function like this
add_filter('woocommerce_product_categories_widget_args', 'custom_product_categories_widget_args', 10, 1);
function custom_product_categories_widget_args($args) {
$args['walker'] = new Custom_WC_Product_Cat_List_Walker;
return $args;
}
Just use this code in your active theme functions.php file and check.
This code shows how to display custom product attributes for without variations. I wish to display all the variations too in the typical dropdown select menu. How do I do that?
P/S: I can't post the code here because this site says I have too much code and too little details.
UPDATED:
/**
* Show all product attributes on the product page
*/
function isa_woocommerce_all_pa(){
global $product;
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
$out = '';
foreach ( $attributes as $attribute ) {
// skip variations
if ( $attribute->get_variation() ) {
continue;
}
$name = $attribute->get_name();
if ( $attribute->is_taxonomy() ) {
$terms = wp_get_post_terms( $product->get_id(), $name, 'all' );
// get the taxonomy
$tax = $terms[0]->taxonomy;
// get the tax object
$tax_object = get_taxonomy($tax);
// get tax label
if ( isset ($tax_object->labels->singular_name) ) {
$tax_label = $tax_object->labels->singular_name;
} elseif ( isset( $tax_object->label ) ) {
$tax_label = $tax_object->label;
// Trim label prefix since WC 3.0
if ( 0 === strpos( $tax_label, 'Product ' ) ) {
$tax_label = substr( $tax_label, 8 );
}
}
$out .= $tax_label . ': ';
$tax_terms = array();
foreach ( $terms as $term ) {
$single_term = esc_html( $term->name );
array_push( $tax_terms, $single_term );
}
$out .= implode(', ', $tax_terms) . '<br />';
} else {
$out .= $name . ': ';
$out .= esc_html( implode( ', ', $attribute->get_options() ) ) . '<br />';
}
}
echo $out;
}
add_action('woocommerce_single_product_summary', 'isa_woocommerce_all_pa', 25);
I made a plugin that converts any uri to a hyperlink, if it finds hyper-link it will ignore it and only convert the no hyper-links uri.
In the plugin admin page I set two options, first to convert links only for the front-end, and the second option is to convert uri when ever a post/comment saved to database.
// permanently means before save to database
if ( $this->plugin_options['uri2links_convert_method'] == 'permanently' ) {
add_action( 'wp_insert_post_data', array($this, 'make_post_url_clickable' ) );
//add_action( 'preprocess_comment', array($this, 'make_comment_url_clickable' ) );
}
else {
add_filter( 'the_content', array($this, 'make_post_url_clickable' ) );
//add_filter( 'preprocess_comment', array($this, 'make_comment_url_clickable' ) );
}
My problem here if I set the plugin option to convert uri for front-end it will ignore hyper-links and convert the other uri, but if I set it to convert before save to database it 'll not ignore hyper-links and treat them as normal uri witch make the results look like this:
<a class="sdf" href=" <a href="http://test.test-75.1474.stackoverflow.com/" >http://test.test-75.1474.stackoverflow.com/ </a>
<a class="sdf" href=" <a href="https://www.stackoverflow.com" >https://www.stackoverflow.com </a>
The complete plugin source code:
<?php
/*
Plugin name: URIs to a click-able links
Plugin URI:
Description: Convert URI's to a click-able links
Author: h Abdou
Author URI: habdou.me
Version: 1.0
*/
// The admin page for the plugin
require_once( 'uri2links-menu.php' );
class Uri2Links {
protected $css = '';
protected $rel = '';
protected $target = '';
protected $convert_links = '';
protected $convert_emails = '';
protected $plugin_options = array();
protected $url_match_pattern = '/[a-zA-Z0-9\.\/\?\:#\-_=#]+\.([a-zA-Z0-9\.\/\?\:#\-_=#])+/';
protected $email_match_pattern = '/^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$/';
//protected $links_match_pattern = '/<a(?:"[^"]*"[\'"]*|\'[^\']*\'[\'"]*|[^\'">])+>^[a-zA-Z0-9_.+-]+#[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$<\/a>/';
protected $links_match_pattern = '/<a[^>]*>(.*?)<\/a>/';
protected $links_matched = array();
public function __construct() {
//$this->plugin_options = get_option( 'uri2links_plugin_options' );
$this->plugin_options = array( 'uri2links_convertLinksEmails' => array( 'Links', 'Emails' ), 'uri2links_convert_method' => 'permanently' );
$this->css = isset( $this->plugin_options['uri2links_custom_css'] ) ? ' class="' . $this->plugin_options['uri2links_custom_css'] . '"' : '';
$this->rel = isset( $this->plugin_options['uri2links_rel_attr'] ) ? ' rel="' . $this->plugin_options['uri2links_rel_attr'] . '"' : '';
if ( isset( $this->plugin_options['uri2links_open_links_method'] ) && $this->plugin_options['uri2links_open_links_method'] == 'new')
$this->target = ' target="_blank"';
$this->convert_links = isset( $this->plugin_options['uri2links_convertLinksEmails'][0] ) ? $this->plugin_options['uri2links_convertLinksEmails'][0] : '';
$this->convert_emails = isset( $this->plugin_options['uri2links_convertLinksEmails'][1] ) ? $this->plugin_options['uri2links_convertLinksEmails'][1] : '';
if ( $this->plugin_options['uri2links_convert_method'] == 'permanently' ) {
add_action( 'wp_insert_post_data', array($this, 'make_post_url_clickable' ) );
//add_action( 'preprocess_comment', array($this, 'make_comment_url_clickable' ) );
}
else {
add_filter( 'the_content', array($this, 'make_post_url_clickable' ) );
//add_filter( 'preprocess_comment', array($this, 'make_comment_url_clickable' ) );
}
// The admin page for the plugin
new Uri2LinksMenu;
}
// Convert all URI in a post to hyper-links.
public function make_post_url_clickable( $content ) {
$links = $this->hash_links( $content );
if ( $this->convert_links == 'Links' ) {
$content = preg_replace($this->url_match_pattern, ' <a href="\\0"' . $this->css . $this->rel . $this->target . ' >\\0</a> ', $content);
}
if ( $this->convert_emails == 'Emails' ) {
$content = preg_replace($this->email_match_pattern, ' <a href="mailto:\\0"' . $this->css . $this->rel . $this->target . ' >\\0</a> ', $content);
}
// Replace back the hashed 'a' tags to the original status.
foreach ( $links as $link_hash => $link_text ) {
$content = str_replace( $link_hash, $link_text, $content );
}
return $content;
}
// Same as 'make_post_url_clickable' but this for comments
public function make_comment_url_clickable( $content ) {
$links = hash_links( $content['comment_content']['comment_content'] );
if ( $this->convert_links == 'Links' ) {
$content['comment_content'] = preg_replace($this->url_match_pattern, ' <a href="\\0"' . $this->css . $this->rel . $this->target . ' >\\0</a> ', $content['comment_content']);
}
if ( $this->convert_emails == 'Emails' ) {
$content['comment_content'] = preg_replace($this->email_match_pattern, ' <a href="mailto:\\0"' . $this->css . $this->rel . $this->target . ' >\\0</a> ', $content['comment_content']);
}
foreach ( $links as $link_hash => $link_text ) {
$content['comment_content'] = str_replace( $link_hash, $link_text, $content['comment_content'] );
}
return $content;
}
// hash all 'a' tags so 'make_*_url_clickable' will ignore them.
public function hash_links( &$content ) {
$links = array();
if ( preg_match_all( $this->links_match_pattern, $content, $matches ) ) {
$array_size = sizeof( $matches[0] );
for ( $i = 0; $i < $array_size; ++$i ) {
$link_hash = md5( $matches[0][$i] );
$links[$link_hash] = $matches[0][$i];
$content = str_replace( $matches[0][$i], $link_hash, $content );
}
}
//die('hash_links called!');
return $links;
}
}
new Uri2Links;
?>
I just want to mention that WordPress already got the make_clickable() function that takes text uri to HTML links.
To activate it for the post content we only need this simple plugin:
<?php
/** Plugin Name: Make text uri to HTML links in the post content **/
add_filter( 'the_content', 'make_clickable', 9 );
By default this function runs on the comment text:
add_filter( 'comment_text', 'make_clickable', 9 );