Showing posts with label API. Show all posts
Showing posts with label API. Show all posts

Tuesday 24 September 2019

How to create pagination in shopify rest api using php

Shopify new API next page issue | page_info => invalid value

Shopify API version 2019-07 you do indeed need to use cursor-based pagination.

First make a normal API call, include the header response.

Then in the header response API, you will see the "link" parameter with rel next or previous.

Extract the page_info and then make another call with page_info.
The first API call is something like this:

https://.....@abc.myshopify.com/admin/api/2019-10/products.json?limit=2&published_status=published
To get header response from API response :
function getheaderarray($apiresponse){
       
        $data = explode("\n",$apiresponse);
        $headers['status'] = $data[0];
        array_shift($data);
        foreach($data as $part){
            $middle = explode(":",$part,2);
            if ( !isset($middle[1]) ) { $middle[1] = null; }
            $headers[trim($middle[0])] = trim($middle[1]);
        }
       
        return $headers;
  }  
Then the second API call is something like this:
https://.....@abc.myshopify.com/admin/api/2019-10/products.json?limit=2&page_info=abcdsf21asdf3afdfdfd1safd51s3f1x32ref4asdj
When you make the second call, do not add any filers as the filters will be applied from the first call.
FYI: if your testing in a browser due to the way the link is in angle brackets, it will be hidden, so just view source code in your developer environment.

Monday 25 June 2018

Product image roles randomly disappear when update product through the Rest API

I think its issue on Magento 2.2.3 or below version

Open root/app/code/Magento/Catalog/Model/ProductRepository.php

protected function processMediaGallery(ProductInterface $product, $mediaGalleryEntries)
{

find : if (isset($entry['value_id']) // near line no : 499 TO
if (isset($entry['value_id']) && $entry['value_id'] != '') {

}

Thursday 5 May 2016

How to Shopify API Product / Order / Collection get sorted when passing in an created_at / updated_at filter

What would be ideal would be to be able to get results in ascending/descending order one way or another, but without having to pass in since_id, because we want to be able to pull in new data on orders on an ongoing basis as they may be updated.

Its possible to sort the Products/Orders/Collection by passing an "order" parameter. The "order" parameter should contain the field to sort by (supported fields are "created_at", "updated_at" and "processed_at"), followed by a space and then by an "asc" or "desc".

Please check following Example for solution.

https://example.myshopify.com/admin/orders.json?limit=2&fields=id,created_at&order=created_at%20asc

Friday 18 September 2015

Add product to cart but price getting 0 or NULL in magento API

I’m testing out the Cart API and it seems I cannot get any data related to price, total price, etc until I enter in customer information.  The variables are there, but all of the price values are null/0. Is this the intended functionality, or am I doing something wrong?

The code below works except any variable relating to the price is null.

<pre>
<?php
$client = new SoapClient('http://magento.com/api/soap/?wsdl');
$session = $client->login('jemsw','write');
$cartId = $client->call($session,'cart.create',array(0));
$result = $proxy->call($sessionId, 'cart_product.add', array($cartId, array("product_id" => 53,"qty" => 2)));
$result = $client->call($session,'cart.totals',array($cartId));
print_r($result);
?></pre>

Array(
    [0] => Array(
        [title] => Subtotal
        [amount] =>
        )
        [1] => Array(
        [title] => Grand Total
        [amount] =>
        )
    )

If I add this code in after add product to cart `cart_product.add` the price is reflected correctly.

<pre>
<?php

    $client = new SoapClient('http://magento.com/api/soap/?wsdl');$session = $client->login('jemsw','write');

    $cartId = $client->call($session,'cart.create',array(0));

    $result = $proxy->call($sessionId, 'cart_product.add', array($cartId, array("product_id" => 53,"qty" => 2)));


    ################################################################################


    $arrAddresses = array(

        array(

            "mode" => "shipping",

            "firstname" => "test",

            "lastname" => "test",

            "company" => "test",

            "street" => "test",

            "city" => "test",

            "region" => "test",

            "postcode" => "test",

            "country_id" => "id",

            "telephone" => "0123789",

            "fax" => "01234589",

            "is_default_shipping" => 0,

            "is_default_billing" => 0

        ),

        array(

            "mode" => "billing",

            "firstname" => "test",

            "lastname" => "test",

            "company" => "test",

            "street" => "test",

            "city" => "test",

            "region" => "test",

            "postcode" => "test",

            "country_id" => "id",

            "telephone" => "012356789",

            "fax" => "0123459",

            "is_default_shipping" => 0,

            "is_default_billing" => 0

        )

    );

    $resultCustomerAddresses = $client->call($session, "cart_customer.addresses", array($cartId, $arrAddresses));


    ################################################################################

    $result = $proxy->call($sessionId, "cart.info", array($cartId));

    print_r($result);

    ?>

    </pre>

Display Your Cart item with price. 

Tuesday 1 September 2015

How to create shipping and invoice in magneto by code or API


Various merchants, various demands. Imagine you have a private on site sale or something like that, where your checkout requirements are pretty simply: create invoice / ship and complete the order all at once. For example, you are doing the checkout for bunch of people standing in front of you, paying you money right on the spot. In such scenario overload of manually creating an invoice and shipment can be too much.

Thus, having your Magento automatically invoice/ship/complete orders can be a logical request. So how do we do that?

Easy! All you need to do is to observe the sales_order_save_after event or observe the controller_action_predispatch event and target the Mage_Checkout_OnepageController::successAction() catching the Mage::getSingleton(‘checkout/session’)->getLastOrderId(). In this example I decided to demonstrate the possible (not ideal, or not even the best) “sales_order_save_after event” approach. 

And here is the code for our observer model. 

$order = $observer->getEvent()->getOrder();
 
        $orders = Mage::getModel('sales/order_invoice')->getCollection()
                        ->addAttributeToFilter('order_id', array('eq'=>$order->getId()));
        $orders->getSelect()->limit(1);  
 
        if ((int)$orders->count() !== 0) {
            return $this;
        }
 
        if ($order->getState() == Mage_Sales_Model_Order::STATE_NEW) {
 
            try {
                if(!$order->canInvoice()) {
                    $order->addStatusHistoryComment('Inchoo_Invoicer: Order cannot be invoiced.', false);
                    $order->save();  
                }
 
                //START Handle Invoice
                $invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
 
                $invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
                $invoice->register();
 
                $invoice->getOrder()->setCustomerNoteNotify(false);          
                $invoice->getOrder()->setIsInProcess(true);
                $order->addStatusHistoryComment('Automatically INVOICED by Invoicer.', false);
 
                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($invoice)
                    ->addObject($invoice->getOrder());
 
                $transactionSave->save();
                //END Handle Invoice
 
                //START Handle Shipment
                $shipment = $order->prepareShipment();
                $shipment->register();
 
                $order->setIsInProcess(true);
                $order->addStatusHistoryComment('Automatically SHIPPED by Invoicer.', false);
 
                $transactionSave = Mage::getModel('core/resource_transaction')
                    ->addObject($shipment)
                    ->addObject($shipment->getOrder())
                    ->save();
                //END Handle Shipment
            } catch (Exception $e) {
                $order->addStatusHistoryComment('Inchoo_Invoicer: Exception occurred during automaticallyInvoiceShipCompleteOrder action. Exception message: '.$e->getMessage(), false);
                $order->save();
            }                
        }