Can't use the variable $checkout with WooCommerce-Multistep-Checkout - php

I'm trying to create a new step in my checkout page and i'm currently using the plugin WooCommerce-Mulitstep-Checkout plugin. I followed the documentation to create a new step (here : http://woocommerce-multistep-checkout.com/documentation/). So here is my code in function.php:
add_action('woocommerce_multistep_checkout_before_order_info', 'add_my_custom_step_with_new_field');
function add_my_custom_step_with_new_field( $checkout ) {
wc_get_template( 'checkout/my-custom-step.php', array( 'checkout' => $checkout, 'test' => "test" ) );
}
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['my_field_name'] ) ) {
update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
}
}
And here is my code in checkout/my-custom-step.php :
<h1> Step Title</h1>
<div class="my-custom-step">
<?php
woocommerce_form_field('my_field_name', array(
'type' => 'text',
'required' => true,
'class' => array('my-field-class form-row-wide'),
'label' => __('Fill in this field'),
'placeholder' => __('Enter something'),
), $checkout->get_value('my_field_name'));
?>
</div>
}
When I use this code (mostly provided by the official documentation), I get this error on the browser console :
GET https://localhost/mywebsite/checkout/ 500 (Internal Server Error)
And when I remove , $checkout->get_value('my_field_name'), it works perfectly fine (but without prefilling the fields.
Moreover, when I use var_dump($checkout);, it displays me this : string(0) "".
Finally, when I want to display $test, it works perfectly fine. So I don't think I use badly the variable.
So my questions are :
Do you know all the mystery behind this empty variable ?
Did I misused something ?
Why is there a final curly bracket at the end of the template in the official documentation ?
Thanks

Related

Wordpress, redirect user after clicking delete post shortcut button

I am working on a new plugin. I am dealing with a problem that I have outlined in the title. My intention is to redirect the user to the My Pages page after the user clicks the "Delete page" button.
Here is my code:
function custom_admin_bar_delete_link( $wp_admin_bar ) {
global $post;
if( is_admin() || ! is_object( $post ) )
return;
if ( ! current_user_can( 'delete_pages' ) )
return;
if ( $post->post_type != 'page' )
return;
$args = array(
'id' => 'delete_link',
'title' => 'Delete this page',
'href' => get_delete_post_link( $post->ID ),
'meta' => array( 'class' => 'delete-link' )
);
$wp_admin_bar->add_node( $args );
}
add_action( 'admin_bar_menu', 'custom_admin_bar_delete_link', 999 );
function custom_page_delete_redirect( $location, $post_id ) {
$post = get_post( $post_id );
if ( 'page' === $post->post_type && 'trash' === get_post_status( $post_id ) ) {
return admin_url( 'edit.php?post_type=page' );
}
return $location;
}
add_filter( 'wp_redirect', 'custom_page_delete_redirect', 10, 2 );
Thank you.
Without thinking too hard about it, I would add an ID to your button and just write some jQuery to do the redirect.
(The PHP: NOT tested)
EDIT:
Per the Wordpress documentation, you should be able to add an ID:
https://developer.wordpress.org/reference/classes/wp_admin_bar/add_node/
$args = array(
'id' => 'delete_link',
'title' => 'Delete this page',
'href' => get_delete_post_link( $post->ID ),
'id' => "someid",
'meta' => array( 'class' => 'delete-link' )
);
The jQuery:
EDIT:
How are you adding the jQuery? You should probably save it in a separate file in your plugin folder (maybe in a subfolder named "js") and enqueue it in your plugin. The 2nd answer to this question would be a good place to start:
https://wordpress.stackexchange.com/questions/42641/how-to-include-a-simple-jquery-file-into-a-wordpress-plugin
jQuery(document).ready(function($){
$('body').on('click','#someid',function(event){
// window.alert('jquery executing');
setTimeout(function(){window.location.href = "http://example.com/page/";}, 500);
});
});
Thoughts: I added a timeout, because Wordpress loads slowly sometimes, and I've had to do this for other applications in Wordpress, but try without and see if it performs without the timeout.
Have a look at this answer:
https://wordpress.stackexchange.com/questions/132196/get-delete-post-link-redirect
The question is similar, and this may get you where you need to go.

Woocommerce Checkbox

I've got a client who needs a checkbox for "accepting the terms and conditions etc." The tricky thing is that he will be adding in-person purchases on the admin panel and needs the checkbox there as well. I got the checkbox on both the front and back-end but I'm having some trouble getting them to talk to each other. When placing an order on the front-end, it shows as admin-panel-pic
But when you click into edit the order, the checkbox in not checked and if you update the order at all the value will disappear. I'm sure I'm missing something obvious but this is the first time I've had to modify Woocommerce fields like this.
Here's the code pertaining to the checkbox I have currently in my functions.php:
<?php
add_action('woocommerce_before_order_notes', 'wps_add_select_checkout_field');
function wps_add_select_checkout_field( $checkout ) {
woocommerce_form_field( 'rem_terms', array(
'type' => 'checkbox',
'class' => array('form-row mycheckbox'),
'label_class' => array('woocommerce-form__label woocommerce-form__label-for-checkbox
checkbox'),
'input_class' => array('woocommerce-form__input woocommerce-form__input-checkbox input-
checkbox'),
'required' => true,
'label' => 'I agree with the terms and conditions described in the Privacy Policy and the <a
target="_blank" rel="noopener" href="/liability-release-and-express-assumption-of-risk">
Liability Release </a>',) // Label and Link
);
$checkout->get_value( 'rem_terms' );
}
add_action('woocommerce_checkout_update_order_meta',
'wps_select_checkout_field_update_order_meta');
add_action( 'woocommerce_checkout_process', 'bt_add_checkout_checkbox_warning' );
/**
* Alert if checkbox not checked
*/
function bt_add_checkout_checkbox_warning() {
if ( ! (int) isset( $_POST['rem_terms'] ) ) {
wc_add_notice( __( 'Please agree to our terms and conditions before placing your order' ),
'error' );
}
}
function wps_select_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['rem_terms'] ) ) {
update_post_meta( $order_id, 'rem_terms', sanitize_text_field( $_POST['rem_terms'] ) );
}
}
add_action( 'woocommerce_admin_order_data_after_order_details',
'misha_editable_order_meta_general' );
function misha_editable_order_meta_general( $order ){ ?>
<br class="clear" />
<h4>Checkbox</h4>
<?php
/*
* get all the meta data values we need
*/
$rem_terms = get_post_meta( $order->get_id(), 'rem_terms', true );
?>
<div class="address">
<p><strong>Customer accepts REM's terms and conditions described in the
privacy policy</strong><?php echo $rem_terms?></p>
</div>
<div class="edit_address"><?php
woocommerce_wp_checkbox( array(
'id' => 'rem_terms',
'label' => 'Customer Agrees to terms and conditions',
'value' => $rem_terms,
'wrapper_class' => 'form-field-wide'
) );
?></div>
<?php }
add_action( 'woocommerce_process_shop_order_meta', 'save_new_fields' );
function save_new_fields( $ord_id ){
update_post_meta( $ord_id, 'rem_terms', wc_clean( $_POST[ 'rem_terms' ] ) );
}
function example_custom_order_fields( $fields, $order ) {
$new_fields = array();
if( get_post_meta( $order->id, 'rem_terms', true ) ) {
$new_fields['rem_terms'] = array(
'label' => 'Cust Accepts Terms',
'value' => get_post_meta( $order->id, 'rem_terms', true )
);
}
return array_merge( $fields, $new_fields );
}
add_filter( 'wcdn_order_info_fields', 'example_custom_order_fields', 10, 2 );
I was able to sort this out. Simply reassigning the value of the checkbox from 1 to "yes" as needed.

woocommerce product attributes empty

I am having problems understanding the following:
I created a custom field in the woocommerce products by the code below.
The custome field shows up and I can fill it as expected.
I don't know how to access the data though.
if I call $product->get_attributes() it returns an empty array.
when I echo $product though I get an json like return value where all expected values are presented.
But how do I access them?
Thanks in advance
function
woocom_general_product_data_custom_field() {
// Create a custom text field
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea',
'label' => __( 'textarea' ),
'placeholder' => '',
'description' => __( '',
'woocommerce' )
)
);
}
add_action(
'woocommerce_process_product_meta',
'woocom_save_general_proddata_custom_field' );
function woocom_save_general_proddata_custom_field( $post_id ) {
// Save Textarea
$textarea = $_POST['_textarea'];
if( ! empty( $textarea ) ) {
update_post_meta( $post_id, '_textarea', esc_html( $textarea ) );
}
}
got it now.
I found the info in
$product->get_meta('_textarea');
not in the attributes

Wordpress customizer preview doesn't refresh functions.php

So I ran into a issue where the customizer preview doesn't fully refresh. Only when I manually refresh the page I see my changes. Some of my code to help explain below.
For my customizer settings I have code that looks something like this
$wp_customize->add_section( 'theme_layout', array(
'title' => __( 'Layout', 'theme' ),
'priority' => 30
) );
$wp_customize->add_setting( 'theme_header_layout', array(
'default' => 'default',
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Control( $wp_customize,
'theme_header_layout', array(
'label' => __( 'Header', 'theme' ),
'section' => 'theme_layout',
'settings' => 'theme_header_layout',
'type' => 'select',
'choices' => array(
'default' => 'default',
'special_header' => 'Special Header',
)
) ) );
Now In my functions.php I have code like this
//this is the code that doesn't seem to execute when the customizer refreshes
if ( 'special_header' == get_theme_mod( 'theme_header_display' ) ):
function theme_special_header( $items ) {
$items .= do_shortcode('[special_header_shortcode]');//This shortcode exists I just didnt bother mentioning it here
}
add_action( 'wp_nav_menu_secondary-menu_items', 'theme_special_header' );//Adds shortcode to menu with id of secondary-menu
endif;
This all works great accept when I go to the customizer and select 'Special Header' the customizer refreshes and I don't see my changes until I completely refresh the page.
I had also faced similar issue earlier. Rather than adding conditional outside, I kept it inside the function and it worked. You can try similar approach for your code and it may help.
Following is not exact answer for your question but it may help to fix your problem.
function wpso_customize_menu($items, $args) {
if ( 'special_header' == get_theme_mod( 'theme_header_layout' ) ) {
if( $args->theme_location == 'menu-1' ) {
$items .= '<li>Custom Link</li>';
}
}
return $items;
}
add_filter('wp_nav_menu_items', 'wpso_customize_menu', 10, 2);

Show one WooCommerce Product meta

I am using one plugin for WooCommerce GST implementation. It has one HSN field inside product page which is stored in the database as the product meta.
The plugin does not have any specific code to get the HSN value and use it anywhere.
The code for HSN in the plugin is below:
public function fn_add_product_custom_meta_box() {
woocommerce_wp_text_input(
array(
'id' => 'hsn_prod_id',
'label' => __('HSN Code', 'woocommerce' ),
'description' => __( 'HSN Code is mandatory for GST.', 'woocommerce' ),
'custom_attributes' => array( 'required' => 'required' ),
'value' => get_post_meta( get_the_ID(), 'hsn_prod_id', true )
)
);
}
public function fn_save_license_field( $post_id ) {
$value = ( $_POST['hsn_prod_id'] )? sanitize_text_field( $_POST['hsn_prod_id'] ) : '' ;
update_post_meta( $post_id, 'hsn_prod_id', $value );
}
I want to get the HSN value by importing it from product meta and I have used this code to get that without any result:
<?php $meta = get_post_meta( get_the_ID(), 'hsn_prod_id', true ); ?>
Can anyone please check my code and correct me how to add the HSN value anywhere using the code?
I am using the code inside a different plugin to get the HSN code.
Thank you.

Categories