Last active
May 22, 2019 03:27
-
-
Save wpmudev-sls/e9ea55053d3a807b91a6b6cbadb7fbe7 to your computer and use it in GitHub Desktop.
MarketPress - Adds quantity field into [mp_list_products] shortcode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: MarketPress - [mp_list_products] quantity | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Description: Adds quantity field into [mp_list_products] shortcode | |
* Author: Panos Lyrakis / Konstantinos Xenos @ WPMUDEV | |
* Author URI: https://premium.wpmudev.org/ | |
* License: GPLv2 or later | |
*/ | |
function add_qty_product_list_page_js() { | |
?> | |
<script type="text/javascript"> | |
( function( $ ) { | |
$.ajaxSetup( { | |
beforeSend: function( jqXHR, settings ) { | |
if ( settings.data ) { | |
var product_id = getUrlParameter( 'product', settings.data ), | |
action = getUrlParameter( 'cart_action', settings.data ), | |
quantity = $( '#mp-buy-product-' + product_id + '-form .mp_form_input-qty' ).val(), | |
dataString = settings.data; | |
if ( typeof action === 'undefined' || 'add_item' != action || dataString.indexOf( 'product_attr' ) != -1 ) { | |
return true; | |
} | |
settings.data += '&qty=' + quantity; | |
return true; | |
} | |
} | |
}); | |
var getUrlParameter = function getUrlParameter( sParam, url ) { | |
var sURLVariables = url.split( '&' ), | |
sParameterName, | |
i; | |
for ( i = 0; i < sURLVariables.length; i++ ) { | |
sParameterName = sURLVariables[i].split( '=' ); | |
if (sParameterName[0] === sParam) { | |
return sParameterName[1] === undefined ? true : sParameterName[1]; | |
} | |
} | |
}; | |
} ) ( jQuery ); | |
</script> | |
<style> | |
#colorbox { | |
width: 500px !important; | |
} | |
#cboxWrapper, | |
#cboxContent { | |
width: 100% !important; | |
} | |
#cboxLoadedContent { | |
width: calc( 100% - 30px )!important; | |
} | |
.customqty { | |
float: left; | |
width: auto; | |
margin-right: 15px; | |
} | |
.customqty .mp_product_options_att_label { | |
display: none; | |
} | |
</style> | |
<?php | |
}; | |
function add_qty_product_list_page( $button, $product_id, $context, $selected_atts, $no_single ) { | |
if ( 'list' === $context ) { | |
$product = new MP_Product( $product_id ); | |
if ( ! $product->has_variations() ) { | |
$html = '<div class="mp_product_options_atts customqty"> | |
<div class="mp_product_options_att"> | |
<strong class="mp_product_options_att_label">' . __( 'Quantity', 'mp' ) . '</strong> | |
<div class="mp_form_field mp_product_options_att_field"> | |
' . $product->attribute_input_fields( true, false, false ) . ' | |
</div><!-- end mp_product_options_att_field --> | |
</div><!-- end mp_product_options_att --> | |
</div><!-- end mp_product_options_atts -->'; | |
$button = substr_replace( $button, $html, strpos( $button, '<input' ), 0 ); | |
} | |
} | |
add_action( 'wp_footer', 'add_qty_product_list_page_js', 999 ); | |
return $button; | |
} | |
add_filter( 'mp_buy_button_tag', 'add_qty_product_list_page', 10, 5 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment