magento

package
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const MaxPageSize = 10000

MaxPageSize is the upper bound for results per page to prevent accidental resource exhaustion on large catalogs.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attribute

type Attribute struct {
	AttributeID   int               `json:"attribute_id"`
	AttributeCode string            `json:"attribute_code"`
	FrontendInput string            `json:"frontend_input"`
	FrontendLabel string            `json:"default_frontend_label"`
	IsRequired    bool              `json:"is_required"`
	IsUserDefined bool              `json:"is_user_defined"`
	Options       []AttributeOption `json:"options,omitempty"`
	EntityTypeID  string            `json:"entity_type_id"`
}

Attribute represents a Magento 2 EAV attribute.

type AttributeOption

type AttributeOption struct {
	Label string `json:"label"`
	Value string `json:"value"`
}

type AttributeSet

type AttributeSet struct {
	AttributeSetID   int    `json:"attribute_set_id"`
	AttributeSetName string `json:"attribute_set_name"`
	SortOrder        int    `json:"sort_order"`
	EntityTypeID     int    `json:"entity_type_id"`
}

AttributeSet represents a Magento 2 attribute set.

type CMSBlock

type CMSBlock struct {
	ID           int    `json:"id"`
	Identifier   string `json:"identifier"`
	Title        string `json:"title"`
	Content      string `json:"content"`
	Active       bool   `json:"active"`
	CreationTime string `json:"creation_time"`
	UpdateTime   string `json:"update_time"`
}

CMSBlock represents a Magento 2 CMS static block.

type CMSPage

type CMSPage struct {
	ID              int    `json:"id"`
	Identifier      string `json:"identifier"`
	Title           string `json:"title"`
	Content         string `json:"content"`
	ContentHeading  string `json:"content_heading"`
	Active          bool   `json:"active"`
	SortOrder       string `json:"sort_order"`
	PageLayout      string `json:"page_layout"`
	MetaTitle       string `json:"meta_title"`
	MetaKeywords    string `json:"meta_keywords"`
	MetaDescription string `json:"meta_description"`
	CreationTime    string `json:"creation_time"`
	UpdateTime      string `json:"update_time"`
}

CMSPage represents a Magento 2 CMS page.

type CartRule

type CartRule struct {
	RuleID              int            `json:"rule_id"`
	Name                string         `json:"name"`
	Description         string         `json:"description"`
	IsActive            bool           `json:"is_active"`
	StopRulesProcessing bool           `json:"stop_rules_processing"`
	SortOrder           int            `json:"sort_order"`
	SimpleAction        string         `json:"simple_action"`
	DiscountAmount      float64        `json:"discount_amount"`
	DiscountQty         float64        `json:"discount_qty"`
	DiscountStep        int            `json:"discount_step"`
	ApplyToShipping     bool           `json:"apply_to_shipping"`
	TimesUsed           int            `json:"times_used"`
	IsRSS               bool           `json:"is_rss"`
	CouponType          string         `json:"coupon_type"`
	UseAutoGeneration   bool           `json:"use_auto_generation"`
	UsesPerCoupon       int            `json:"uses_per_coupon"`
	UsesPerCustomer     int            `json:"uses_per_customer"`
	FromDate            string         `json:"from_date"`
	ToDate              string         `json:"to_date"`
	CustomerGroupIDs    []int          `json:"customer_group_ids"`
	WebsiteIDs          []int          `json:"website_ids"`
	StoreLabels         []StoreLabel   `json:"store_labels,omitempty"`
	ExtensionAttrs      map[string]any `json:"extension_attributes,omitempty"`
}

CartRule represents a Magento 2 cart price rule (sales rule).

type CatalogRule

type CatalogRule struct {
	RuleID              int            `json:"rule_id"`
	Name                string         `json:"name"`
	Description         string         `json:"description"`
	IsActive            bool           `json:"is_active"`
	StopRulesProcessing bool           `json:"stop_rules_processing"`
	SortOrder           int            `json:"sort_order"`
	SimpleAction        string         `json:"simple_action"`
	DiscountAmount      float64        `json:"discount_amount"`
	FromDate            string         `json:"from_date"`
	ToDate              string         `json:"to_date"`
	CustomerGroupIDs    []int          `json:"customer_group_ids"`
	WebsiteIDs          []int          `json:"website_ids"`
	ExtensionAttrs      map[string]any `json:"extension_attributes,omitempty"`
}

CatalogRule represents a Magento 2 catalog price rule.

type Category

type Category struct {
	ID               int               `json:"id"`
	ParentID         int               `json:"parent_id"`
	Name             string            `json:"name"`
	IsActive         bool              `json:"is_active"`
	Position         int               `json:"position"`
	Level            int               `json:"level"`
	ProductCount     int               `json:"product_count"`
	ChildrenData     []Category        `json:"children_data,omitempty"`
	CustomAttributes []CustomAttribute `json:"custom_attributes,omitempty"`
}

Category represents a Magento 2 category.

type CategoryProduct

type CategoryProduct struct {
	SKU        string `json:"sku"`
	Position   int    `json:"position"`
	CategoryID string `json:"category_id"`
}

CategoryProduct represents a product assignment within a category.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client provides access to the Magento 2 REST API.

func New

func New(opts ClientOptions) (*Client, error)

New creates a new Magento REST API client.

func (*Client) GetAttribute

func (c *Client) GetAttribute(ctx context.Context, code string) (*Attribute, error)

GetAttribute retrieves a product attribute by code.

func (*Client) GetAttributeOptions

func (c *Client) GetAttributeOptions(ctx context.Context, code string) ([]AttributeOption, error)

GetAttributeOptions retrieves the option values for an attribute.

func (*Client) GetCMSBlock

func (c *Client) GetCMSBlock(ctx context.Context, id int) (*CMSBlock, error)

GetCMSBlock retrieves a single CMS block by ID.

func (*Client) GetCMSPage

func (c *Client) GetCMSPage(ctx context.Context, id int) (*CMSPage, error)

GetCMSPage retrieves a single CMS page by ID.

func (*Client) GetCartRule

func (c *Client) GetCartRule(ctx context.Context, id int) (*CartRule, error)

GetCartRule retrieves a single cart price rule by ID.

func (*Client) GetCatalogRule

func (c *Client) GetCatalogRule(ctx context.Context, id int) (*CatalogRule, error)

GetCatalogRule retrieves a single catalog price rule by ID.

func (*Client) GetCategory

func (c *Client) GetCategory(ctx context.Context, id int) (*Category, error)

GetCategory retrieves a single category by ID.

func (*Client) GetCategoryProducts

func (c *Client) GetCategoryProducts(ctx context.Context, categoryID int) ([]CategoryProduct, error)

GetCategoryProducts retrieves products assigned to a category.

func (*Client) GetCategoryTree

func (c *Client) GetCategoryTree(ctx context.Context, rootID int, depth int) (*Category, error)

GetCategoryTree retrieves the full category tree from a root ID.

func (*Client) GetConfigEntries

func (c *Client) GetConfigEntries(ctx context.Context, storeCode string) ([]ConfigEntry, error)

GetConfigEntries retrieves store configuration and returns it as a flat list of ConfigEntry values with Magento config paths. If storeCode is non-empty, results are filtered to that store.

func (*Client) GetConfigurableChildren

func (c *Client) GetConfigurableChildren(ctx context.Context, sku string) ([]Product, error)

GetConfigurableChildren returns the simple product variants of a configurable product.

func (*Client) GetConfigurableOptions

func (c *Client) GetConfigurableOptions(ctx context.Context, sku string) ([]ConfigurableOption, error)

GetConfigurableOptions returns the configurable options for a product.

func (*Client) GetCoupon

func (c *Client) GetCoupon(ctx context.Context, id int) (*Coupon, error)

GetCoupon retrieves a single coupon by ID.

func (*Client) GetProduct

func (c *Client) GetProduct(ctx context.Context, sku string) (*Product, error)

GetProduct retrieves a single product by SKU.

func (*Client) GetProductMedia

func (c *Client) GetProductMedia(ctx context.Context, sku string) ([]MediaEntry, error)

GetProductMedia retrieves media gallery entries for a product.

func (*Client) GetStockStatus

func (c *Client) GetStockStatus(ctx context.Context, sku string) (*StockItem, error)

GetStockStatus retrieves the stock/inventory status for a product SKU.

func (*Client) GetStoreConfigRaw

func (c *Client) GetStoreConfigRaw(ctx context.Context) ([]map[string]any, error)

GetStoreConfigRaw retrieves store configuration as raw maps to capture all fields returned by the API, including any not mapped in our struct.

func (*Client) HTTP

func (c *Client) HTTP() *httpx.Client

HTTP returns the underlying HTTP client.

func (*Client) ListAttributeSets

func (c *Client) ListAttributeSets(ctx context.Context, search *SearchCriteria) (*SearchResult[AttributeSet], error)

ListAttributeSets retrieves attribute sets matching the search criteria.

func (*Client) ListCMSBlocks

func (c *Client) ListCMSBlocks(ctx context.Context, search *SearchCriteria) (*SearchResult[CMSBlock], error)

ListCMSBlocks retrieves CMS blocks matching search criteria.

func (*Client) ListCMSPages

func (c *Client) ListCMSPages(ctx context.Context, search *SearchCriteria) (*SearchResult[CMSPage], error)

ListCMSPages retrieves CMS pages matching search criteria.

func (*Client) ListCartRules

func (c *Client) ListCartRules(ctx context.Context, search *SearchCriteria) (*SearchResult[CartRule], error)

ListCartRules retrieves cart price rules (sales rules) matching search criteria.

func (*Client) ListCatalogRules

func (c *Client) ListCatalogRules(ctx context.Context, search *SearchCriteria) (*SearchResult[CatalogRule], error)

ListCatalogRules retrieves catalog price rules matching search criteria.

func (*Client) ListCoupons

func (c *Client) ListCoupons(ctx context.Context, search *SearchCriteria) (*SearchResult[Coupon], error)

ListCoupons retrieves coupons matching search criteria.

func (*Client) ListProducts

func (c *Client) ListProducts(ctx context.Context, search *SearchCriteria) (*SearchResult[Product], error)

ListProducts retrieves products matching the search criteria.

func (*Client) ListStoreConfigs

func (c *Client) ListStoreConfigs(ctx context.Context) ([]StoreConfig, error)

ListStoreConfigs retrieves store configuration for all store views.

func (*Client) ListStoreGroups

func (c *Client) ListStoreGroups(ctx context.Context) ([]StoreGroup, error)

ListStoreGroups retrieves all store groups.

func (*Client) ListStoreViews

func (c *Client) ListStoreViews(ctx context.Context) ([]StoreView, error)

ListStoreViews retrieves all store views.

func (*Client) ListWebsites

func (c *Client) ListWebsites(ctx context.Context) ([]Website, error)

ListWebsites retrieves all websites.

type ClientOptions

type ClientOptions struct {
	BaseURL   string
	Token     string
	StoreCode string

	EnableCache bool
	Retry       httpx.RetryPolicy
	Debug       bool
}

ClientOptions configures a Magento client.

type ConfigEntry

type ConfigEntry struct {
	Path  string `json:"path"`
	Value string `json:"value"`
	Scope string `json:"scope"`
}

ConfigEntry represents a single configuration path and its value.

func FilterConfigEntries

func FilterConfigEntries(entries []ConfigEntry, filter string) []ConfigEntry

FilterConfigEntries returns entries whose path starts with prefix or contains the search term (case-insensitive).

type ConfigurableOption

type ConfigurableOption struct {
	ID          int                 `json:"id"`
	AttributeID string              `json:"attribute_id"`
	Label       string              `json:"label"`
	Position    int                 `json:"position"`
	Values      []ConfigurableValue `json:"values"`
	ProductID   int                 `json:"product_id"`
}

ConfigurableOption represents a configurable product attribute option.

type ConfigurableValue

type ConfigurableValue struct {
	ValueIndex int `json:"value_index"`
}

type Coupon

type Coupon struct {
	CouponID         int    `json:"coupon_id"`
	RuleID           int    `json:"rule_id"`
	Code             string `json:"code"`
	UsageLimit       int    `json:"usage_limit"`
	UsagePerCustomer int    `json:"usage_per_customer"`
	TimesUsed        int    `json:"times_used"`
	IsPrimary        bool   `json:"is_primary"`
	Type             int    `json:"type"`
	CreatedAt        string `json:"created_at"`
	ExpirationDate   string `json:"expiration_date"`
}

Coupon represents a Magento 2 coupon.

type CustomAttribute

type CustomAttribute struct {
	AttributeCode string `json:"attribute_code"`
	Value         any    `json:"value"`
}

type InventorySourceItem

type InventorySourceItem struct {
	SKU        string  `json:"sku"`
	SourceCode string  `json:"source_code"`
	Quantity   float64 `json:"quantity"`
	Status     int     `json:"status"`
}

InventorySourceItem represents MSI (Multi-Source Inventory) source items.

type MediaEntry

type MediaEntry struct {
	ID        int      `json:"id"`
	MediaType string   `json:"media_type"`
	Label     string   `json:"label"`
	Position  int      `json:"position"`
	Disabled  bool     `json:"disabled"`
	Types     []string `json:"types"`
	File      string   `json:"file"`
}

type OptionValue

type OptionValue struct {
	Title     string  `json:"title"`
	SortOrder int     `json:"sort_order"`
	Price     float64 `json:"price"`
	PriceType string  `json:"price_type"`
	ValueID   int     `json:"option_type_id"`
}

type Product

type Product struct {
	ID               int               `json:"id"`
	SKU              string            `json:"sku"`
	Name             string            `json:"name"`
	Price            float64           `json:"price"`
	Status           int               `json:"status"`
	Visibility       int               `json:"visibility"`
	TypeID           string            `json:"type_id"`
	Weight           float64           `json:"weight"`
	CreatedAt        string            `json:"created_at"`
	UpdatedAt        string            `json:"updated_at"`
	AttributeSetID   int               `json:"attribute_set_id"`
	CustomAttributes []CustomAttribute `json:"custom_attributes,omitempty"`
	ExtensionAttrs   map[string]any    `json:"extension_attributes,omitempty"`
	MediaGallery     []MediaEntry      `json:"media_gallery_entries,omitempty"`
	ProductLinks     []ProductLink     `json:"product_links,omitempty"`
	Options          []ProductOption   `json:"options,omitempty"`
	TierPrices       []TierPrice       `json:"tier_prices,omitempty"`
}

Product represents a Magento 2 catalog product.

type ProductLink struct {
	SKU               string `json:"sku"`
	LinkType          string `json:"link_type"`
	LinkedProductSKU  string `json:"linked_product_sku"`
	LinkedProductType string `json:"linked_product_type"`
	Position          int    `json:"position"`
}

type ProductOption

type ProductOption struct {
	ProductSKU string        `json:"product_sku"`
	OptionID   int           `json:"option_id"`
	Title      string        `json:"title"`
	Type       string        `json:"type"`
	SortOrder  int           `json:"sort_order"`
	IsRequired bool          `json:"is_require"`
	Values     []OptionValue `json:"values,omitempty"`
}

type SearchCriteria

type SearchCriteria struct {
	// contains filtered or unexported fields
}

SearchCriteria builds Magento 2 REST API search query parameters.

func NewSearch

func NewSearch() *SearchCriteria

NewSearch creates a new SearchCriteria builder.

func (*SearchCriteria) AddFilter

func (s *SearchCriteria) AddFilter(expr string) error

AddFilter adds a filter to a new filter group (AND with other groups). Format: "field operator value" e.g. "name like %shirt%", "price gt 50"

func (*SearchCriteria) AddSort

func (s *SearchCriteria) AddSort(expr string) error

AddSort adds a sort order. Format: "field:direction" e.g. "price:ASC"

func (*SearchCriteria) Encode

func (s *SearchCriteria) Encode() string

Encode returns the search criteria as URL query parameters.

func (*SearchCriteria) SetCurrentPage

func (s *SearchCriteria) SetCurrentPage(page int)

SetCurrentPage sets the page number (1-based).

func (*SearchCriteria) SetPageSize

func (s *SearchCriteria) SetPageSize(size int)

SetPageSize sets the number of results per page (capped at MaxPageSize).

type SearchResult

type SearchResult[T any] struct {
	Items          []T `json:"items"`
	TotalCount     int `json:"total_count"`
	SearchCriteria struct {
		FilterGroups []struct {
			Filters []struct {
				Field         string `json:"field"`
				Value         string `json:"value"`
				ConditionType string `json:"condition_type"`
			} `json:"filters"`
		} `json:"filter_groups"`
		PageSize    int `json:"page_size"`
		CurrentPage int `json:"current_page"`
	} `json:"search_criteria"`
}

SearchResult is the standard Magento 2 paginated response.

type StockItem

type StockItem struct {
	ItemID       int     `json:"item_id"`
	ProductID    int     `json:"product_id"`
	StockID      int     `json:"stock_id"`
	Qty          float64 `json:"qty"`
	IsInStock    bool    `json:"is_in_stock"`
	IsQtyDecimal bool    `json:"is_qty_decimal"`
	MinQty       float64 `json:"min_qty"`
	MinSaleQty   float64 `json:"min_sale_qty"`
	MaxSaleQty   float64 `json:"max_sale_qty"`
}

StockItem represents Magento 2 inventory data.

type StoreConfig

type StoreConfig struct {
	ID                     int    `json:"id"`
	Code                   string `json:"code"`
	WebsiteID              int    `json:"website_id"`
	Locale                 string `json:"locale"`
	BaseCurrencyCode       string `json:"base_currency_code"`
	DefaultDisplayCurrency string `json:"default_display_currency_code"`
	Timezone               string `json:"timezone"`
	WeightUnit             string `json:"weight_unit"`
	BaseURL                string `json:"base_url"`
	BaseLinkURL            string `json:"base_link_url"`
	BaseStaticURL          string `json:"base_static_url"`
	BaseMediaURL           string `json:"base_media_url"`
	SecureBaseURL          string `json:"secure_base_url"`
	SecureBaseLinkURL      string `json:"secure_base_link_url"`
	SecureBaseStaticURL    string `json:"secure_base_static_url"`
	SecureBaseMediaURL     string `json:"secure_base_media_url"`
}

StoreConfig represents Magento 2 store configuration.

type StoreGroup

type StoreGroup struct {
	ID             int    `json:"id"`
	WebsiteID      int    `json:"website_id"`
	Name           string `json:"name"`
	RootCategoryID int    `json:"root_category_id"`
	DefaultStoreID int    `json:"default_store_id"`
	Code           string `json:"code"`
}

StoreGroup represents a Magento 2 store group.

type StoreLabel

type StoreLabel struct {
	StoreID int    `json:"store_id"`
	Label   string `json:"store_label"`
}

StoreLabel represents a label for a specific store view.

type StoreView

type StoreView struct {
	ID           int    `json:"id"`
	Code         string `json:"code"`
	Name         string `json:"name"`
	WebsiteID    int    `json:"website_id"`
	StoreGroupID int    `json:"store_group_id"`
	IsActive     int    `json:"is_active"`
}

StoreView represents a Magento 2 store view.

type TierPrice

type TierPrice struct {
	CustomerGroupID int     `json:"customer_group_id"`
	Qty             float64 `json:"qty"`
	Value           float64 `json:"value"`
}

type Website

type Website struct {
	ID             int    `json:"id"`
	Code           string `json:"code"`
	Name           string `json:"name"`
	DefaultGroupID int    `json:"default_group_id"`
}

Website represents a Magento 2 website.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL