LemonStand Forum: Restricted Shipping Module - LemonStand Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Restricted Shipping Module

#1 User is offline   jonathani501 

  • Member
  • Group: Members
  • Posts: 3
  • Joined: 19-October 11

Posted 13 December 2011 - 09:13 PM

I am not sure if anyone else needs this, but I am creating a module for restricted shipping. :-) I'm quite sure I will need some help from the community on pieces of this.

Here are the first two pieces. Note: I changed my product buttons from the default images to css styled inputs. You can do yours anyway. I just wanted to save a little bandwidth.

Replace the "add to cart" button code in product_partial with the following.

<!-- Add To Cart -->

<? 
	// Look for the value of of the restricted item column
	($restricted = $product->x_restricted_shipping) 
?>
<? 
	// Checks to see if item is not a restriced item
	if ($restricted == false): 
?>

<input type="button" class="button_blue float_right"
	onclick="return $(this).getForm().sendRequest('shop:on_addToCart', {
    extraFields: {flash_partial: 'add_to_cart_partial'},
    update: {'mini_cart': 'shop:mini_cart', 'product_page': 'product_partial' }})" value="Add to Cart"/> 
		
<? 
	else: 
	// Show Restriced Button

?>

<input type="button" class="button_blue float_right"
    onclick="return $(this).getForm().sendRequest('shop:on_addToCart', {
    confirm: 'This item can only be shipped within the Continental United States, do you still want to add this item to your cart?',
    extraFields: {flash_partial: 'add_to_cart_partial'},
    update: {'mini_cart': 'shop:mini_cart', 'product_page': 'product_partial' }})" value="Add to Cart"/> 

<? endif ?>

<!-- End Add To Cart -->


If the item is restricted, when a customer adds it to their cart, a pop up window will open alerting the customer that the item being added is restricted, and ask them if they still want to add it to their cart. In my case, I alerted the customer that this item can only be shipped within the Continental United States.

The attached Zip File is a Restricted Shipping Module that creates a new column in the product database for the restricted option. It also creates Restricted Shipping check box in the product tab under shipping.

Attached File(s)


0

#2 User is offline   Hexarobi 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 103
  • Joined: 10-August 11

Posted 19 December 2011 - 07:29 AM

I did something similar to this for ORM-D restricted shipping. My solution was an ORM-D restricted checkbox under the Inventory tab for each product. If this box is checked, a note is displayed on the product details page, but I like your idea of a pop-up when adding to cart.

modules/ormdproducts/classes/ormdproducts_module.php
<?php
class ormdProducts_Module extends Core_ModuleBase {

    protected function createModuleInfo() {
        return new Core_ModuleInfo(
            "ORM-D Products",
            "Allows products to be marked as ORM-D and shipping restricted appropriately",
            "Hexarobi"
        );
    }

    public function subscribeEvents() {
        Backend::$events->addEvent('shop:onFilterShippingOptions', $this, 'filter_shipping_options');
        Backend::$events->addEvent('shop:onExtendProductModel', $this, 'extend_product_model');
        Backend::$events->addEvent('shop:onExtendProductForm', $this, 'extend_product_form');
    }

    public function extend_product_model($product) {
        $product->define_column('x_ormdproducts_is_ormd', 'ORM-D Restricted')->invisible();
    }

    public function extend_product_form($product, $context) {
        $product->add_form_field('x_ormdproducts_is_ormd')->tab('Shipping');
    }

    public static function filter_shipping_options($params) {

        $cart_contains_ormd_product = false;
        foreach ($params['order_items'] as $item) {
            if (isset($item->product->x_ormdproducts_is_ormd) && $item->product->x_ormdproducts_is_ormd == 1) $cart_contains_ormd_product = true;
        }

        // If no ORM-D products then return all shipping options
        if (!$cart_contains_ormd_product && !$cart_contains_domesitic_only) {
            return $params['options'];
        }

        if ($cart_contains_ormd_product) {
            $allowed_ormd_shipping_methods = array(
                'UPS Ground',
            );
        }

        // Restrict shipping options to only those listed above
        $result = array();
        foreach ($params['options'] as $i=>$option) {

            $new_option = $option;

            if (in_array($option->name, $allowed_ormd_shipping_methods)) {
                $result[$i] = $new_option;
            } else {
                $new_sub_options = array();
                foreach ($option->sub_options as $key=>$sub_option) {
                    //var_dump($sub_option);
                    if (in_array($sub_option->name, $allowed_ormd_shipping_methods)) {
                        $new_sub_options[] = $sub_option;
                    }
                }
                if (sizeof($new_sub_options) > 0) {
                    $new_option->sub_options = $new_sub_options;
                    $result[$i] = $new_option;
                }
            }
        }
        return $result;
    }
}
?>

Edit: Changed $result[] = $new_option; to $result[$i] = $new_option;

modules/ormproducts/updates/1.sql
alter table shop_products add column x_ormdproducts_is_ormd tinyint(4);


modules/ormproducts/updates/version.dat
#1 Initial release


added to product_partial.php
<?
if ($product->x_ormdproducts_is_ormd == 1) {
    ?>
    <p>
        NOTE: This product is considered an ORM-D "Consumer Commodity" by the US Department of Transportation and cannot
        be sent via international or airborne expedited shipping.
        Adding this product to your cart will result in the restriction of shipping options to UPS Ground only.
    </p>
    <?
}
?>

1

#3 User is offline   Hexarobi 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 103
  • Joined: 10-August 11

Posted 19 January 2012 - 11:17 AM

A recent update seems to have started requiring proper indexes on the $results array. This is easily fixed in the code above by changing each occurance of:

$result[] = $new_option;

to
$result[$i] = $new_option;

0

#4 User is offline   pixelsailor 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 43
  • Joined: 08-January 12
  • LocationTexas

Posted 06 February 2012 - 01:26 AM

Is it possible to edit either of these options to check for the restriction key at checkout?

I have a similar situation only mine requires denial of shipment to particular states. The module I wrote uses a new many-to-many table in the database to link the products table with the states table. On the admin side, individual states are checked indicating those that the product can't be shipped to. On the customer side, when viewing a product the array of flagged states (if any) is retrieved and display to the customer -- but that's it. I'd like to be able to include these states in the checkout as well, effectively denying the sale if the shipping address includes a flagged state. I'm thinking the only way to do this is to run another query when checkout starts on each product in the cart then store those states in an array. I would prefer to just use my original query from the product page and store those states to the order somehow. But I don't know how to do that. Any suggestions or should I just go the "query at checkout" route?
0

#5 User is offline   Hexarobi 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 103
  • Joined: 10-August 11

Posted 06 February 2012 - 09:31 AM

It sounds like you still want to hook into FilterShippingOptions, which will handle filtering shipping options for both the estimator and the checkout process. If you want to deny all shipping methods to certain states, it is pretty easy. Just loop through the order items, for each one look up that products' "banned state list" and if the shipping_state is in that list, return an empty array. If you want to only allow certain shipping options to states than it's a little more complicated, but not that different from the code above. I can help you out with that code if needed.
0

#6 User is offline   pixelsailor 

  • Member
  • PipPipPip
  • Group: Members
  • Posts: 43
  • Joined: 08-January 12
  • LocationTexas

Posted 06 February 2012 - 09:53 PM

Thanks for the info. I'll try giving that a shot.
0

Share this topic:


Page 1 of 1

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users