So first I'll say the plugin works fine, returns correct rates and all that perfectly. What I am attempting to do is send to paypal a custom attribute that displays which UPS service that was selected (ground, 2nd day air, 3rd day, etc).
I know the custom attribute is reaching paypal correctly because I tested the code as follows:
$paypal_args = array(
'CUSTOM' => "THIS IS MY SHIPPING"
);
And it creates the "CUSTOM" field and displays the text on my paypal dashboard when I'm looking at the order details.
I've tried the following with no success:
'CUSTOM' => $order -> method_title,
&
'CUSTOM' =>$order -> shipping_method_title,
I feel like it's should look something like this:
$order -> (whatever variable I'm missing)
I'm adding the solution to my problem here just in case anyone stumbles across this issue.
So from the beginning the code was entered in to the following file on the woothemes UPS shipping method plugin: class-wc-paypal-advanced.php
I added the following line: 'CUSTOM' => $order ->get_shipping_method(), and what it does now is it sends a "CUSTOM" attribute to paypal which will read "ups ground" or "2nd day air" in my paypal transaction details.
Related
I am using wordpress to build a website that uses Stripe as its payment gateway.
Through the Paid Membership Pro plugin I have configured my Stripe connection.
The issue I am faced with is that I need to append a custom field, in this case a unique identifier for the customer, to the payment information for it to be available to send via my webhook to my server for processing.
I have come across a load of answers but none seem to be doing exactly what I am looking for.
I noticed in the PMPro plugin it allows you to add User Fields which I presumed would be a way to add custom data to the information but after checking the JSON payloads in Stripe none of the user field information is available.
I then tried adding the code from this answer to my functions.php file in word press just using a test meta key but again this information was not available in the payloads.
I am not using woocommerce.
How can I achieve this?
So I was able to crack this using a php script that intercepts the Stripe checkout event.
I added this as a snippet to my Wordpress site and to read in a custom field from the post submit.
Stripe allows you to add custom filed data to the metadata block and once populated this was successfully sent to my webhook.
function my_pmpro_stripe_params_send_user_id( $params, $order = null ) {
global $my_pmpro_checkout_order;
if ( empty( $params['metadata'] ) ) {
$params['metadata'] = array();
}
if ( empty ( $order ) ) {
// Save order while creating payment intent.
$order = $my_pmpro_checkout_order;
} else {
// Use saved order while creating subscription.
$my_pmpro_checkout_order = $order;
}
$params['metadata']['my_custom_field'] = $_POST['my_custom_value'];
return $params;
}
add_filter( 'pmpro_stripe_payment_intent_params', 'my_pmpro_stripe_params_send_user_id', 10, 2 );
add_filter( 'pmpro_stripe_create_subscription_array', 'my_pmpro_stripe_params_send_user_id', 10, 1 );
How do I go about limiting some products to be shipped locally only (a range of Zip codes)? P.S: Other products may ship locally and/or country-wide.
A scenario of this situation would be:
the user selects a product, adds it to cart, checks out, enters the shipping address, then the system would notify him that this item is only available for shipping locally.
I checked all the settings on Woocommerce but nothing worked.
Try looking for a plugin:
https://woocommerce.com/product-category/woocommerce-extensions/shipping-methods/delivery-shipping-options/
If that doesn't work, you can code it:
You need to flag some products as 'local delivery only', but not others. That way at checkout time you can display the appropriate shipping options. You'll also need to know the user's Zip code etc.
Step 1: Add the flag to the product.
Create a check box on the product that allows you to flag it's a local ship item.
The code below will add the option in Products -> Product data -> Advanced
add_action('woocommerce_product_options_advanced', 'localship_options');
function localship_options()
{
echo '<div class="options_group">';
woocommerce_wp_checkbox(array(
'id' => 'localship',
'value' => get_post_meta(get_the_ID(), 'localship', true),
'label' => 'Local Ship',
'desc_tip' => true,
'description' => 'Only ships locally',
));
echo '</div>';
}
add_action('woocommerce_process_product_meta', 'localship_save_fields', 10, 2);
function localship_save_fields($id, $post)
{
if (!empty($_POST['localship'])) {
update_post_meta($id, 'localship', $_POST['localship']);
} else {
delete_post_meta($id, 'localship');
}
}
To see if a product is flagged or not, use get_post_meta. You'll need the id of the
product
$is_localship = get_post_meta( /*product id here */, 'localship', true);
Now you can use $is_localship to see if the product is a ship local.
Step 2: Check the Buyer's Zip Code
You'll need to get the user's order information.
The wc_get_order function should do it:
$order = wc_get_order( $order_id )
From there you should be able to pull out the user's address, zip code etc.
See if the buyer's zip code is in an array of local Zip codes.
Step 3: Display or Hide Shipping Accordingly
Now that you know if the product is local ship only and the Zip code is a match, you can show or hide the shipping option in your template.
You can use the $is_localship to display or hide the shipping box in your template.
That should get you going in the right direction.
Maybe I overlooked it, but as much as I searched, I could not find which action to hook when a subscription changes price or frequency in Woocommerce Subscriptions.
The documentation says that to support price changes in your payment gateway you have to list subscription_amount_changes, but nowhere it says which function will be called when the amount actually changes..
Also in the Action reference I was unable to find an action hook which is called when the subscription amount or frequency changes. If anyone has a clue which hook to use or how to implement this particular feature, please tell me.
Edit
Ok thanks for the comments and answer by #Reigel, so if I understand correctly the change of a subscription in the admin menu (which I indeed refer to), has to be handled by the save_post action. Could you provide a small example how to hook this action and check if it is a subscription and get the $order_id (I guess this is the same as post_id?) to use in the subscription management calls?
Thank you very much already!
This should be considered an addon to the answer by #Reigel. If you upvote this, upvote his answer too.
Here's an example of hooking the pre_post_update action. It occurs a little before the save_post action. Both actions are triggered in the wp_insert_post() function in post.php.
function post_save_subscription_check( $post_ID, $data )
{
if( $data['post_type'] == 'product' ) {
if (!empty($_POST['_subscription_price']) && get_post_meta($post_ID, '_subscription_price', true) != $_POST['_subscription_price']) {
/* do stuff here */
}
if (!empty($_POST['_subscription_period']) && get_post_meta($post_ID, '_subscription_period', true) != $_POST['_subscription_period']) {
/* do stuff here */
}
}
}
add_action('pre_post_update', 'post_save_subscription_check', 10, 2 );
In the logic we are checking for the old value, obtained with get_post_meta() and the new value, held in the $_POST variable and comparing them.
This code only executes when a post updates, not for a new post
The code gets placed in your theme functions.php or custom plugin code.
In live code I would recommend cleaning any $_POST data before using it. I haven't bothered here.
I will try to explain about supports.
subscription_amount_changes is just a support and will not fire anything. You can use it for conditional statements, like:
if ( !$chosen_gateway->supports( 'subscription_amount_changes' )) {
echo 'Please be considerate and do not change the price for the chosen gateway does not support it.';
}
now, other plugins can then check if the chosen gateway supports subscription_amount_changes and do their appropriate actions.
action hook which is called when the subscription amount or frequency
changes
subscription is just a product type. Which means this is just a post with a post_type of product. The amount and frequency are just post meta. all are handled on save_post action. add_action( 'save_post', __CLASS__ . '::save_subscription_meta', 11 );. This is on the post_type=product. You have to check also for save_post on post_type=shop_order as it's more appropriate for checking support. Because there's already a gateway chosen.
I have a problem that I am not sure how to fix.
I have a formstack form on my wordpress site. It allows people to buy a product. For this
particular page, there is only one item available for sale, so once the
form is processed (data sent to Formstack->Stripe and payment confirmation received),
the page needs to "turn off" so others can't purchase the item.
Before I used formstack, I used gravity forms plugin.
When the form submitted, I had a add_action filter in my functions.php in
wordpress. It fired when gravity forms completed the submission process and
ran a function that turned off the page.
Here is that code that worked beautifully.
add_action("gform_after_submission", "set_post_content", 10, 2);
function set_post_content(){
global $cfs;
$field_data = array('sold' => '1');
$post_data = array('ID' => get_the_ID()); // the ID is required
$cfs->save($field_data, $post_data);
}
I see formstack has a webhook function.
I cannot seem to find code on how to parse the data the formstack webhook sends. I used http://requestb.in/ to see the data that was being sent.
I believe I need to use php://input, but once I read the data, I am not sure how to say
If formstack webhook fires, then run this code
function set_post_content(){
global $cfs;
$field_data = array('sold' => '1');
$post_data = array('ID' => get_the_ID()); // the ID is required
$cfs->save($field_data, $post_data);
}
I also believe the code needs to be in the functions.php in my theme file because otherwise it won't understand the $cfs variable that is from another wordpress plugin.
I saw this new formstack function the other day - it sounds like it could solve your problem?http://support.formstack.com/customer/portal/articles/1444519-event-fields
Event handling options
When your Event has filled to capacity, you can mark the Event as "Sold Out" so new submitters will see the item is no longer available. You may also choose to hide this field on the Form once the items have sold out. Additionally, you can deactivate the whole Form when the Event has sold out; this is done through the Form Settings tab > General > Deactivate Form settings.
I have added functionality to Payment method step, which is related to shipping address, if shipping address changes, then Payment method HTML content must be reloaded. I tried to add JavaScript to Billing and Shipping step, but my backend requires that Shipping address is saved to work. So If I add my own JavaScript after onclick="shipping.save();" in shipping.phtml, it is never called.
Is there way to refresh step HTML every time step is viewed?
Yes it's possible but not with pure JS (there is not ajax callbacks available to reload a step by a single click)
You'll need to overload the OnepageController from Mage_Checkout. In this class you'll see that each step saveAction contains the logic to refresh/display the next step.
For example, in the saveShippingAction() of Mage_Checkout_OnepageController you can see :
[...]
if (!isset($result['error'])) {
$result['goto_section'] = 'shipping_method';
$result['update_section'] = array(
'name' => 'shipping-method',
'html' => $this->_getShippingMethodsHtml()
);
}
[...]
The part update_section is the one to tell magento to force-refresh the step.
You should be able to adapt this to your needs.