• Resolved charlesgodwin

    (@charlesgodwin)


    I want to develop a link / button on each row in a WPDA table and when clicked will invoke an AJAX call to add the item to a WooCommerce cart. The payload in the AJAX is different for each row. I want to pass the SKU, product ID and special text. All these values are in my data table so can be part of the AJAX call. I see lots of examples of one AJAX button per page, but this is a case of maybe 100 unique buttons invoking the same AJAX call. There’s got to be a straightforward way to do this within the WPDA framework. I am not a great JS programmer but PHP is strong.

    I will be grateful for any suggestions or examples

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Hi Charles,

    Have you seen this section in the documentation:
    https://wpdataaccess.com/docs/documentation/data-explorer/table-and-view-settings/#examples-of-dynamic-hyperlinks

    Please keep in mind:
    (1) When the content of your dynamic hyperlink starts with plain text, the plugin will put your content in an a tag
    (2) When the content of your dynamic hyperlink starts with a html tag or #macro, the plugin will only substitute values and execute macros

    If you want to add javascript to a dynamic hyperlink, make sure the content of your hyperlink starts with a html tag. After that you can add a function call to your hyperlink like this:

    
    <a href="javascript:void(0)" onclick="addItemToCart('$$id$$')">$$text$$</a>
    

    You can add function addItemToCart to your page with the Code Manager. Just like you suggested for the Data Publisher! ??

    This is the simplest solution from my perspective, but you can do this with php as well. ?? Please see the documentation for filter wpda_column_default:
    https://wpdataaccess.com/2020/01/29/filter-wpda_column_default-to-influence-column-layout-in-list-table/

    Filter wpda_column_default is executed for every row/column and allows you to overwrite column content. You can still use a dynamic hyperlink and overwrite that column. But you could add your hyperlink to an existing column as well. You can use any column value in your content with: $item['column_name']

    For column is this would be: $item['id']
    For your dynamic hyperlink: $item['wpda_hyperlink_0']

    Where wpda_hyperlink_0 represents the first dynamic hyperlink of your table. Here is the same examples with a filter:

    
    function my_column_default(
    	$value,
    	$item,
    	$column_name,
    	$table_name,
    	$schema_name,
    	$wpda_list_columns,
    	$list_number,
    	$self
    ) {
    	if ( 'wpda_hyperlink_0' === $column_name ) {
    		return '<a href="javascript:void(0)" onclick="addItemToCart(\'" + $item['id'] + "\')">' . $item['text'] . '</a>';
    	}
    
    	return $value;
    }
    add_filter( 'wpda_column_default' , 'my_column_default' , 10 , 8 );
    

    Make sure the code is executed only for the table involved. To prevent the filter is execute for every column on every page, you might consider to add this filter only when needed.

    Does this help?

    Best regards,
    Peter

    Plugin Author Passionate Programmer Peter

    (@peterschulznl)

    Did you find a solution for this issue Charles?

    Thanks,
    Peter

    Thread Starter charlesgodwin

    (@charlesgodwin)

    Yes and no. I know what I need to do but have not done it. So this is resolved as far as my initial question is concerned but not yet implemented.

    Thanks. You can mark this as resolved

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Add to Cart hotlink’ is closed to new replies.