Im using Woo estimated shipping date plugin to add a custom text (field) in my products pages.
The problem is:
Example: If I use this text in that custom field: "In stock" and then I save the changes, the plugin show "In stock" in the product page...BUT if I go back to edit anything else on the product page, the plugin restore the default sentence "Estimated Delivery Date" and if I save the changes without modifying that sentences once more, the "Estimated Delivery Date" shows in the product page instead the new sentence. Its something like an annoying loop, after save the changes always back the default text and If I don't look that field my custom text is gone.
Here's the Plugin code:
<?php
/**
* No cheating please
*/
if ( ! defined( 'WPINC' ) ) exit;
/**
* WCESD_Product_Settings Class
*/
class WCESD_Product_Settings {
/**
* Hold the instance
*
* #var string
*/
private static $instance;
use helperMethods;
/**
* Constructor method
*
* #return void
*/
public function __construct() {
if ( ! $this->enabled() ) {
return;
}
$this->init_hooks();
}
/**
* Init all the hooks
*
* #return void
*/
protected function init_hooks() {
add_action( 'woocommerce_product_options_shipping', array( $this, 'wc_esd_add_estimated_shipping_date' ) );
add_action( 'woocommerce_process_product_meta', array( $this, 'wc_esd_save_shipping_date') );
}
/**
* Add wcesd form
*
* #return void
*/
public function wc_esd_add_estimated_shipping_date() {
woocommerce_wp_checkbox( array(
'id' => 'wc_esd_date_enable',
'label' => __( 'Habilitar fecha estimada entrega', 'wcesd' ),
'description' => __( 'Enable or Disable woocommerce estimated shipping date', 'wcesd' ),
'desc_tip' => true,
) );
woocommerce_wp_text_input( array(
'id' => 'wc_esd_date',
'label' => __( 'Fecha estimada en días', 'wcesd' ),
'description' => __( 'Días del posible arribo del producto', 'wcesd' ),
'desc_tip' => true,
'type' => 'number',
'placeholder' => 5,
'value' => 5,
) );
woocommerce_wp_text_input( array(
'id' => 'wc_esd_date_message',
'label' => __( 'Frase', 'wcesd' ),
'description' => __( 'Agregue su mensaje', 'wcesd' ),
'desc_tip' => true,
'placeholder' => 'Estimated Delivery Date',
'value' => 'Estimated Delivery Date',
) );
do_action( 'wc_esd_add_estimated_shipping_date' );
}
/**
* Save wcesd form data
*
* #param int $product_id
*
* #return void
*/
public function wc_esd_save_shipping_date( $product_id ) {
if ( ! is_admin() || get_post_type() !== 'product' ) {
return;
}
$wc_esd_date_enable = isset( $_POST['wc_esd_date_enable'] ) ? sanitize_text_field( $_POST['wc_esd_date_enable'] ) : '';
$wc_esd_date = isset( $_POST['wc_esd_date'] ) ? sanitize_text_field( $_POST['wc_esd_date'] ) : '';
$wc_esd_date_message = isset( $_POST['wc_esd_date_message'] ) ? sanitize_text_field( $_POST['wc_esd_date_message'] ) : '';
update_post_meta( $product_id, 'wc_esd_date_enable', $wc_esd_date_enable );
update_post_meta( $product_id, 'wc_esd_date', $wc_esd_date );
update_post_meta( $product_id, 'wc_esd_date_message', $wc_esd_date_message );
do_action( 'wc_esd_save_shipping_date', $product_id );
}
/**
* Get instance
*
* #return object
*/
public static function init() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Disable cloning this class
*
* #return void
*/
private function __clone() {
//
}
private function __wakeup() {
//
}
}
WCESD_Product_Settings::init();
Any idea how to solve this problem?
Thank you :)
Please remove the value field
woocommerce_wp_text_input( array(
'id' => 'wc_esd_date_message',
'label' => __( 'Frase', 'wcesd' ),
'description' => __( 'Agregue su mensaje', 'wcesd' ),
'desc_tip' => true,
'placeholder' => 'Estimated Delivery Date',
/*'value' => 'Estimated Delivery Date',*/
) );
Then it should work.
Related
I really hope someone can help me, cause i'm running out of ideas.
I made this custom shipping methon on Woocommerce, copying and pasting from the website documentation and other StakO topics.
Everything seems to be okay, apart of when i try to update my only one field called "Store Address", it doesn't works and values are not updated.
Here's the code. Thanks to everyone
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function pickupinstore_shipping_method() {
if ( ! class_exists( 'PickupInStore_Shipping_Method' ) ) {
class PickupInStore_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* #access public
* #return void
*/
public function __construct( $instance_id = 0 ) {
$this->id = 'pickupinstore';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Pickup in Store', 'pickupinstore' );
$this->method_description = __( 'Custom Shipping Method - Pickup in Store', 'pickupinstore' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->init();
$this->title = isset( $this->settings['title'] ) ? "Ritiro in Negozio | ".$this->settings['title'].": GRATIS" : __( 'Pickup in Store', 'pickupinstore' ); }
/**
* Init your settings
*
* #access public
* #return void
*/
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Define settings field for this shipping
* #return void
*/
function init_form_fields() {
$this->form_fields = array(
'title' => array(
'title' => __( 'Ritiro in Negozio', 'pickupinstore' ),
'type' => 'text',
'description' => __( 'Store Address', 'pickupinstore' ),
'default' => __( '197, Brooklyn Road', 'New York' ),
'desc_tip' => true,
),
);
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* #access public
* #param mixed $package
* #return void
*/
public function calculate_shipping( $package = array() ) {
$cost = 0;
$this->add_rate( array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
) );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'pickupinstore_shipping_method' );
function add_pickupinstore_shipping_method( $methods ) {
$methods['pickupinstore'] = 'PickupInStore_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_pickupinstore_shipping_method' );
}
Edit:
I tried using #docker code, but when I save changes nothing compares in the title section (backend). Otherwise the form field seems to be saved, but as you can see, not diplayed.
In front end, it displays the name of the custom shipping method instead of the title I save.
Move the $this->title definition to the init() method and use $this->get_option() instead of $this->settings.
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
$this->title = null != $this->get_option('title') ? "Ritiro in Negozio | " . $this->get_option('title') . ": GRATIS" : __( 'Pickup in Store', 'pickupinstore' );
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
Additionally in the init_form_fields() method you should use $this->instance_form_fields.
function init_form_fields() {
$this->instance_form_fields = array(
'title' => array(
'title' => __( 'Ritiro in Negozio', 'pickupinstore' ),
'type' => 'text',
'description' => __( 'Store Address', 'pickupinstore' ),
'default' => __( '197, Brooklyn Road', 'New York' ),
'desc_tip' => true,
),
);
}
In some cases you may need the get_instance_from_fields() method but I have a working solution without it:
/**
* Get setting form fields for instances of this shipping method within zones.
*
* #return array
*/
public function get_instance_form_fields() {
return parent::get_instance_form_fields();
}
EDIT:
This is the full working code:
<?php
/**
* Plugin Name: Pickup in Store
* Plugin URI: https://www.perspectiveweb.it/
* Description: Pickup in store - Custom Shipping Method
* Version: 1.0.0
* Author: Perspective Web
* Author URI: https://www.perspectiveweb.it/
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Domain Path: /lang
* Text Domain: perspectiveweb
*/
if ( ! defined( 'WPINC' ) ) {
die;
}
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
function pickupinstore_shipping_method() {
if ( ! class_exists( 'PickupInStore_Shipping_Method' ) ) {
class PickupInStore_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* #access public
* #return void
*/
public function __construct( $instance_id = 0 ) {
$this->id = 'pickupinstore';
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Pickup in Store', 'pickupinstore' );
$this->method_description = __( 'Custom Shipping Method - Pickup in Store', 'pickupinstore' );
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->init();
}
/**
* Init your settings
*
* #access public
* #return void
*/
public function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
$this->title = null != $this->get_option('title') ? "Ritiro in Negozio | " . $this->get_option('title') . ": GRATIS" : __( 'Pickup in Store', 'pickupinstore' );
// Save settings in admin if you have any defined
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Define settings field for this shipping
* #return void
*/
public function init_form_fields() {
$this->instance_form_fields = array(
'title' => array(
'title' => __( 'Ritiro in Negozio', 'pickupinstore' ),
'type' => 'text',
'description' => __( 'Store Address', 'pickupinstore' ),
'default' => __( '197, Brooklyn Road', 'New York' ),
'desc_tip' => true,
),
);
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* #access public
* #param mixed $package
* #return void
*/
public function calculate_shipping( $package = array() ) {
$cost = 0;
$this->add_rate( array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
) );
}
}
}
}
add_action( 'woocommerce_shipping_init', 'pickupinstore_shipping_method' );
function add_pickupinstore_shipping_method( $methods ) {
$methods['pickupinstore'] = 'PickupInStore_Shipping_Method';
return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_pickupinstore_shipping_method' );
}
I have created woocommerce plugin which gets shipping rates from third party URL(my web site) and displays it on the checkout page. when I click on one of shipping method it also calculates correct shipping rates, but when I again refresh checkout page my selected shipping option gets lost and I have to select again. also, my custom shipping option does not reach to order after a successful order.
In below Screenshot, I pass Suburb and Postcode, and I get all rates i desire, when i click on one of shipping method it also calculates correct a shipping rates
After Click on Place Order, it takes first shipping method which is a flat rate: $5, please see below screenshot
Here is my Plugin please have a look and guide me where I am going wrong.
<?php
/**
* Plugin Name: Iconsignit Shipping
* Plugin URI: http://Iconsignit.com.au
* Description: Iconsignit Shipping Method for WooCommerce
* Version: 1.0.0
* Author: Jaimin prajapati
* Author URI: http://www.webbrainstechnologies.com
* License: GPL-3.0+
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
* Domain Path: /
* Text Domain: Iconsignit
*/
if (!defined('WPINC')) {
die;
}
/*
* Check if WooCommerce is active
*/
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
function iconsignit_shipping_method()
{
if (!class_exists('Iconsignit_Shipping_Method')) {
class Iconsignit_Shipping_Method extends WC_Shipping_Method
{
/**
* Constructor for your shipping class
*
* #access public
* #return void
*/
public function __construct($instance_id = 0)
{
$this->id = 'iconsignit';
$this->instance_id = absint($instance_id);
$this->method_title = __('Iconsignit Shipping', 'iconsignit');
$this->method_description = __('Custom Shipping Method for iconsignit', 'iconsignit');
$this->init();
$this->enabled = isset($this->settings['enabled']) ? $this->settings['enabled'] : 'yes';
$this->title = isset($this->settings['title']) ? $this->settings['title'] : __('Iconsignit Shipping', 'iconsignit');
}
/**
* Init your settings
*
* #access public
* #return void
*/
function init()
{
// Load the settings API
$this->init_form_fields();
$this->init_settings();
// Save settings in admin if you have any defined
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
}
/**
* Define settings field for this shipping
* #return void
*/
function init_form_fields()
{
// We will add our settings here
$this->form_fields = array(
'ApiToken' => array(
'title' => __('Api Token', 'iconsignit-integration-demo'),
'type' => 'text',
'description' => __('Enter with your API Key. You can find this in "User Profile" drop-down (top right corner) > API Keys.', 'iconsignit-integration-demo'),
'desc_tip' => true,
'default' => '',
),
'ApiUrl' => array(
'title' => __('Api Url', 'iconsignit-integration-demo'),
'type' => 'text',
'default' => '',
'desc_tip' => true,
'description' => __('Website URL', 'iconsignit-integration-demo'),
),
'ConnectIconsignUrl' => array(
'title' => __('Iconsignit Url', 'iconsignit-integration-demo'),
'type' => 'text',
'default' => '',
'desc_tip' => true,
'description' => __('Url from where all shipping rates will come', 'iconsignit-integration-demo'),
),
);
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* #access public
* #param mixed $package
* #return void
*/
public function calculate_shipping($package = array())
{
// We will add the cost, rate and logics in here
$item = array();
$count = 0;
foreach ($package['contents'] as $item_id => $values) {
$item[$count]['item_qty'] = $values['quantity'];
$item[$count]['item_length'] = $values['data']->get_length();
$item[$count]['item_width'] = $values['data']->get_width();
$item[$count]['item_height'] = $values['data']->get_height();
$item[$count]['item_weight'] = $values['data']->get_weight();
$item[$count]['item_palletised'] = 0;
$count++;
}
$country = $_POST['s_country'];
$state = $_POST['s_state'];
$postcode = $_POST['s_postcode'];
$city = $_POST['s_city'];
$address = $_POST['s_address'];
$address_2 = $_POST['s_address_2'];
$isCredentials = get_option('woocommerce_iconsignit_settings');
$data = array('ApiUrl' => $isCredentials['ApiUrl'], 'ApiToken' => $isCredentials['ApiToken'], 'DeliveryTown' => $city, 'DeliveryPostcode' => $postcode, 'Items' => $item);
$isResponse = Requests::post($isCredentials['ConnectIconsignUrl'].'/api/getconsignrate', array(), $data);
$resp = json_decode($isResponse->body, true);
$counter = 1;
foreach ($resp['result'] as $key => $res) {
$rate = array(
'id' => $res['QuoteRateID'],//$this->id,
'label' => $res['carrier_nm'] . "-(" . $res['service_nm'] . ")",
'cost' => $res['total_charge'],
'calc_tax' => 'per_item',
);
$this->add_rate($rate);
$counter++;
}
}
}
}
}
add_action('woocommerce_shipping_init', 'iconsignit_shipping_method');
function add_iconsignit_shipping_method($methods)
{
$methods[] = 'Iconsignit_Shipping_Method';
return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_iconsignit_shipping_method');
}
problem is solved the problem was in $this->id, it should have to be unique.
Please consider the following php class extends the WP_REST_Controller class of Wordpress related to Rest API:
<?php
class MCQAcademy_Endpoint extends WP_REST_Controller {
/**
* Register the routes for the objects of the controller.
*/
public function register_routes() {
$version = '1';
$namespace = 'custompath/v' . $version;
$base = 'endpointbase';
register_rest_route(
$namespace,
'/' . $base,
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'get_items' ),
'permission_callback' => array( $this, 'get_items_permissions_check' ),
'args' => array(),
)
)
);
}
/**
*
*/
public function get_items( $request ) {
$rs = array(
'data' => array(),
'request' => array(
'lang' => 'en',
),
);
$args = array();
$items = get_posts( $args );
foreach( $items as $item ) {
$itemdata = $this->prepare_item_for_response( $item, $request );
$rs['data'][] = $this->prepare_response_for_collection( $itemdata );
}
$rs['wp_get_current_user'] = wp_get_current_user(); // Does not output as expected
return new WP_REST_Response( $rs, 200 );
}
/**
* Check if a given request has access to get items
*/
public function get_items_permissions_check( $request ) {
return true; // to make readable by all
}
/**
* Prepare the item for create or update operation
*/
protected function prepare_item_for_database( $request ) {
return $request;
}
/**
* Prepare the item for the REST response
*/
public function prepare_item_for_response( $item, $request ) {
$data = array(
'ID' => $item->ID,
'post_content' => wpautop($item->post_content),
'post_title' => $item->post_title,
);
return $data;
}
/**
* Get the query params for collections
*/
public function get_collection_params() {
return array(
'page' => array(
'description' => 'Current page of the collection.',
'type' => 'integer',
'default' => 1,
'sanitize_callback' => 'absint',
),
'per_page' => array(
'description' => 'Maximum number of items to be returned in result set.',
'type' => 'integer',
'default' => 10,
'sanitize_callback' => 'absint',
),
'search' => array(
'description' => 'Limit results to those matching a string.',
'type' => 'string',
'sanitize_callback' => 'sanitize_text_field',
),
);
}
// Register our REST Server
public function hook_rest_server(){
add_action( 'rest_api_init', array( $this, 'register_routes' ) );
}
}
$myEndpoint = new MCQAcademy_Endpoint();
$myEndpoint->hook_rest_server();
Everything is going well except calling the wp_get_current_user() function in the get_items() function return empty user even though the user is loggedin in the website.
What is the solution to get the loggedin user info in Rest API endpoint function?
I have a custom order status called Shipping Details which will be selected after the Order complete trigger is fired.
Code for the Order Status
function register_shipping_details_status() {
register_post_status( 'wc-shipping-details', array(
'label' => 'Send Shipping Details',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true
//'label_count' => _n_noop( 'Shipping Details <span class="count">(%s)</span>', 'Shipping Details <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_shipping_details_status' );
// Add to list of WC Order statuses
function add_shipping_details_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after completed
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-completed' === $key ) {
$new_order_statuses['wc-shipping-details'] = 'Send Shipping Details';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_shipping_details_to_order_statuses' );
and my email trigger code is here.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! class_exists( 'WC_Shipping_Details_Email' ) ) :
/**
* Customer Shipping Details Email
*/
class WC_Shipping_Details_Email extends WC_Email {
/**
* Constructor.
*/
public function __construct() {
$this->id = 'wc_shipping_details';
$this->customer_email = true;
$this->title = __( 'Shipping Details', 'woocommerce' );
$this->description = __( 'Shipping details emails are sent after the order has been marked completed .', 'woocommerce' );
$this->heading = __( 'Shipping Details for your order', 'woocommerce' );
$this->subject = __( 'Shipping details for your order from INAI', 'woocommerce' );
$this->template_html = 'emails/customer-shipping-details.php';
$this->template_plain = 'emails/plain/customer-shipping-details.php';
// Triggers for this email
add_action( 'woocommerce_order_status_shipping_details_notification', array( $this, 'trigger' ) );
// Call parent constuctor
parent::__construct();
}
/**
* Trigger.
*
* #param int $order_id
*/
public function trigger( $order_id ) {
if ( $order_id ) {
$this->object = wc_get_order( $order_id );
$this->recipient = $this->object->billing_email;
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = date_i18n( wc_date_format(), strtotime( $this->object->order_date ) );
$this->replace['order-number'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {
return;
}
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
The action for registering the email
//custom hooks for custom woocommerce order email
function register_custom_order_status_action( $actions ){
$actions[] = 'woocommerce_order_status_shipping_details';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'register_custom_order_status_action' );
For some reason the mail is not getting triggered. I have looked around a lot and even found a few others for whom the problem has been solved. WooCommerce - send custom email on custom order status change.
My code is almost exactly the same, still don't know what i'm missing. Please help.
I am trying to make a Custom Gateway Plugin for my Merchant Account but I got stuck and I don't know how to continue.
Now, here is what I done so far:
<?php
/*
Plugin Name: Paysecure.ro Payment Gateway
Plugin URI: http://www.Paysecure.ro
Description: Allows you to use Paysecure.ro Payment Gateway with the WooCommerce plugin.
Version: 0.0.1
Author: Andrei Raileanu
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
add_action('plugins_loaded', 'woocommerce_Paysecure', 0);
function woocommerce_Paysecure(){
if (!class_exists('WC_Payment_Gateway'))
return; // if the WC payment gateway class is not available, do nothing
if(class_exists('WC_Paysecure'))
return;
class WC_Gateway_Paysecure extends WC_Payment_Gateway{
public function __construct(){
$plugin_dir = plugin_dir_url(__FILE__);
global $woocommerce;
$this->id = 'Paysecure';
$this->icon = apply_filters('woocommerce_Paysecure_icon', ''.$plugin_dir.'Paysecure.png');
$this->has_fields = true;
// Load the settings
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->title = "Credit/Debit Card";
$this->description = "You will be redirected to paysecure.ro to complete your purchase.";
$this->cui = "XXXXXXXXX";
$this->encryption_key = "XXXXXXXXX";
$this->currency = "USD";
// Logs
if ($this->debug == 'yes'){
$this->log = $woocommerce->logger();
}
// Actions
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
// Save options
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
// Payment listener/API hook
add_action('woocommerce_api_wc_' . $this->id, array($this, 'check_ipn_response'));
}
function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Paysecure', 'woocommerce' ),
'default' => 'yes'
)
);
}
public function admin_options() {
?>
<h3><?php _e( 'Paysecure', 'woocommerce' ); ?></h3>
<p><?php _e( 'Paysecure Payment Gateway', 'woocommerce' ); ?></p>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table>
<?php
}
function payment_fields() {
$plugin_dir = plugin_dir_url(__FILE__);
// Description of payment method from settings
if ($this->description) { ?>
<p><?php
echo $this->description; ?>
</p><?php
} ?>
<?php
}
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order($order_id);
// I WILL NEED THESE FIELDS
//$this->notify_url;
//$order->get_total();
//$order->get_order_number();
//$order->billing_first_name;
//$order->billing_email;
}
}
function add_Paysecure_gateway($methods){
$methods[] = 'WC_Gateway_Paysecure';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_Paysecure_gateway');
}
Here are the PHP instructions from my merchant account provider:
<?php
require_once('./functions.php');
$key = 'THIS IS THE ENCRYPTION KEY';
$data = array( 'cui' => 'THIS IS THE CUI CODE',
'AMOUNT' => '200',
'CURRENCY' => 'USD',
'ORDER' => '12313',
'DESC' => ps_clean('SHORT DESCRIPTION'),
'PENTRU' => 'EMAIL ADDRESS',
'TRTYPE' => 0,
'BACKREF' => 'http://www.site.ro/test.php',
'TIMESTAMP'=>time()
);
echo '<form action="https://paysecure.ro/order/cgi-bin/" method="post" >';
foreach ($data as $l => $v ) echo '<input size="55" readonly type="text" name="'.$l.'" value="'.$v.'" /> : <label for="'.$l.'">'.$l.'</label><br>';
echo '<input size="55" type="text" name="P_SIGN" readonly value="'.calculateSign($data,$key).'" /><br>' ; //se calculeaza P_SIGN - ul , encriptarea datelor
?>
<input type="submit" />
</form>
Can someone help me with the rest of the code?
1st mistake I noticed you made, $this->id MUST be in lowercase. 'Paysecure' is not gonna work here come time for the API hook.
// Payment listener/API hook
add_action('woocommerce_api_wc_' . $this->id, array($this, 'check_ipn_response'));
change to this instead:
// Payment listener/API hook
add_action('woocommerce_api_wc_paysecure', array($this, 'check_ipn_response'));
2nd mistake is that you forgot to create the function check_ipn_response()
// Handles the callbacks received from the payment backend. give this url to your payment processing comapny as the ipn response URL:
// USAGE: http://myurl.com/?wc-api=WC_Gateway_Paysecure
function check_ipn_response() {
$http_contents = file_get_contents("php://input");
$json = json_decode($http_contents, true);
$order = new WC_Order($json['custom']);
if ($callback_json['status'] == 'Transaction approved')
{
$order->payment_complete(); //This will ensure stock reductions are made, and the status is changed to the correct value.
$order->add_order_note( __( 'KiwiPay Transaction Approved.', 'kiwipay' ) );
}
else
{
$order->update_status('failed');
$order->add_order_note('KiwiPay status received - ' . $callback_json['status']);
}
//references:
//http://docs.woothemes.com/document/payment-gateway-api/
//https://github.com/Paymium/WooCommerce/blob/master/checkout.php
//https://github.com/tubiz/voguepay-woocommerce-payment-gateway/blob/master/voguepay-woocommerce-payment-gateway.php
/*
{
"id":1863,
"amount":1750,
"status":"Transaction approved",
"created":1434198776,
"reference":"2626", //this is the order number $order->get_order_number()
"name":"HamishTest Test",
"email":"hamish#tst.xom",
"address":{"street":"my rd","region":"N/A","city":"tauranga","zip":"3175","country":"NZ"},
"test":true,
"price":"17.50",
"custom":"6589" //this is the wc order_id passed during process_payment to the paymen processor.
}
*/
How about this?
add_action( 'plugins_loaded', 'init_gateway_cash' );
function init_gateway_cash() {
class WC_Gateway_Cash extends WC_Payment_Gateway {
function __construct() {
$this->id = "Cash Gateway";
$this->title = "Cash";
$this->description = "More later";
$this->init_form_fields();
$this->init_settings();
add_action( 'woocommerce_update_options_payment_gateways', array( &$this, 'process_admin_options' ) );
}
function init_form_fields(){
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable Cheque Payment', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Cheque Payment', 'woocommerce' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Customer Message', 'woocommerce' ),
'type' => 'textarea',
'default' => ''
)
);
}
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$productArray = array();
$x = 0;
foreach( $order->get_items() as $item_id => $item ) {
$productArray[$x] = $order->get_product_from_item( $item );
$x++;
}
// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold',
__( 'Awaiting cheque payment.', 'woocommerce' )
);
// Remove cart
$woocommerce->cart->empty_cart();
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
}
}
function add_gateway_cash_class ($methods ) {
$methods[] = 'WC_Gateway_Cash';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_gateway_cash_class' );