WordPress.org

Ready to get started?Download WordPress

Forums

WooCommerce SagePay Direct Payment Gateway
[resolved] Passing basket contents (8 posts)

  1. richardhowes
    Member
    Posted 9 months ago #

    Anybody had any success in passing the basket contents to sage pay? Any example code, as my version is not passing anything in the basket field.

    http://wordpress.org/extend/plugins/sagepay-direct-for-woocommerce-payment-gateway/

  2. swicks
    Member
    Plugin Author

    Posted 9 months ago #

    Interestingly nobody has asked for the basket parameter - this should be very straight forward to implement - SagePay uses colons to separate the fields, starting with the item count.

    if( $this->add_basket )
                {
                    $items = $this->order->get_items();
    
                    // total count
                    $basket = count($items);
    
                    foreach($items as $item)
                    {
                        $desc = $item['name'];
                        $qty = $item['qty'];
                        $line_sub_total = $item['line_subtotal'];
                        $line_sub_tax = $item['line_tax'];
                        $item_value = round($line_sub_total / $qty, 2);
                        $item_tax = round($line_sub_tax / $qty, 2);
                        $item_total = $item_value + $item_tax;
                        $line_total = $line_sub_total + $line_sub_tax;
    
                        $basket .= ':' . $desc . ':' . $qty . ':' . $item_value . ':' . $item_tax . ':' . $item_total . ':' .$line_total;
                    }
                    $this->add_param( 'Basket', $basket );
                }

    I will implement this in the next rev.

    regards

    swicks

  3. richardhowes
    Member
    Posted 9 months ago #

    Do you have a quick way of returning the SKU also for the product? I cannot see that it is passed back by the query in the Get_Items() function so would presume we'd have to write a query to cross reference the product ID with the _sku field stored in wp_postmeta (table)?

  4. swicks
    Member
    Plugin Author

    Posted 9 months ago #

    the wc-product abstract class uses magic method __get which will do most of the work for you. In the example above you could add:

    $product = get_product( $item['product_id'] ); //returns an instance
    'sku = $product->get_sku(); //returns the sku
    
    $desc = $item['name'] . ' [' . $sku . ']';
  5. richardhowes
    Member
    Posted 8 months ago #

    is there a method for getting the shipping total also? Shipping doesn't come under the product class?

  6. swicks
    Member
    Plugin Author

    Posted 8 months ago #

    You will need to pull this from WC Order

    You would have called this earlier to get the items

    $this->order = new WC_Order($order_id);

    Append to the $basket string

    $desc = 'Shipping';
    $qty = '1';
    $item_value = $this->order->order_shipping;
    $item_tax = $order->order_shipping_tax;
    $item_total = $item_value + $item_tax;
    $line_total = $item_total;
    
    $basket .= ':' . $desc . ':' . $qty . ':' . $item_value . ':' . $item_tax . ':' . $item_total . ':' .$line_total;
  7. richardhowes
    Member
    Posted 8 months ago #

    The SKU being returned brings back the product SKU, but doesn't take into account any variation of the product. i.e. if there are blue, red, and green versions and blue is default, the blue sku will be returned to the system regardless of which variation has been selected in the cart?

    Is there a way around this?

  8. swicks
    Member
    Plugin Author

    Posted 8 months ago #

    Oops - very good point - you need to get the variation SKU

    Try replacing:-

    $product = get_product( $item['product_id'] ); //returns an instance
    $sku = $product->get_sku(); //returns the sku

    With:-

    $product = get_product( $item['product_id'] ); //returns an instance
    // check whether this is a variable product - if so use it's variable id
    if( $product->product_type == 'variable' )
        $product = get_product( $item['variation_id'] );
    $sku = $product->get_sku(); //returns the sku

    I haven't tried this, but it should work...

    Regards

    Steve

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.