I am trying to take the price in the filter woocommerce_update_order_review_fragments, but failing to understand where it resides
add_filter('woocommerce_update_order_review_fragments', 'price_bottom_checkout');
function price_bottom_checkout($arr) {
$price = ? // how to get it over here?
$price_txt = '<span class="total-pay">'.$price.'</span>';
$arr['.total-pay'] = $price;
return $arr;
}
Managed to find out how. Needed to see the session cart which is updated on every cart refresh via ajax.
add_filter('woocommerce_update_order_review_fragments', 'price_bottom_checkout');
function price_bottom_checkout($arr) {
global $woocommerce;
$the_total = '';
foreach ($woocommerce->cart->cart_contents as $cart_item) {
$the_total = $cart_item['line_total'];
break;
}
$price_txt = '<span class="total-pay">'.wc_price($the_total).'</span>';
$arr['.total-pay'] = $price_txt;
return $arr;
}
Related
We are facing a problem with a website.
A prospect noticed that a form was prefilled with data from another prospect.
We have 4 contactforms on one page and when a contact has entered a form than the data is saved in a session. When they return the forms are prefilled with that data from the session. But now we got a printscreen from a prospect with data from a previous prospect.
Here's the code for the session:
//This will start a session, then save the posted_data[something] to the session
add_action('init', 'myStartSession', 1);
add_action('wp_logout', 'myEndSession');
add_action('wp_login', 'myEndSession');
function myStartSession() {
if ( empty( $_SESSION ) ) {
session_start();
}
}
function myEndSession() {
session_destroy ();
}
function set_sessions($cf7){
/* Use WPCF7_Submission object's get_posted_data() method to get it. */
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$posted_data = $submission->get_posted_data();
}
$_SESSION['formdata']['firstname'] = $posted_data['firstname'];
$_SESSION['formdata']['lastname'] = $posted_data['lastname'];
$_SESSION['formdata']['email2'] = $posted_data['email2'];
$_SESSION['formdata']['tel'] = $posted_data['tel'];
$_SESSION['formdata']['postcode'] = $posted_data['postcode'];
$_SESSION['formdata']['adres'] = $posted_data['adres'];
return false;
}
add_action( 'wpcf7_before_send_mail', 'set_sessions' );
function firstname(){
return $_SESSION['formdata']['firstname'];
}
add_shortcode('firstname', 'firstname');
function lastname(){
return $_SESSION['formdata']['lastname'];
}
add_shortcode('lastname', 'lastname');
function email2(){
return $_SESSION['formdata']['email2'];
}
add_shortcode('email2', 'email2');
function tel(){
return $_SESSION['formdata']['tel'];
}
add_shortcode('tel', 'tel');
function postcode(){
return $_SESSION['formdata']['postcode'];
}
add_shortcode('postcode', 'postcode');
function adres(){
return $_SESSION['formdata']['adres'];
}
add_shortcode('adres', 'adres');
Have you checked if you are caching the pages? This could lead to an issue like that.
I am trying to pre select a value within a select field in my contact from on the contact page from another page by passing a query string ?request=call-back. I am using Ninja Forms and the values that I have in my select field are: email-us, call-back
I have tried the following:
add_filter( 'ninja_forms_render_default_value', 'my_ninja_forms_pre_populate', 10, 3 );
function my_ninja_forms_pre_populate( $default_value, $field_type, $field_settings ){
if( 'request' == $field_settings[ 'key' ] ){
$default_value = $_GET['request'];
}
return $default_value;
}
I would like the select field to have call-back already selected.
I didnt need to use the filter that I was attempting. I changed the key value under administration within the ninja form builder and I made sure that none of the values were pre selected.
This drove me mad... but finally I've got a solution:
// register custom get parameter (to use it with get_query_var() for safety reasons)
function add_get_val() {
global $wp;
$wp->add_query_var('request');
}
add_action('init', 'add_get_val');
add_filter('ninja_forms_localize_field_listselect', function ($field) {
if (get_query_var('request')) {
$request = get_query_var('request');
$optionExists = FALSE;
// check if field exists
foreach ($field['settings']['options'] as $option) {
if ($option['value'] === $request) {
$optionExists = TRUE;
break;
}
}
if ($optionExists) {
foreach ($field['settings']['options'] as $key => $option) {
// deselect all fields
$field['settings']['options'][$key]['selected'] = 0;
// select parameter
if ($option['value'] === $request) {
$field['settings']['options'][$key]['selected'] = 1;
}
}
}
}
return $field;
});
I would like to add a select field in admin/post-new.php.
This select field will be populated with JSON data (from a GET URL).
^ This point is solved. In your function.php:
function acf_load_colors_field_choices($field) {
$field['choices'] = [];
$choices = json_decode(file_get_contents('http://127.0.0.1/json/colors'), true);
if (is_array($choices)) {
foreach ($choices as $choice) {
$field['choices'][$choice] = $choice;
}
}
return $field;
}
add_filter('acf/load_field/name=colors', 'acf_load_colors_field_choices');
Once the page is ready to be published, I would like to catch POST data to send them to another URL.
How to catch those data?
In functions.php, to catch POST data:
function on_save_post_articles($post_id) {
var_dump($post_id);
var_dump($_POST);
exit;
}
add_action('save_post', 'on_save_post_articles');
Solved!
I am using the latest version of wordpress and Woocommerce.
My Functionality is " Overriding the existing Quantity of Products already in cart". I already try this idea but its failure. See the Code and its errors.
add_action('woocommerce_add_to_cart_handler', 'update_product_in_cart', 11, 2);
function update_product_in_cart($p, $q) {
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
$wCart = $woocommerce->cart->get_cart();
// If cart already exists, and product exists, than remove product, and add the new product to it.
if ($wCart)
{
$cart_item_keys = array_keys($wCart);
foreach($cart_item_keys as $key)
{
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
if ($productItemId == $currentProductId)
{
// If you want to empty the entire cart...
// $woocommerce->cart->empty_cart();
// If you want to remove just the product from the cart...
$woocommerce->cart->set_quantity($key, 0);
}
}
}
}
// This adds the product to the cart...
return $q;
}
Its Error is:
Catchable fatal error: Object of class WC_Product_Grouped could not be converted to string in plugins\woocommerce\includes\class-wc-form-handler.php on line 715
This code is the answer, Previously I added on comment boxes....
add_action('woocommerce_add_to_cart_handler', 'update_product_in_cart', 11, 2);function update_product_in_cart($p, $q) { global $woocommerce; $cartItem = $woocommerce->cart->cart_contents; $currentProductId = $q->id; $wCart = $woocommerce->cart->get_cart(); if ($wCart){ $cart_item_keys = array_keys($wCart); foreach($cart_item_keys as $key) {foreach($cartItem as $item) {$productItemId = $item['product_id']; if ($productItemId == $currentProductId) {$woocommerce->cart->set_quantity($key, 0); }}}}return $q; }
Apparently the GridFieldExportButton only exports the currently visible data-set (paginated). Is there a way to make it export all the rows from a model?
Or alternatively: Is there a way to show all rows (eg. bypass pagination), so that the user can perform an export after showing all the rows? I don't want to show all rows all the time (which would probably be possible by setting ModelAdmin::set_page_length(<ridiculouslyHighNumber>);) but only on demand.
You can override ModelAdmin::getExportFields() to define the columns you want to export.
The method needs to return an array with column name as the key, and the db field as the value.
For example:
class MyCustomModelAdmin extends ModelAdmin {
....
public function getExportFields() {
return array(
'FirstName' => 'FirstName',
'Surname' => 'Surname',
'Age' => 'Age'
);
}
}
Solved it by creating a custom subclass of the GridFieldExportButton and using this for my models. The key is to use $gridField->getList(); instead of $gridField->getManipulatedList(); in the generateExportFileData method.
Here's the complete class for anybody interested:
class GridFieldExportAllButton extends GridFieldExportButton {
/**
* Generate export fields for CSV.
*
* #param GridField $gridField
* #return array
*/
public function generateExportFileData($gridField) {
$separator = $this->csvSeparator;
$csvColumns = ($this->exportColumns)
? $this->exportColumns
: singleton($gridField->getModelClass())->summaryFields();
$fileData = '';
$columnData = array();
$fieldItems = new ArrayList();
if($this->csvHasHeader) {
$headers = array();
// determine the CSV headers. If a field is callable (e.g. anonymous function) then use the
// source name as the header instead
foreach($csvColumns as $columnSource => $columnHeader) {
$headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader;
}
$fileData .= "\"" . implode("\"{$separator}\"", array_values($headers)) . "\"";
$fileData .= "\n";
}
$items = $gridField->getList();
foreach($items as $item) {
$columnData = array();
foreach($csvColumns as $columnSource => $columnHeader) {
if(!is_string($columnHeader) && is_callable($columnHeader)) {
if($item->hasMethod($columnSource)) {
$relObj = $item->{$columnSource}();
} else {
$relObj = $item->relObject($columnSource);
}
$value = $columnHeader($relObj);
} else {
$value = $gridField->getDataFieldValue($item, $columnSource);
}
$value = str_replace(array("\r", "\n"), "\n", $value);
$columnData[] = '"' . str_replace('"', '\"', $value) . '"';
}
$fileData .= implode($separator, $columnData);
$fileData .= "\n";
$item->destroy();
}
return $fileData;
}
}
Thanks for this!
I had to use this for Members GF in Security Admin.
Created an extension for anyone interested.
class SecurityAdminExtension extends Extension{
function updateEditForm($form){
$gf = $form->Fields()->fieldByName('Root.Users.Members');
$gfConfig = $gf->getConfig();
$gfConfig->removeComponentsByType('GridFieldExportButton');
$gfConfig->addComponent(new GridFieldExportAllButton());
}
}
I while back, I created a little plugin to make it easy to export DataObjects to CSV or Excel files.
https://github.com/firebrandhq/excel-export
It comes with a button you can add to a grid field.
It's got a dependency on PHP-Excel.