Hi there
I’m working on a custom version of the Shopify plugin, not only migrating to v5, but also extending some functionality, like adding logic for the cart via Shopify Storefront API, add native buttons to sync manually, etc.
While most of the migration works fine, I run into a persistent issue with saving content on the product page that I haven’t been able to solve. The products mix virtual content from Shopify with custom content. When editing any field, the changes are visible but as soon as I try to save the content, switching a tab or going into the new preview mode, the changes get lost.
The shopify.product.php model is quite simple, and should not cause the problem, unless anything is missing, like e.g overwriting the save function:
<?php
use Kirby\Cms\Page;
use Kirby\Content\Field;
class ShopifyProductPage extends Page
{
public function smartPrice(): string
{
$price = $this->shopifyPrice()->value();
$compareAtPrice = $this->shopifyCompareAtPrice()->value();
if (!empty($compareAtPrice) && $compareAtPrice !== '0.00') {
return '<span class="product__price"><ins>' . $this->formatPrice($price) . '</ins> <del>' . $this->formatPrice($compareAtPrice) . '</del></span>';
}
return '<span class="product__price">' . $this->formatPrice($price) . '</span>';
}
public function smartReleaseDate(): Field
{
return new Field($this, 'smartReleaseDate', $this->release()->or($this->shopifyPublishedAt())->or($this->shopifyCreatedAt()));
}
}
shopify.products.php:
<?php
use Kirby\Cms\Page;
use Kirby\Cms\Pages;
use Kirby\Data\Yaml;
use Kirby\Toolkit\Str;
use Kirby\Uuid\Uuid;
class ShopifyProductsPage extends Page
{
static $subpages = null;
public function subpages()
{
if (static::$subpages) {
return static::$subpages;
}
return static::$subpages = Pages::factory($this->inventory()['children'], $this);
}
public function children(): Pages
{
if ($this->children instanceof Pages) {
return $this->children;
}
$products = \KirbyShopify\App::getProducts();
$activeproducts = array_filter($products, function ($product) {
return $product['status'] == 'active';
});
$activeproductscount = count($activeproducts);
$pages = array_map(function ($product) {
$slug = Str::slug($product['handle']);
$page = $this->subpages()->findBy('slug', $slug);
$pagecontent = $page ? $page->content()->toArray() : null;
$shopifyProduct = [
'title' => $product['title'],
'shopifyStatus' => $product['status'],
'shopifyTitle' => $product['title'],
'shopifyID' => $product['id'],
'shopifyCreatedAt' => $product['created_at'],
'shopifyUpdatedAt' => $product['updated_at'],
'shopifyPublishedAt' => $product['published_at'],
'shopifyHandle' => $product['handle'],
'shopifyVendor' => $product['vendor'],
'shopifyFeaturedImage' => count($product['images']) > 0 ? Yaml::encode([$product['images'][0]]) : '',
'shopifyImages' => Yaml::encode($product['images']),
'shopifyDescriptionHTML' => $product['body_html'],
'shopifyPrice' => count($product['variants']) > 0 ? $product['variants'][0]['price'] : '',
'shopifyCompareAtPrice' => count($product['variants']) > 0 ? $product['variants'][0]['compare_at_price'] : '',
'shopifyType' => $product['product_type'],
'shopifyTags' => $product['tags'],
'shopifyVariants' => Yaml::encode($product['variants']),
'shopifyOptions' => Yaml::encode($product['options']),
'shopifyInventory' => count($product['variants']) > 0 ? $product['variants'][0]['inventory_quantity'] : '',
];
if ($pagecontent) {
foreach ($shopifyProduct as $k) {
unset($pagecontent[strtolower($k)]);
}
}
if ($page && $page->num()) {
$num = $page->num();
} else {
$publishedAt = $product['published_at'];
$createdAt = $product['created_at'];
$timestamp = $publishedAt ? strtotime($publishedAt) : strtotime($createdAt);
$num = date('Ymd', $timestamp); // Convert timestamp to YYYYMMDD format
}
return [
'slug' => $slug,
'num' => $num,
'template' => 'shopify.product',
'model' => 'shopify.product',
'files' => $page ? $page->files()->toArray() : null,
'uuid' => $page ? $page->uuid() : Uuid::generate(),
'content' => $page ? $shopifyProduct + $pagecontent : $shopifyProduct,
];
}, $products);
usort($pages, function ($a, $b) {
return $a['num'] <=> $b['num'];
});
return Pages::factory($pages, $this);
}
}
Would be happy for any hints.
All the best
Jakob