代码改变世界

沃尔玛修改促销价格

2023-11-15 16:53  天心PHP  阅读(266)  评论(0编辑  收藏  举报

文档地址:Promotion - US - API Reference - Walmart Developer Portal

1.单个调价接口 Update a promotional price

us和mx 接口地址:https://marketplace.walmartapis.com/v3/price?promo=true

ca接口地址:https://marketplace.walmartapis.com/v3/ca/price?promo=true

"priceDisplayCodes": "CART" 这个参数不能要  不然页面变成了  see price in cart

processMode = DELETE  删除活动 必须是当前活动的原始开始和结束时间

replaceAll 不填 可以吧非促销活动变成促销活动, replaceAll="true" 可以删除当前促销活动,重新设置促销活动

促销价格最好设置为 90%  如果设置少一分 两分的 不会成功  促销价格必须小于对比价格

//currency货币类型  $currencyarr = ['us'=>'USD','mx'=>'MXN','ca'=>'CAD'];

$walmartPostService = new WalmartListingData(30);
$effective_date = date('Y-m-d H:i:s', (strtotime(date('Y-m-d H:i:s')) - (3600*8)+300));//减去时差8个小时,也就是开始时间为当前时间+5分钟
$expiration_date = date('Y-m-d H:i:s', (strtotime('2023-11-20 08:00:00') - (3600*8)));//结束时间
$data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Price xmlns="http://walmart.com/">
    <itemIdentifier>
        <sku>Bling-WFS-3904-003009-12U0D</sku>
    </itemIdentifier>
    <pricingList replaceAll="true">
        <pricing effectiveDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($effective_date)).'" expirationDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($expiration_date)).'" processMode="UPSERT">
            <currentPrice>
                <value currency="USD" amount="21.58"/>
            </currentPrice>
            <currentPriceType>REDUCED</currentPriceType>
            <comparisonPrice>
                <value currency="USD" amount="23.99"/>
            </comparisonPrice>
        </pricing>
    </pricingList>
</Price>';
$res = $walmartPostService->updateproprice($data);//返回为xml类型
//xml转换为数组
$preg = "/(<|<\/)(?:\w+:)/is";
$newXmlStr = preg_replace($preg,"\\1",$res[1]);
$xmlstring = simplexml_load_string($newXmlStr);
$res[1] = json_decode(json_encode($xmlstring),true);


/**
 * 单个促销调价(new)
 */
public function updatepropricenew($data){
    $accountInfo = array();
    $url = 'https://marketplace.walmartapis.com/v3/price?promo=true';
    $extra = [
        "Accept"=>"application/xml",
        "Content-Type"=>"application/xml",
    ];
    if($this->site=='ca'){
        $url = 'https://marketplace.walmartapis.com/v3/ca/price?promo=true';
    }
    $headers = $this->getHeaders($url, 'PUT', $accountInfo, $extra);
    $result = $this->cur_request($url,'PUT',$data,$headers);
    return $result;
}

public function cur_request($URL, $type, $params, $headers)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    if ($headers != "") {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    } else {
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/x-www-form-urlencoded'));
    }
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch,CURLOPT_ENCODING,""); // 返回数据支持所有编码,不乱码 ;如果为空字符串"",会发送所有支持的编码类型。
    switch ($type) {
        case "GET" :
            curl_setopt($ch, CURLOPT_HTTPGET, true);
            break;
        case "POST":
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            break;
        case "PUT" :
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            break;
        case "PATCH":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            break;
        case "DELETE":
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            break;
    }
    $file_contents = curl_exec($ch);//获得返回值
    $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return [$responseCode, $file_contents];
}

 2.批量调促销价格

$xmlus = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<PriceFeed xmlns="http://walmart.com/">
    <PriceHeader>
        <version>1.5.1</version>
    </PriceHeader>
    <Price xmlns="http://walmart.com/">
        <itemIdentifier>
            <sku>Bling-xuni-3901-007012-07U0A</sku>
        </itemIdentifier>
        <pricingList replaceAll="true">
            <pricing effectiveDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($effective_date)).'" expirationDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($expiration_date)).'" processMode="UPSERT">
                <currentPrice>
                    <value currency="USD" amount="38.99"/>
                </currentPrice>
                <currentPriceType>REDUCED</currentPriceType>
                <comparisonPrice>
                    <value currency="USD" amount="43.99"/>
                </comparisonPrice>
            </pricing>
        </pricingList>
    </Price>
    <Price xmlns="http://walmart.com/">
        <itemIdentifier>
            <sku>CY-HWC-3701-010416-06W0A</sku>
        </itemIdentifier>
        <pricingList replaceAll="true">
            <pricing effectiveDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($effective_date)).'" expirationDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($expiration_date)).'" processMode="UPSERT">
                <currentPrice>
                    <value currency="USD" amount="25.99"/>
                </currentPrice>
                <currentPriceType>REDUCED</currentPriceType>
                <comparisonPrice>
                    <value currency="USD" amount="26.99"/>
                </comparisonPrice>
            </pricing>
        </pricingList>
    </Price>
    <Price xmlns="http://walmart.com/">
        <itemIdentifier>
            <sku>CY-HWC-3009-010018-06U0A</sku>
        </itemIdentifier>
        <pricingList replaceAll="true">
            <pricing effectiveDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($effective_date)).'" expirationDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($expiration_date)).'" processMode="UPSERT">
                <currentPrice>
                    <value currency="USD" amount="45.99"/>
                </currentPrice>
                <currentPriceType>REDUCED</currentPriceType>
                <comparisonPrice>
                    <value currency="USD" amount="49.99"/>
                </comparisonPrice>
            </pricing>
        </pricingList>
    </Price>
    <Price xmlns="http://walmart.com/">
        <itemIdentifier>
            <sku>Bling-xuni-2008-002326-01W0A</sku>
        </itemIdentifier>
        <pricingList replaceAll="true">
            <pricing effectiveDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($effective_date)).'" expirationDate="'.date('Y-m-d\TH:i:s.000\Z', strtotime($expiration_date)).'" processMode="UPSERT">
                <currentPrice>
                    <value currency="USD" amount="39.99"/>
                </currentPrice>
                <currentPriceType>REDUCED</currentPriceType>
                <comparisonPrice>
                    <value currency="USD" amount="45.99"/>
                </comparisonPrice>
            </pricing>
        </pricingList>
    </Price>
</PriceFeed>';
$res = $walmartPostService->updatebulkprice($xml,'promo');

/**
 * 批量调价
 */
public function updatebulkprice($data,$feedtype){
    $accountInfo = array();
    if($this->site=='ca'){
        $url = 'https://marketplace.walmartapis.com/v3/ca/feeds?feedType='.$feedtype;
    }else{
        $url = 'https://marketplace.walmartapis.com/v3/feeds?feedType='.$feedtype;
    }
    $extra = [
        "Accept"=>"application/xml",
        "Content-Type"=>"multipart/form-data",
    ];
    $body = ['file'=>($data)];
    $headers = $this->getHeaders($url, 'POST', $accountInfo, $extra);
    $res = $this->cur_request($url,'POST',$body,$headers);
    return $res;
}

获取调价结果

public function feedStatusNew($feedId, $accountId)
{
    $accountInfo = array();
    $offset = 0;
    $preg = "/(<|<\/)(?:\w+:)/is";
    $info = $itemDetails = [];
    for($i=1;$i<=10;$i++){
        $url = "https://marketplace.walmartapis.com/v3/feeds/{$feedId}?includeDetails=true&limit=1000&offset=".$offset;
        if($this->site=='ca'){
            $url = "https://marketplace.walmartapis.com/v3/ca/feeds/{$feedId}?includeDetails=true&limit=1000&offset=".$offset;
        }
        $extra = [
            "Accept"=>"application/xml",
            "Content-Type"=>"application/xml",
        ];
        $headers = $this->getHeaders($url, 'GET', $accountInfo, $extra);
        $result  = $this->getResult($url, $headers);
        $newXmlStr = preg_replace($preg,"\\1",$result);
        $xmlstring = simplexml_load_string($newXmlStr);
        $xmlarr = json_decode(json_encode($xmlstring),true);
        if(isset($xmlarr['error']) || isset($xmlarr['errors'])){
            if(isset($info['itemDetails'])){
                return $info;
            }else{
                return $xmlarr;
            }
        }
        if($offset==0){
            $itemDetails = $xmlarr['itemDetails']['itemIngestionStatus'];
            unset($xmlarr['itemDetails']);
            $info = $xmlarr;
        }else{
            $info['offset'] = $offset;
            $itemDetails = array_merge($itemDetails,$xmlarr['itemDetails']['itemIngestionStatus']);
        }
        $info['itemDetails'] = $itemDetails;
        $offset = $offset+1000;
    }
    return $info;
}

 

结果数据

Array
(
    [feedId] => F87332655E3845DE86B61E23AD52C7C0@ARMBBgA
    [feedStatus] => PROCESSED
    [feedSubmissionDate] => 2023-11-18T03:57:01.704Z
    [itemsReceived] => 3
    [itemsSucceeded] => 3
    [itemsFailed] => 0
    [itemsProcessing] => 0
    [offset] => 0
    [limit] => 1000
    [itemDetails] => Array
        (
            [0] => Array
                (
                    [martId] => 7
                    [sku] => NikouMX-haiXD12487
                    [wpid] => 6638ERWI19MO
                    [index] => 0
                    [itemid] => 5188303865
                    [productIdentifiers] => Array
                        (
                        )

                    [ingestionStatus] => SUCCESS
                )

            [1] => Array
                (
                    [martId] => 7
                    [sku] => NikouMX-haiXD05332-04
                    [wpid] => 3S3G629YPAQG
                    [index] => 1
                    [itemid] => 5157989450
                    [productIdentifiers] => Array
                        (
                        )

                    [ingestionStatus] => SUCCESS
                )

            [2] => Array
                (
                    [martId] => 7
                    [sku] => NikouMX-hai2411210184611
                    [wpid] => 7K8GQ28FZOSK
                    [index] => 2
                    [itemid] => 5188270257
                    [productIdentifiers] => Array
                        (
                        )

                    [ingestionStatus] => SUCCESS
                )

        )

)