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.
Related
I'm attempting to create a page that lists products from woocommerce that have been posted by a specific editor. The below code works great, however it effects all shop and category pages. Is there any way I can make this a stand alone shortcode?
function htdat_shortcode_atts_products( $out, $pairs, $atts, $shortcode ) {
if ( isset ( $atts['author'] ) ) $out['author'] = $atts['author'];
return $out;
}
add_filter('shortcode_atts_products', 'htdat_shortcode_atts_products', 10, 4);
function htdat_woocommerce_shortcode_products_query( $query_args, $attributes ) {
$query_args = array(
'author' => $attributes['author'],
'post_status' => 'publish',
'post_type' => 'product',
'post_per_page' => 12,
);
return $query_args;
}
add_filter( 'woocommerce_shortcode_products_query', 'htdat_woocommerce_shortcode_products_query', 10, 2 );
I expected the shortcode to default to normal if an author id wasnt set. Like so: [products author=0] - but it would just be easier to have two seperate shortcodes such as [products-author] and [products]
I have a small problem
I wrote a simple shortcode function to display my acf value in the grid of the visual grid composer, this is my simple custom shortcode
function my_module_add_grid_shortcodes( $shortcodes ) {
$shortcodes['vc_say_hello'] = array(
'name' => __( 'Say Hello', 'my-text-domain' ),
'base' => 'vc_say_hello',
'category' => __( 'Content', 'my-text-domain' ),
'description' => __( 'Just outputs Hello World', 'my-text-domain' ),
'post_type' => Vc_Grid_Item_Editor::postType(),
);
return $shortcodes;
}
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
if( get_field('item') ):
the_field('item');
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}
When I call the shortcode in the builder, "ITEM Locked" is displayed, even if the value of the element is set in the post !!!
Looks like get_field doesn't know where to get it from in the shortcode. Try adding the current post id as the second parameter
add_shortcode( 'vc_say_hello', 'vc_say_hello_render' );
function vc_say_hello_render() {
$id = get_the_ID();
if( get_field('item', $id) ):
the_field('item', $id);
else:
echo "<h2>ITEM LOCKED</h2>";
endif;
}
This question already has answers here:
Adding multiple tabs to WooCommerce single product pages
(3 answers)
WooCommerce single product custom Tab displaying a page content
(1 answer)
Closed 4 years ago.
Using official documentation I added custom tab in woocommerce:
/**
* Add a custom product data tab
*/
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['test_tab'] = array(
'title' => __( 'New Product Tab', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
function woo_new_product_tab_content() {
// The new tab content
echo '<h2>New Product Tab</h2>';
echo '<p>Here\'s your new product tab.</p>';
}
The problem is that if I try to add another tab using this code, then I get the error.
The code snippet you are trying to save produced a fatal error on line 17:
Cannot redeclare woo_new_product_tab() (previously declared in /var/www/html/sport-print.online/wp-content/plugins/code-snippets/php/snippet-ops.php(352) : eval()'d code:5)
Can you please tell me how to add another tab?
I understand my mistake =).
Work code:
/**
* Add a custom product data tab
*/
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tabs
$tabs['new_tab_1'] = array(
'title' => __( 'new_tab_1', 'woocommerce' ),
'priority' => 11,
'callback' => 'new_tab_1'
);
$tabs['new_tab_2'] = array(
'title' => "new_tab_2",
'priority' => 12,
'callback' => 'new_tab_2'
);
$tabs['new_tab_3'] = array(
'title' => "new_tab_3",
'priority' => 13,
'callback' => 'new_tab_3'
);
return $tabs;
}
function new_tab_1() {
// The new tab content
echo '<ol>
<li>content new_tab_1</li>
</ol>';
}
function new_tab_2() {
echo'
<h3>content new_tab_2</h3>';
}
function new_tab_3() {
echo'<p style="font-weight: bold;">content new_tab_3</p>';
}
The problem is solved.
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
// Adds the new tab
$tabs['desc_tab'] = array(
'title' => __( 'Additional Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}
Paste this code your active theme.
I have an Ajax checkout on my woocommerce installation and I am trying to capture the custom fields on the checkout. I am using below code to try to capture the field called add_gift_box, the field appears fine on my checkout page.
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['add_gift_box']) {
update_post_meta( $order_id, '_add_gift_box', esc_attr($_POST['add_gift_box']));
}
}
When I press "place order" button it creates a new order but the value of this field does not get saved in the database. I don't think even the hook gets fired. As I said everything is being handled by Ajax. The checkout page is one page checkout.
I need help sorting this out. Thanks in advance.
UPDATE
I am setting the field as follows:
add_action( 'woocommerce_after_checkout_billing_form', 'add_box_option_to_checkout' );
function add_box_option_to_checkout( $checkout ) {
woocommerce_form_field( 'add_gift_box', array(
'type' => 'radio',
'class' => array('add_gift_box form-row-wide'),
'label_class' => array('checkbox'),
'input_class' => array('input-checkbox'),
'required' => true,
'options' => array(
'option_1' => 'option1</br>' ,
'option_2' =>'option2</br>',
'option_3' =>'option3</br>',
'option_4' =>'option4</br>',
'option_5' =>'option5</br>',
'option_6' =>'option6</br>',
'option_7' =>'option7</br>',
),
'label' => __('Select Option'),
'placeholder' => __(''),
), $checkout->get_value( 'add_gift_box' ));
}
here is a plugin to add custom field on checkout page woocomerce https://wordpress.org/plugins/woo-custom-checkout-field/
or if you want to add by code you can follow codding from here
https://www.cloudways.com/blog/custom-field-woocommerce-checkout-page/
add_action('woocommerce_after_order_notes', 'customise_checkout_field');
function customise_checkout_field($checkout)
{
echo '<div id="customise_checkout_field"><h2>' . __('Heading') . '</h2>';
woocommerce_form_field('customised_field_name', array(
'type' => 'text',
'class' => array(
'my-field-class form-row-wide'
) ,
'label' => __('Customise Additional Field') ,
'placeholder' => __('Guidence') ,
'required' => true,
) , $checkout->get_value('customised_field_name'));
echo '</div>';
}
Do you have a plugin activated which creates the order by hooking into woocommerce_create_order filter? I don't see any other reason why add_gift_box isn't saved or why woocommerce_checkout_update_order_meta isn't firing.
The original code from includes/class-wc-checkout.php is
/**
* Action hook to adjust order before save.
* #since 3.0.0
*/
do_action( 'woocommerce_checkout_create_order', $order, $data );
// Save the order.
$order_id = $order->save();
do_action( 'woocommerce_checkout_update_order_meta', $order_id, $data );
So, you could create log files in your themes directory to see which hooks are firing like this:
add_action('woocommerce_checkout_update_order_meta', function ($order_id) {
file_put_contents(__DIR__ . '/woocommerce_checkout_update_order_meta.log', 'woocommerce_checkout_update_order_meta fired for order id ' . $order_id);
});
add_action('woocommerce_checkout_create_order', function () {
file_put_contents(__DIR__ . '/woocommerce_checkout_create_order.log', 'woocommerce_checkout_create_order fired');
});
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.