Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author swicks

    (@swicks)

    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

    Thread Starter richardhowes

    (@richardhowes)

    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)?

    Plugin Author swicks

    (@swicks)

    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 . ']';

    Thread Starter richardhowes

    (@richardhowes)

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

    Plugin Author swicks

    (@swicks)

    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;

    Thread Starter richardhowes

    (@richardhowes)

    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?

    Plugin Author swicks

    (@swicks)

    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

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Passing basket contents’ is closed to new replies.