trying to modify an e-commerce WP plugin

Hi–I am very new to php. I have installed the Marketpress plugin on my site: www.hickoryyhillheather.com
I love the plugin so far, but it offers one option for shipping–a flat rate for everything. My client needs to offer another rate, but fairly simple.
She sells plants online, with a minimum of 3 plants per order. She charges a flat rate (x) for each box of 3, so 3 plants costs x to ship and 6 plants costs 2x, etc
Can someone help me tweak the php to modify this?
[php]<?php
/*
MarketPress Flat-Rate Shipping Plugin
Author: Aaron Edwards (Incsub)
*/

class MP_Shipping_Flat_Rate extends MP_Shipping_API {

//private shipping method name. Lowercase alpha (a-z) and dashes (-) only please!
var $plugin_name = ‘flat-rate’;

//public name of your method, for lists and such.
var $public_name = ‘flat-rate’;

//set to true if you need to use the shipping_metabox() method to add per-product shipping options
var $use_metabox = true;

/**

  • Runs when your class is instantiated. Use to setup your plugin instead of __construct()
    */
    function on_creation() {
    //set name here to be able to translate
    $this->public_name = __(‘Flat Rate’, ‘mp’);
    }

/**

  • Echo anything you want to add to the top of the shipping screen
    */
    function before_shipping_form() {

}

/**

  • Echo anything you want to add to the bottom of the shipping screen
    */
    function after_shipping_form() {

}

/**

  • Echo a table row with any extra shipping fields you need to add to the shipping checkout form
    */
    function extra_shipping_field() {

}

/**

  • Use this to process any additional field you may add. Use the $_POST global,
  • and be sure to save it to both the cookie and usermeta if logged in.
    */
    function process_shipping_form() {

}

/**
  • Echo a settings meta box with whatever settings you need for you shipping module.
  • Form field names should be prefixed with mp[shipping][plugin_name], like “mp[shipping][plugin_name][mysetting]”.
  • You can access saved settings via $settings array.
    */
    function shipping_settings_box($settings) {
    global $mp;
    ?>
<div id="mp_flat_rate" class="postbox">
  <h3 class='hndle'><span><?php _e('Flat Rate Settings', 'mp'); ?></span></h3>
  <div class="inside">
    <span class="description"><?php _e('Be sure to enter a shipping price for every option or those customers may get free shipping.', 'mp') ?></span>
    <table class="form-table">
<?php
switch ($settings['base_country']) {
  case 'US':
    ?>
      <tr>
			<th scope="row"><?php _e('Lower 48 States', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][lower_48]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['lower_48']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('Hawaii and Alaska', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][hi_ak]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['hi_ak']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('Canada', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][canada]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['canada']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('International', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][international]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['international']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
    <?php
    break;

  case 'CA':
    ?>
      <tr>
			<th scope="row"><?php _e('In Country', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][in_country]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['in_country']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('United States', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][usa]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['usa']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('International', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][international]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['international']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
    <?php
    break;

  default:
    //in european union
    if ( in_array($settings['base_country'], $mp->eu_countries) ) {
      ?>
      <tr>
			<th scope="row"><?php _e('In Country', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][in_country]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['in_country']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('European Union', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][eu]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['eu']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('International', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][international]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['international']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <?php
    } else { //all other countries
      ?>
      <tr>
			<th scope="row"><?php _e('In Country', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][in_country]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['in_country']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <tr>
			<th scope="row"><?php _e('International', 'mp') ?></th>
			<td>
			<?php echo $mp->format_currency(); ?><input type="text" name="mp[shipping][flat-rate][international]" value="<?php echo esc_attr($settings['shipping']['flat-rate']['international']); ?>" size="5" maxlength="10" />
			</td>
      </tr>
      <?php
    }
    break;
}
?>
    </table>
  </div>
</div>
<?php

}

/**

  • Filters posted data from your form. Do anything you need to the $settings[‘shipping’][‘plugin_name’]
  • array. Don’t forget to return!
    */
    function process_shipping_settings($settings) {
return $settings;

}

/**

  • Echo any per-product shipping fields you need to add to the product edit screen shipping metabox
  • @param array $shipping_meta, the contents of the post meta. Use to retrieve any previously saved product meta
  • @param array $settings, access saved settings via $settings array.
    */
    function shipping_metabox($shipping_meta, $settings) {
    global $mp;
    ?>
    <?php _e('Extra Shipping Cost', 'mp'); ?>:
<?php echo $mp->format_currency(); ?><input type="text" size="6" id="mp_extra_shipping_cost" name="mp_extra_shipping_cost" value="<?php echo ($shipping_meta['extra_cost']) ? $mp->display_currency($shipping_meta['extra_cost']) : '0.00'; ?>" />
</label>
<?php

}

/**

  • Save any per-product shipping fields from the shipping metabox using update_post_meta
  • @param array $shipping_meta, save anything from the $_POST global
  • return array $shipping_meta
    */
    function save_shipping_metabox($shipping_meta) {
    //process extra per item shipping
    $shipping_meta[‘extra_cost’] = (!empty($_POST[‘mp_extra_shipping_cost’])) ? round($_POST[‘mp_extra_shipping_cost’], 2) : 0;
return $shipping_meta;

}

/**

  • Use this function to return your calculated price as an integer or float
  • @param int $price, always 0. Modify this and return
  • @param float $total, cart total after any coupons and before tax
  • @param array $cart, the contents of the shopping cart for advanced calculations
  • @param string $address1
  • @param string $address2
  • @param string $city
  • @param string $state, state/province/region
  • @param string $zip, postal code
  • @param string $country, ISO 3166-1 alpha-2 country code
  • return float $price
    */
    function calculate_shipping($price, $total, $cart, $address1, $address2, $city, $state, $zip, $country) {
    global $mp;
    $settings = get_option(‘mp_settings’);
//don't charge shipping if only digital products
if ( $mp->download_only_cart($cart) )
  return 0;

switch ($settings['base_country']) {
  case 'US':
    if ($country == 'US') {
      //price based on state
      if ($state == 'HI' || $state == 'AK')
        $price = $settings['shipping']['flat-rate']['hi_ak'];
      else
        $price = $settings['shipping']['flat-rate']['lower_48'];
    } else if ($country == 'CA') {
      $price = $settings['shipping']['flat-rate']['canada'];
    } else {
      $price = $settings['shipping']['flat-rate']['international'];
    }
    break;
  
  case 'CA':
    if ($country == 'CA') {
      $price = $settings['shipping']['flat-rate']['in_country'];
    } else if ($country == 'US') {
      $price = $settings['shipping']['flat-rate']['usa'];
    } else {
      $price = $settings['shipping']['flat-rate']['international'];
    }
    break;

  default:
    //in european union
    if ( in_array($settings['base_country'], $mp->eu_countries) ) {
      if ($country == $settings['base_country']) {
        $price = $settings['shipping']['flat-rate']['in_country'];
      } else if (in_array($country, $mp->eu_countries)) {
        $price = $settings['shipping']['flat-rate']['eu'];
      } else {
        $price = $settings['shipping']['flat-rate']['international'];
      }
    } else { //all other countries
      if ($country == $settings['base_country']) {
        $price = $settings['shipping']['flat-rate']['in_country'];
      } else {
        $price = $settings['shipping']['flat-rate']['international'];
      }
    }
    break;
}


//calculate extra shipping

$extras = array();
$counter = 0;
foreach ($cart as $product_id => $variations) {
$counter ++;
$shipping_meta = get_post_meta($product_id, ‘mp_shipping’, true);
foreach ($variations as $variation => $data) {
if (!$data[‘download’])
$extras[] = $shipping_meta[‘extra_cost’] * $data[‘quantity’];
}
}
$extra = array_sum($extras);

//merge
$price = round($price + $extra, 2);
//figure shipping based on counter
$perthreeitemsshipping = 1.50;
$multiplier = round( ( $counter / 3 ) );
$price += $muliplier * perthreeitemsshipping;

//merge
$price = round($price + $extra, 2);

return $price;

}
}

//register plugin
mp_register_shipping_plugin( ‘MP_Shipping_Flat_Rate’, ‘flat-rate’, __(‘Flat Rate’, ‘mp’) );
?>[/php]
Thanks!

Inside of the

[php] function calculate_shipping($price, $total, $cart, $address1, $address2, $city, $state, $zip, $country)[/php]

method, after

[php]//don’t charge shipping if only digital products
if ( $mp->download_only_cart($cart) )
return 0;[/php]

you can change the code in order to provide the desired outcome. To do this, you would have to calculate how many items are in the shopping cart then work out the shipping cost based on the 3 items per box model. For the shipping calculation, we could divide the number of items by 3 and then round up (as when the items don’t fit exactly into 3’s, an extra box is needed):

[php]$boxes_needed = ceil($number_of_items / 3);[/php]

Then multiply this to find the shipping cost:

[php]$shipping_cost = $boxes_needed * $flat_rate;[/php]

To do this, we have to first find the ‘$number_of_items’ variable. Are there other files included with the plugin? I found the ‘$cart’ variable yet could not find how this is defined or structured. Also, there are methods - such as ‘$mp->download_only_cart’ that are not defined in that code snippet you posted.

Oh my thank you so much for responding.

There is also this:
[php]<?php

/*

MarketPress Shipping Plugin Base Class

*/

if(!class_exists(‘MP_Shipping_API’)) {

class MP_Shipping_API {

//private shipping method name. Lowercase alpha (a-z) and dashes (-) only please!

var $plugin_name = '';



//public name of your method, for lists and such.

var $public_name = '';



//set to true if you need to use the shipping_metabox() method to add per-product shipping options

var $use_metabox = false;



/****** Below are the public methods you may overwrite via a plugin ******/



/**

 * Runs when your class is instantiated. Use to setup your plugin instead of __construct()

 */

function on_creation() {



	}



/**

 * Echo anything you want to add to the top of the shipping screen

 */

	function before_shipping_form() {



}



/**

 * Echo anything you want to add to the bottom of the shipping screen

 */

	function after_shipping_form() {



}



/**

 * Echo a table row with any extra shipping fields you need to add to the form

 */

	function extra_shipping_field() {



}



/**

 * Use this to process any additional field you may add. Use the $_POST global,

 *  and be sure to save it to both the cookie and usermeta if logged in.

 */

	function process_shipping_form() {



}

	

	/**

 * Echo a settings meta box with whatever settings you need for you shipping module.

 *  Form field names should be prefixed with mp[shipping][plugin_name], like "mp[shipping][plugin_name][mysetting]".

 *  You can access saved settings via $settings array.

 */

	function shipping_settings_box($settings) {



}



/**

 * Filters posted data from your form. Do anything you need to the $settings['shipping']['plugin_name']

 *  array. Don't forget to return!

 */

	function process_shipping_settings($settings) {



  return $settings;

}



/**

 * Echo any per-product shipping fields you need to add to the product edit screen shipping metabox

 *

 * @param array $shipping_meta, the contents of the post meta. Use to retrieve any previously saved product meta

 * @param array $settings, access saved settings via $settings array.

 */

	function shipping_metabox($shipping_meta, $settings) {

  //it is required to override this method if $use_metabox is set to true

  if ($this->use_metabox)

    wp_die( __("You must override the shipping_metabox() method in your {$this->public_name} shipping plugin if \$use_metabox is set to true!", 'mp') );

}



/**

 * Save any per-product shipping fields from the shipping metabox using update_post_meta

 *

 * @param array|string $shipping_meta, save anything from the $_POST global

 * return array|string $shipping_meta

 */

	function save_shipping_metabox($shipping_meta) {

	

  return $shipping_meta;

}



/**

 * Use this function to return your calculated price as an integer or float

 *

 * @param int $price, always 0. Modify this and return

 * @param float $total, cart total after any coupons and before tax

 * @param array $cart, the contents of the shopping cart for advanced calculations

 * @param string $address1

 * @param string $address2

 * @param string $city

 * @param string $state, state/province/region

 * @param string $zip, postal code

 * @param string $country, ISO 3166-1 alpha-2 country code

 *

 * return float $price

 */

	function calculate_shipping($price, $total, $cart, $address1, $address2, $city, $state, $zip, $country) {

  //it is required to override this method

  wp_die( __("You must override the calculate_shipping() method in your {$this->public_name} shipping plugin!", 'mp') );

}



	

	

	/****** Do not override any of these private methods please! ******/

	function _filter_method_lbl($lbl) {

  return $this->public_name;

}

	

//DO NOT override the construct! instead use the on_creation() method.

function MP_Shipping_API() {

	$this->__construct();

}



function __construct() {

  $this->on_creation();



  add_action( 'mp_checkout_before_shipping', array(&$this, 'before_shipping_form') );

  add_action( 'mp_checkout_after_shipping', array(&$this, 'after_shipping_form') );

  add_action( 'mp_checkout_shipping_field', array(&$this, 'extra_shipping_field') );

  add_action( 'mp_shipping_process', array(&$this, 'process_shipping_form') );

  //add_filter( 'mp_shipping_method_lbl', array(&$this, '_filter_method_lbl') );

  add_action( 'mp_shipping_settings', array(&$this, 'shipping_settings_box') );

  add_action( 'mp_shipping_settings_filter', array(&$this, 'process_shipping_settings') );

  add_action( 'mp_calculate_shipping', array(&$this, 'calculate_shipping'), 10, 9 );

  

  if ($this->use_metabox) {

    add_action( 'mp_shipping_metabox', array(&$this, 'shipping_metabox'), 10, 2 );

    add_filter( 'mp_save_shipping_meta', array(&$this, 'save_shipping_metabox') );

  }



}

}

}

/**

  • Use this function to register your shipping plugin class

  • @param string $plugin_name - the sanitized private name for your plugin

  • @param string $class_name - the case sensitive name of your plugin class

  • @param string $public_name - the public name of the plugin for lists and such

*/

function mp_register_shipping_plugin($class_name, $plugin_name, $public_name) {

global $mp_shipping_plugins;

if(!is_array($mp_shipping_plugins)) {

	$mp_shipping_plugins = array();

}



if(class_exists($class_name)) {

	$mp_shipping_plugins[$plugin_name] = array($class_name, $public_name);

} else {

	return false;

}

}

?>[/php]

and this:

[php]

<?php /* MarketPress Example Shipping Plugin Template */ class My_Plugin_Name extends MP_Shipping_API { //private shipping method name. Lowercase alpha (a-z) and dashes (-) only please! var $plugin_name = 'my-plugin-name'; //public name of your method, for lists and such. var $public_name = ''; //set to true if you need to use the shipping_metabox() method to add per-product shipping options var $use_metabox = false; /** * Runs when your class is instantiated. Use to setup your plugin instead of __construct() */ function on_creation() { //declare here for translation $this->public_name = __('My Plugin', 'mp'); } /** * Echo anything you want to add to the top of the shipping screen */ function before_shipping_form() { } /** * Echo anything you want to add to the bottom of the shipping screen */ function after_shipping_form() { } /** * Echo a table row with any extra shipping fields you need to add to the form */ function extra_shipping_field() { } /** * Use this to process any additional field you may add. Use the $_POST global, * and be sure to save it to both the cookie and usermeta if logged in. */ function process_shipping_form() { } /** * Echo a settings meta box with whatever settings you need for you shipping module. * Form field names should be prefixed with mp[shipping][plugin_name], like "mp[shipping][plugin_name][mysetting]". * You can access saved settings via $settings array. */ function shipping_settings_box($settings) { } /** * Filters posted data from your form. Do anything you need to the $settings['shipping']['plugin_name'] * array. Don't forget to return! */ function process_shipping_settings($settings) { return $settings; } /** * Echo any per-product shipping fields you need to add to the product edit screen shipping metabox * * @param array $shipping_meta, the contents of the post meta. Use to retrieve any previously saved product meta * @param array $settings, access saved settings via $settings array. */ function shipping_metabox($shipping_meta, $settings) { } /** * Save any per-product shipping fields from the shipping metabox using update_post_meta * * @param array $shipping_meta, save anything from the $_POST global * return array $shipping_meta */ function save_shipping_metabox($shipping_meta) { return $shipping_meta; } /** * Use this function to return your calculated price as an integer or float * * @param int $price, always 0. Modify this and return * @param float $total, cart total after any coupons and before tax * @param array $cart, the contents of the shopping cart for advanced calculations * @param string $address1 * @param string $address2 * @param string $city * @param string $state, state/province/region * @param string $zip, postal code * @param string $country, ISO 3166-1 alpha-2 country code * * return float $price */ function calculate_shipping($price, $total, $cart, $address1, $address2, $city, $state, $zip, $country) { return $price; } } //register plugin - uncomment to register //mp_register_shipping_plugin( 'My_Plugin_Name', 'my-plugin-name', __('My Plugin', 'mp') ); ?>[/php]

and there is a general marketpress.php if you need that.

Can you upload that as well? I still can’t find the $cart variables definition.

Hmm…having trouble posting this

Could you post it to a “pastebin” site - store of pasted information that gives you a link such as:

It’s pasted–how do I get you there?

Sponsor our Newsletter | Privacy Policy | Terms of Service