Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Buy Now button with Variations #304

Open
wants to merge 6 commits into from

4 participants

@1bigidea

This is a long standing problem. When the Buy Now button is output (a standalone form), it doesn't include any variation information. As a result, any changes in the variation are ignored. Worse, when you press the Buy Now button, it submits to Paypal with the value of the parent product including whatever price is stored on the product record.

This will require changes to:

wp-e-commerce.js
when updating on change in selected variance, update the
corresponding Buy Now button

display.functions.php -
include a hidden field in the Buy Now form to pass the variation
value(s) to the Add_to_cart

@1bigidea 1bigidea Buy Now button with Variations
wp-e-commerce.js
    when updating on change in selected variance, update the
corresponding Buy Now button

display.functions.php -
    include a hidden field in the Buy Now form to pass the variation
value(s) to the Add_to_cart
a53b53e
wpsc-includes/display.functions.php
((8 lines not shown))
$src = _x( 'https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif', 'PayPal Buy Now Button', 'wpsc' );
$src = apply_filters( 'wpsc_buy_now_button_src', $src );
$classes = "wpsc-buy-now-form wpsc-buy-now-form-{$product_id}";
- $button_html = '<input class="wpsc-buy-now-button wpsc-buy-now-button-' . esc_attr( $product_id ) . '" type="image" name="submit" border="0" src=' . esc_url( $src ) . ' alt="' . esc_attr( 'PayPal - The safer, easier way to pay online', 'wpsc' ) . '" />';
+ $button_html = sprintf('<input %s class="wpsc-buy-now-button wpsc-buy-now-button-%s" type="image" name="submit" border="0" src="%s" alt="%s" />',
+ $disabled,
+ esc_attr( $product_id ),
+ esc_url( $src ),
+ esc_attr( 'PayPal - The safer, easier way to pay online', 'wpsc' )
@JustinSainton Owner

This should be esc_attr__(), since it's being translated.

I presume that you are talking about $disabled

@JustinSainton Owner

Nope - talking about this - esc_attr( 'PayPal - The safer, easier way to pay online', 'wpsc' ) - that should be esc_attr__( 'PayPal - The safer, easier way to pay online', 'wpsc' )

And now that you mention it, we should be using the disabled() function for the $disabled bit.

So I'd remove the chunk above (L38-L-40), and replace the $disabled var on L46 with disabled( $has_variants, wpsc_product_has_variations( $product_id ), false ) - also, the code there is also assigning, not comparing.

http://codex.wordpress.org/Function_Reference/disabled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
1bigidea added some commits
@1bigidea 1bigidea Escape the things
We need to escape the "disabled" attribute. And apparently the Paypal
string has never used the right i18n function
1b2ec55
@1bigidea 1bigidea Use Disabled function, clean up code
Cleaner code
eebb411
wpsc-core/js/wp-e-commerce.js
@@ -320,6 +321,9 @@ jQuery(document).ready(function ($) {
}
}
donation_price.val(response.numeric_price);
+
+ buynow.find('input[name="'+$(self).prop('name')+'"]').val($(self).val());
@meloniq Collaborator
meloniq added a note

Shouldn't this be jQuery and not dollar sign? Whole code above uses that way...

No difference except stylistically - but a simple enough change since everything else uses jQuery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@1bigidea 1bigidea Change $ to jQuery for consistency
Since all other jQuery selectors using the jQuery (rather than $
shortcut), the two references to self have modified
cafac86
@JustinSainton

Yeah, $/jQuery doesn't matter at all. This entire file is slated to be refactored in the near future anyway, at which point we'll be properly using the $ namespace alias.

wpsc-includes/display.functions.php
@@ -38,12 +38,26 @@ function wpsc_buy_now_button( $product_id, $replaced_shortcode = false ) {
$src = _x( 'https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif', 'PayPal Buy Now Button', 'wpsc' );
$src = apply_filters( 'wpsc_buy_now_button_src', $src );
$classes = "wpsc-buy-now-form wpsc-buy-now-form-{$product_id}";
- $button_html = '<input class="wpsc-buy-now-button wpsc-buy-now-button-' . esc_attr( $product_id ) . '" type="image" name="submit" border="0" src=' . esc_url( $src ) . ' alt="' . esc_attr( 'PayPal - The safer, easier way to pay online', 'wpsc' ) . '" />';
+ $button_html = sprintf('<input %s class="wpsc-buy-now-button wpsc-buy-now-button-%s" type="image" name="submit" border="0" src="%s" alt="%s" />',
+ disabled(wpsc_product_has_variations($product_id), true, false),
@JustinSainton Owner

Sorry to keep nit-picking! A couple things -

1) Since we're calling this wpsc_product_has_variations() function below on L53, too, we should definitely assign it to the $has_variants var. That way, we're not calling it twice. The performance is mitigated a bit by the static caching within the function - but I'd still reason it's worth only calling once.
2) Watch the whitespace :) Anywhere we're updating or adding new code, we should be following WP coding conventions. So here, we'd want disabled( $has_variants [...] ).
3) Speaking of whitespace, when disabled() returns "disabled='disabled'", it does so with the preceding space (See http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/general-template.php#L2278). So you could have '<input%s class [...]' - that way, we don't have the somewhat annoying phantom spaces in the HTML

@JustinSainton Owner

No criticism intended - just a few small tweaks and we're there! And what was done before e.g. my if( $has_variants = wpsc……) wasn't quite right - it was checking if ( [...] ) on an assignment ($x = [...] ) - rather than a comparison.

I do see, from time to time, some clever coding (usually with transients, probably because of the codex) to this effect -

if ( false !== ( $x = func() ) ) {

}

I can get behind that when readability isn't sacrificed, but that's not quite what was happening there.

@JustinSainton Owner

WRT disabled() - that makes sense. Looking through core, however, there's actually no instance anywhere of disabled() (or the other helper functions) being run through sprintf() or printf(). There is, however (especially with checked()), precedence for putting it directly next to the attribute.

So with that in mind, what I'd actually suggest is removing the placeholder for disabled() and putting that directly in the variable - next to wherever you want it, sans extra space. I think that keeps up with your note of maintainability/readability. And it eliminates the extra space.

That work for you?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@JustinSainton

Slating this for 3.8.12., @1bigidea, can you rebase?

@instinct
Owner
@JustinSainton

Related: #422.

@1bigidea
@JustinSainton

Punting to a future release. Would be awesome to fix it though.

@JustinSainton JustinSainton modified the milestone: Future Release, 3.8.14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 21, 2013
  1. @1bigidea

    Buy Now button with Variations

    1bigidea authored
    wp-e-commerce.js
        when updating on change in selected variance, update the
    corresponding Buy Now button
    
    display.functions.php -
        include a hidden field in the Buy Now form to pass the variation
    value(s) to the Add_to_cart
Commits on Mar 22, 2013
  1. @1bigidea

    Escape the things

    1bigidea authored
    We need to escape the "disabled" attribute. And apparently the Paypal
    string has never used the right i18n function
  2. @1bigidea

    Use Disabled function, clean up code

    1bigidea authored
    Cleaner code
  3. @1bigidea

    Change $ to jQuery for consistency

    1bigidea authored
    Since all other jQuery selectors using the jQuery (rather than $
    shortcut), the two references to self have modified
Commits on Mar 23, 2013
  1. @1bigidea

    Reduce the calls to wpsc_product_has_variations

    1bigidea authored
    minor whitespace corrections
  2. @1bigidea

    Include disabled() in the input string directly

    1bigidea authored
    rather than via sprintf
This page is out of date. Refresh to see the latest.
View
6 wpsc-core/js/wp-e-commerce.js
@@ -280,6 +280,7 @@ jQuery(document).ready(function ($) {
// update the price when the variations are altered.
jQuery(".wpsc_select_variation").live('change', function() {
jQuery('option[value="0"]', this).attr('disabled', 'disabled');
+ self = this;
var parent_form = jQuery(this).closest("form.product_form");
if ( parent_form.length == 0 )
return;
@@ -292,7 +293,7 @@ jQuery(document).ready(function ($) {
donation_price = jQuery('input#donation_price_' + prod_id),
old_price = jQuery('#old_product_price_' + prod_id),
save = jQuery('#yousave_' + prod_id),
- buynow = jQuery('#BB_BuyButtonForm' + prod_id);
+ buynow = jQuery('#buy-now-product_' + prod_id);
if ( response.variation_found ) {
if ( response.stock_available ) {
stock_display.removeClass('out_of_stock').addClass('in_stock');
@@ -320,6 +321,9 @@ jQuery(document).ready(function ($) {
}
}
donation_price.val(response.numeric_price);
+
+ buynow.find('input[name="'+jQuery(self).prop('name')+'"]').val(jQuery(self).val());
+ buynow.find('input.wpsc-buy-now-button').prop('disabled', false);
}
}, 'json');
});
View
26 wpsc-includes/display.functions.php
@@ -35,15 +35,29 @@ function wpsc_buy_now_button( $product_id, $replaced_shortcode = false ) {
$handling = $shipping;
}
+ $has_variants = wpsc_product_has_variations($product_id);
$src = _x( 'https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif', 'PayPal Buy Now Button', 'wpsc' );
$src = apply_filters( 'wpsc_buy_now_button_src', $src );
$classes = "wpsc-buy-now-form wpsc-buy-now-form-{$product_id}";
- $button_html = '<input class="wpsc-buy-now-button wpsc-buy-now-button-' . esc_attr( $product_id ) . '" type="image" name="submit" border="0" src=' . esc_url( $src ) . ' alt="' . esc_attr( 'PayPal - The safer, easier way to pay online', 'wpsc' ) . '" />';
+ $button_html = sprintf('<input'.disabled( $has_variants, true, false ).' class="wpsc-buy-now-button wpsc-buy-now-button-%s" type="image" name="submit" border="0" src="%s" alt="%s" />',
+ esc_attr( $product_id ),
+ esc_url( $src ),
+ esc_attr__( 'PayPal - The safer, easier way to pay online', 'wpsc' )
+ );
$button_html = apply_filters( 'wpsc_buy_now_button_html', $button_html, $product_id );
- ?>
- <form class="<?php echo esc_attr( $classes ); ?>" target="paypal" action="<?php echo esc_url( home_url() ); ?>" method="post">
+?>
+ <form class="<?php echo esc_attr( $classes ); ?>" id="buy-now-product_<?php echo $product_id; ?>"target="paypal" action="<?php echo esc_url( home_url() ); ?>" method="post">
<input type="hidden" name="wpsc_buy_now_callback" value="1" />
<input type="hidden" name="product_id" value="<?php echo esc_attr( $product_id ); ?>" />
+<?php
+ if( $has_variants ):
+ // grab the variation form fields here
+ $wpsc_variations = new wpsc_variations( $product_id );
+ while ( wpsc_have_variation_groups() ) : wpsc_the_variation_group();
+ printf('<input type="hidden" class="variation-value" name="variation[%s]" id="%s" value="0"/>', wpsc_vargrp_id(), wpsc_vargrp_form_id());
+ endwhile;
+ endif; /* END wpsc_product_has_variations */
+?>
<?php if ( get_option( 'multi_add' ) ): ?>
<label for="quantity"><?php esc_html_e( 'Quantity', 'wpsc' ); ?></label>
<input type="text" size="4" id="quantity" name="quantity" value="" /><br />
@@ -70,20 +84,20 @@ function wpsc_also_bought( $product_id ) {
if ( get_option( 'wpsc_also_bought' ) == 0 ) {
return '';
}
-
+
// To be made customiseable in a future release
$also_bought_limit = 3;
$element_widths = 96;
$image_display_height = 96;
$image_display_width = 96;
-
+
// Filter will be used by a plugin to provide 'Also Bought' functionality when this is deprecated from core.
// Filter is currently private and should not be used by plugin/theme devs as it may only be temporary.
$output = apply_filters( '_wpsc_also_bought', '', $product_id );
if ( ! empty( $output ) ) {
return $output;
}
-
+
// If above filter returns output then the following is ignore and can be deprecated in future.
$also_bought = $wpdb->get_results( $wpdb->prepare( "SELECT `" . $wpdb->posts . "`.* FROM `" . WPSC_TABLE_ALSO_BOUGHT . "`, `" . $wpdb->posts . "` WHERE `selected_product`= %d AND `" . WPSC_TABLE_ALSO_BOUGHT . "`.`associated_product` = `" . $wpdb->posts . "`.`id` AND `" . $wpdb->posts . "`.`post_status` IN('publish','protected') ORDER BY `" . WPSC_TABLE_ALSO_BOUGHT . "`.`quantity` DESC LIMIT $also_bought_limit", $product_id ), ARRAY_A );
if ( is_array( $also_bought ) && count( $also_bought ) > 0 ) {
Something went wrong with that request. Please try again.