wordpress apply_filters - wordpress

I'm totally new to wordpress, so apologies for the poor explanation.
I'm trying to return a list of values of the array 'options' I know I could filter this info (and ADD to this array) by using add_filter with 'woocommerce_currencies' but how can I see the values already contained in the array?
Here is a snippet of code, it's from the woocommerce functions.php file - Basically.. I'm trying to echo the values in this options array.
'options' => array_unique(apply_filters('woocommerce_currencies', array(
'USD' => __( 'US Dollars ($)', 'woocommerce' ),
'EUR' => __( 'Euros (€)', 'woocommerce' ),
'GBP' => __( 'Pounds Sterling (£)', 'woocommerce' ).....

You can use the get_option function to retrieve any named option from the database.
$values = get_option('woocommerce_curencies');
foreach ( (array) $values as $option ) {
print_r( $option );
}

You can do:
add_filter('woocommerce_currencies', function($currencies) {
var_dump($currencies);
}, 10, 1 );
You can then modify currencies as you wish.
FYI, 10 is the priority within the woocommerce_currencies filter, and 1 is the number of parameters being passed by the filter.

Related

Woocommerce display Reviews and Ratings by tags

I am building an e-commerce website and I have installed the following plugin (Customer reviews for Commerce - https://wordpress.org/plugins/customer-reviews-woocommerce/) for Reviews and Ratings of Orders once user completes the order process.
However, the nature of the products we deal with (like fabrics, dresses, sarees etc.) will run out of stock and the same product will not be available again to procure. So, I would want to display the reviews and ratings of old orders using the 'tags' of the products which the order had (For this reason, I would like to have review at order line item). Further, the new product page should fetch the reviews and ratings using it's own tags from old orders which had the same tags.
Any guidance would be helpful in this matter!
To approach this problem, first thing to do is to get all tags associated with a given product into an array. And then, WP_Comments_Query needs to be queried with the array of product ids generated in the first step.
Here is a snippet with the above mentioned approach.
function get_reviews_by_tags(){
global $product;
$productid = $product->get_id();
// get all product_tags of the current product in an array
$current_tags = get_the_terms( $productid, 'product_tag' );
//only start if we have some tags
if ( $current_tags && ! is_wp_error( $current_tags ) ) {
//get all related product ids mapped by tags array we created earlier
$relatedproductids_by_tags = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_tag',
'field' => 'term_id',
'terms' => $current_tags,
'operator' => 'IN'
)
),
));
// create a wp comment query object as wc uses comments for reviews
$reviews_args = array(
'post__in' => $relatedproductids_by_tags
);
$reviews_query = new WP_Comment_Query;
$reviews = $reviews_query->query( $reviews_args );
if ( !empty( $reviews ) ) {
foreach ( $reviews as $review ) {
echo '<p>' . $review->comment_content . '</p>';
}
} else {
echo 'No reviews found.';
}
}
add_action( 'woocommerce_after_single_product_summary', 'get_reviews_by_tags', 10, 2 );
}
The above code does not consider any modifications being made by the plugin you mentioned in your question. Also, please note that this code is for fetching and displaying reviews as mentioned in your question. This is not for creating reviews.

Updating custom product meta box field - WooCommerce

I'm trying to update a custom product meta box field using the updating system from WooCommerce core. Here is my code:
The new field Shipping info
add_action( 'woocommerce_product_options_shipping', 'my_product_options_shipping' );
function my_product_options_shipping() {
global $post;
$shipping_info = get_post_meta( $post->ID, '_shipping_info', true );
woocommerce_wp_text_input(
array(
'id' => '_shipping_info',
'value' => $shipping_info,
'label' => __( 'Shipping info', 'woocommerce' ),
'placeholder' => __( 'Shipping in two days', 'woocommerce' ),
)
);
}
And this is the function that adds the new field as prop in WC_Meta_Box_Product_Data::save
add_action( 'woocommerce_admin_process_product_object', 'my_admin_process_product_object' );
function my_admin_process_product_object( $product ) {
$product->set_props(
array(
'shipping_info' => isset( $_POST['_shipping_info'] ) ? wc_clean( wp_unslash( $_POST['_shipping_info'] ) ) : null,
)
);
}
I think I'm missing a step. Shouldn't it be saved automatically from function WC_Meta_Box_Product_Data::save which is attached to woocommerce_process_product_meta?
EDIT
I found the missing step. I need to add my custom post meta in the protected array $extra_data from abstract class WC_Data.
I'm not too good at OOP, so how I can access that array to push my custom data?
I can see you're just doing $shipping_info = get_post_meta( $post->ID, '_shipping_info', true );
So why not save the trouble and just use update_post_meta
add_action( 'woocommerce_admin_process_product_object', 'my_admin_process_product_object' );
function my_admin_process_product_object( $product ) {
update_post_meta($product->get_id(), '_shipping_info', wc_clean( wp_unslash( $_POST['_shipping_info'] ) ) );
}

WP Bakery custom template variable not working

I'm trying to add in grid builder a custom shortcode. The shortcode is working as expected but the printed value is empty.
add_filter( 'vc_grid_item_shortcodes', 'my_module_add_grid_shortcodes' );
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_custom_post_press_link'] = array(
'name' => __( 'Press link', 'domain' ),
'base' => 'vc_custom_post_press_link',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Show custom post meta', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
// output function
add_shortcode( 'vc_custom_post_press_link', 'vc_custom_post_press_link_render' );
function vc_custom_post_press_link_render($atts, $content, $tag) {
return 'test1 {{ press_link }}';
}
add_filter( 'vc_gitem_template_attribute_press_link', 'vc_gitem_template_attribute_press_link ', 10, 2 );
function vc_gitem_template_attribute_press_link( $value, $data ) {
return 'test2';
}
The expected output should be
test1 test2
but i only get
test1
The original code came from the official doc but it doesn't seem to work.
EDIT:
I just tried and the code in the documentation dont work too. I guess its not updated. I need a way to get the value of an ACF field and append some HTML around it.
Solved! The problem was in this line
add_filter( 'vc_gitem_template_attribute_press_link', 'vc_gitem_template_attribute_press_link ', 10, 2 );
that should have been
add_filter( 'vc_gitem_template_attribute_press_link', 'vc_gitem_template_attribute_press_link', 10, 2 );
Notice the missing space at the end of vc_gitem_template_attribute_press_link.

how can i display product's custom field in wp-admin/edit.php?post_type=product ( product listing table in admin ) woocommerce

I have added some custom fields in woocommerce when adding product. Now i want to display those custom fields in product listing page ( wp-admin/edit.php?post_type=product ). I also want to quick edit and save like other values can be edit and save in listing page.
I tried bellow code but it did not work.
add_filter( 'manage_edit-product', 'show_product_order' );
function show_product_order($columns){
$new_columns = (is_array($columns)) ? $columns : array();
unset( $new_columns['order_actions'] );
$new_columns['product_order'] = 'Product Order';
$new_columns['order_actions'] = $columns['order_actions'];
return $new_columns;
}
I also tried below code but it did not worked too.
add_action( 'woocommerce_product_options_general_product_data', 'woocommerce_general_product_data_custom_field' );
function woocommerce_general_product_data_custom_field() {
global $woocommerce, $post;
echo '<div class="options_group">';
woocommerce_wp_checkbox(
array(
'id' => 'product_order',
'wrapper_class' => 'checkbox_class',
'label' => __('Order for Product', 'woocommerce' ),
'description' => __( 'Order for Product', 'woocommerce' )
)
);
echo '</div>';
}
i also reffered this post WooCommerce show custom column but i did not succeed to get solution
Please help.

Custom fields not showing on order

I have a few custom fields within my WooCommerce setup, however I am trying to get them to display on the order view.
This is my code (one custom field for the sake of it):
add_action('woocommerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h3>'.__('Extra Information').'</h3>';
woocommerce_form_field( 'date_of_birth', array(
'type' => 'select',
'class' => array('my-field-class form-row-wide'),
'label' => __('Date of Birth'),
'placeholder' => __('Enter something'),
'required' => true,
'options' => array(
'1995' => __('1995', 'woocommerce' ),
'1994' => __('1994', 'woocommerce' )
)
),
$checkout->get_value( 'date_of_birth' ));
I thought this below would do the job on adding it to the order view, however it does not.. or I am completely wrong and the custom field is not saving?
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 ($_POST['date_of_birth']) update_post_meta( $order_id, 'Extra Information', esc_attr($_POST['date_of_birth']));
I then looked through stackoverflow and found some more information and tried this:
function my_custom_checkout_field1($order){
echo "<p><strong>Date of Birth:</strong> " . $order->order_custom_fields['_date_of_birth'][0] . "</p>";
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field1', 10, 1 );
That does add a "Date of Birth:" to the order view, but unfortunately it is empty afterwards. I tried both _date_of_birth and date_of_birth, apparently it needed the extra _ infront, I got that answer from here: Show custom field on order in woocommerce
I have tried to look inside the database, but can not find the extra details on an order displayed anywhere (no regular details either except order id's).
Can anyone tell me what I am doing wrong and what I can do to make the field show up on the order view?
Thanks
I don´t know, but this:
'Extra Information'
here:
update_post_meta($post_id, $meta_key, $meta_value, $prev_value);
Are you sure 'Extra Information' is the $meta_key? Is not "date_of_birth"?

Resources