0

I have created a shipping group attribute for products where the product is either boxed or wrapped.

I currently have the WebShopApps MatrixRate extension installed and have created a table where it is weight vs destination and if the product group is boxed then the price is higher.

For example:

zip 1001 and weight 5 where group boxed equal to price 5 dollars and delivery location boxed near

zip 1001 and weight 5 where group wrapped equal to price 3 dollars and delivery location wrapped near

Now on the onepage checkout there is a selection for the product shipping. If I buy a product and it is grouped into wrapped it shows like this.

Shipping Options:

(radio select)boxed near 5 dollars -->this is not supposed to show<--

(radio select)wrapped near 3 dollars

Pickup:

(radio select)In Store

I decided to remove the shipping option that is not supposed to show by coding the available.phtml file. My theme is based on RWD and the file is located in the app > design > frontend > theme_name > theme_default > template > checkout > onepage > shipping_method

<?php
    /* check if cart has boxed item */
    $cart_check = 0;
    $cart = Mage::getModel('checkout/cart')->getQuote();
    foreach ($cart->getAllItems() as $item) {
        $productId = $item->getProduct()->getId();
        $product = Mage::getModel('catalog/product')->load($productId);
        $attributes = $product->getAttributes();
        foreach ($attributes as $attribute) {
            if($attribute->getAttributeCode() == 'package_id') {
                $attributeLabel = $attribute->getFrontendLabel();
                $value = $attribute->getFrontend()->getValue($product);
                if($value="Boxed") {
                    $cart_check = $cart_check + 1;
                }
            }
        }
    }
    $show_ship = "Wrapped";
    if($cart_check!==0) {
        $show_ship = "Boxed";
    } else {
        $show_ship = "Wrapped";
    }
?>
<dl class="sp-methods">
<?php $shippingCodePrice = array(); ?>
<?php $_sole = count($_shippingRateGroups) == 1; foreach ($_shippingRateGroups as $code => $_rates): ?>
    <dt>
        <?php
            echo $this->escapeHtml($this->getCarrierName($code));
        ?>
    </dt>
    <dd>
        <ul>
        <?php $_sole = $_sole && count($_rates) == 1; foreach ($_rates as $_rate): ?>
            <?php
                /* code for checking if Boxed or Wrapped or not */
                $label_shipping = $this->escapeHtml($_rate->getMethodTitle());
                if( strpos( $label_shipping, $show_ship ) !== false ) {
                } else {
            ?>
            <?php $shippingCodePrice[] = "'".$_rate->getCode()."':".(float)$_rate->getPrice(); ?>
            <li>
               <?php if ($_rate->getErrorMessage()): ?>
                    <ul class="messages"><li class="error-msg"><ul><li><?php echo $this->escapeHtml($_rate->getErrorMessage()); ?></li></ul></li></ul>
               <?php else: ?>
                    <?php if ($_sole) : ?>
                        <span class="no-display"><input name="shipping_method" type="radio" value="<?php echo $_rate->getCode(); ?>" id="s_method_<?php echo $_rate->getCode(); ?>" checked="checked" /></span>
                    <?php else: ?>
                        <input name="shipping_method" type="radio" value="<?php echo $_rate->getCode(); ?>" id="s_method_<?php echo $_rate->getCode(); ?>"<?php if($_rate->getCode()===$this->getAddressShippingMethod()) { echo ' checked="checked"'; } ?> class="radio" />
                    <?php if ($_rate->getCode() === $this->getAddressShippingMethod()): ?>
                        <script type="text/javascript">
                            //<![CDATA[
                                lastPrice = <?php echo (float)$_rate->getPrice(); ?>;
                            //]]>
                        </script>
                    <?php endif; ?>
                    <?php endif; ?>
                        <label for="s_method_<?php echo $_rate->getCode(); ?>">
                        <?php
                            /* code for removing wrapped or boxed text */
                            $label_shipping = $this->escapeHtml($_rate->getMethodTitle());
                            if( strpos( $label_shipping, 'Wrapped' ) !== false ) { echo substr($label_shipping, 9); }
                            elseif( strpos( $label_shipping, 'Boxed' ) !== false ) { echo substr($label_shipping, 6); }
                            else { echo $label_shipping; }
                        ?>
                    <?php $_excl = $this->getShippingPrice($_rate->getPrice(), $this->helper('tax')->displayShippingPriceIncludingTax()); ?>
                    <?php $_incl = $this->getShippingPrice($_rate->getPrice(), true); ?>
                    <?php echo $_excl; ?>
                    <?php if ($this->helper('tax')->displayShippingBothPrices() && $_incl != $_excl): ?>
                        (<?php echo $this->__('Incl. Tax'); ?> <?php echo $_incl; ?>)
                    <?php endif; ?>
                        </label>
                        <?php if($label_shipping=="Somewhere Over There") { ?>
                            <?php echo "<div>" . $this->getLayout()->createBlock('cms/block')->setBlockId('shipping_in_store')->toHtml() . "</div>"; ?>
                        <?php } ?>
               <?php endif; ?>
            </li>
            <?php } ?>
        <?php endforeach; ?>
        </ul>
    </dd>
<?php endforeach; ?>
</dl>

This was a rush job and I just wanted results. The problem is now if another shipping selection were to be on another page it would show the removed shipping option.

Is there a better way to code this?

1

Instead of modifying the template, I would create a rewrite for the getRate function in Webshopapps_Matrixrate_Model_Carrier_Matrixrate. Something like this.

class Namespace_Module_Model_Carrier_Matrixrate extends Webshopapps_Matrixrate_Model_Carrier_Matrixrate
{
    public function getRate(Mage_Shipping_Model_Rate_Request $request)
    {
       $ratearray = parent::getRate($request);

       foreach($request->getAllItems() as $item){
        if(/*write condition that you are looking for to remove shipping rate*/){
            foreach($ratearray as $key=>$rate){
                if(strpos(strtolower($rate['delivery_type']),'shipping_method_you_are_looking_for') === false){
                    unset($ratearray[$key]);
                }
            }
        }
    }

    return $ratearray;
}
  • I will try this out... – fja3omega Feb 25 '17 at 9:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.