Forum

Thread tagged as: Question, Shop

Retrieve Parent ID's from Cart

I'm trying to retrieve an array of parent ID's from each item in the cart, but I'm just getting a string.

I'm calling:

$cartContents = perch_shop_cart([
    'template' => 'cart/_parent_ids.html',
    'split-items' => true
  ], true);

The _parent_ids.html is like so:

<perch:cartitems>
  <perch:cartitem id="parentID" />
</perch:cartitems>

However, an array isn't returned, it's just a string of the parent ID's – is split-items not supported by perch_shop_cart?

My use case is that certain products expire, so I need to loop through each cart item and remove them from the cart if the expiry date has passed. I have it working for simple products but now I need to adapt the code to accommodate variants.

Diagnostics

Perch Runway: 3.0.8
Production mode: Production (100)
Installed apps: content (3.0.8), assets (3.0.8), categories (3.0.8), perch_forms (1.9.1), perch_shop_orders (1.2.4), perch_shop_products (1.2.4), perch_shop (1.2.4), perch_members (1.6.2)
DB driver: PDO
DB tables: perch2_backup_plans (1), perch2_backup_resources (0), perch2_backup_runs (20), perch2_categories (7), perch2_category_counts (0), perch2_category_sets (1), perch2_collection_index (0), perch2_collection_items (0), perch2_collection_revisions (0), perch2_collections (0), perch2_content_index (78), perch2_content_items (29), perch2_content_locks (0), perch2_content_regions (7), perch2_forms (1), perch2_forms_responses (2), perch2_members (4), perch2_members_forms (1), perch2_members_member_tags (0), perch2_members_sessions (1), perch2_members_tags (0), perch2_menu_items (15), perch2_navigation (0), perch2_navigation_pages (0), perch2_page_routes (6), perch2_page_templates (18), perch2_pages (17), perch2_resource_log (69), perch2_resource_tags (0), perch2_resources (18), perch2_resources_to_tags (0), perch2_settings (32), perch2_shop_addresses (77), perch2_shop_admin_index (64), perch2_shop_brands (1), perch2_shop_cart (168), perch2_shop_cart_data (103), perch2_shop_cart_items (44), perch2_shop_countries (250), perch2_shop_currencies (152), perch2_shop_customers (5), perch2_shop_emails (2), perch2_shop_index (1593), perch2_shop_option_values (6), perch2_shop_options (2), perch2_shop_order_items (65), perch2_shop_order_promotions (0), perch2_shop_order_statuses (9), perch2_shop_orders (33), perch2_shop_orders_meta (1), perch2_shop_product_files (0), perch2_shop_product_option_values (6), perch2_shop_product_options (2), perch2_shop_product_tags (0), perch2_shop_products (28), perch2_shop_promotions (0), perch2_shop_sales (0), perch2_shop_search (18), perch2_shop_shipping_zone_countries (1), perch2_shop_shipping_zones (1), perch2_shop_shippings (2), perch2_shop_tax_exhibits (105), perch2_shop_tax_group_rates (1), perch2_shop_tax_groups (1), perch2_shop_tax_locations (1), perch2_shop_tax_rates (1), perch2_shop_variants (21), perch2_user_passwords (1), perch2_user_privileges (72), perch2_user_role_privileges (22), perch2_user_roles (2), perch2_users (3)
Users: 3
App runtimes:
<?php
  $apps_list = [
        'perch_forms',
        'perch_members',
        'perch_shop',
    ];
Scheduled tasks for Backup: plan_1 (10 mins)
Editor plug-ins: config-min.js, config.js
H1: c2c0d4c862fcff0f53f3e20e7722906e
L1: b8e0f7cd814dce17db4311d25a624608
F1: 0c66c2e1f82f9e0b7617b2cb8270f2c7
content_singlePageEdit: 1
helpURL:
hideBranding: 0
content_collapseList: 1
lang: en-gb
update_2.8.34: done
headerColour: #000000
headerScheme: dark
update_runway_2.8.34: done
perch_shop_update: 1.2.3
perch_members_login_page: /members/login.php?r={returnURL}
perch_shop_price_tax_mode: inc
perch_shop_trade_price_tax_mode: inc
perch_shop_product_url: /products/{slug}
perch_shop_default_currency: 47
perch_shop_reporting_currency: 47
perch_shop_invoice_number_format: invoice%d
dashboard: 0
hide_pwd_reset: 0
content_hideNonEditableRegions: 0
content_frontend_edit: 0
latest_version:
on_sale_version:
update_runway_3.0.8: done
perch_members_update: 1.4
keyboardShortcuts: 0
siteOffline: 0
content_skip_region_list: 0
siteURL: /
perch_shop_site_url: https://1dw.creativemonster.co.uk
logoPath: /admin/resources/logo.svg
PERCH_DEVELOPMENT: 10
PERCH_STAGING: 50
PERCH_PRODUCTION: 100
PERCH_SITEPATH: /Users/Toby/Sites/1dw
PERCH_SCHEDULE_SECRET: eXybvCZ4wDfAzr9F
PERCH_DB_USERNAME: root
PERCH_DB_SERVER: localhost
PERCH_DB_DATABASE: perch_1dw_local
PERCH_DB_PREFIX: perch2_
PERCH_EMAIL_FROM: admin@1daywow.com
PERCH_EMAIL_FROM_NAME: 1 Day Wow
PERCH_LOGINPATH: /admin
PERCH_PATH: /Users/Toby/Sites/1-day-wow/admin
PERCH_CORE: /Users/Toby/Sites/1-day-wow/admin/core
PERCH_RESFILEPATH: /Users/Toby/Sites/1-day-wow/admin/resources
PERCH_RESPATH: /admin/resources
PERCH_HTML5: 1
PERCH_TZ: europe/london
PERCH_DEBUG: 1
PERCH_CUSTOM_EDITOR_CONFIGS: 1
PERCH_RUNWAY: 1
PERCH_ERROR_MODE: DIE
PERCH_DATE_LONG: %d %B %Y
PERCH_DATE_SHORT: %d %b %Y
PERCH_TIME_SHORT: %H:%M
PERCH_TIME_LONG: %H:%M:%S
PERCH_RUNWAY_ROUTED:
PERCH_STRONG_PASSWORDS:
PERCH_ASSET_VERSION: 5163d57ff611b3cf853b
PERCH_PREVIEW_ARG: preview
PERCH_TEMPLATE_PATH: /Users/Toby/Sites/1-day-wow/admin/templates
PERCH_TEMPLATE_FILTERS:
PERCH_DEFAULT_DOC: index.php
PERCH_DEFAULT_EXT: .php
PERCH_PRODUCTION_MODE: 100
PERCH_XHTML_MARKUP:
PERCH_RWD: 1
PERCH_HTML_ENTITIES:
PERCH_SSL:
PERCH_STRIPSLASHES:
PERCH_PROGRESSIVE_FLUSH: 1
PERCH_PARANOID:
PERCH_FORCE_SECURE_COOKIES:
PERCH_DEFAULT_BUCKET: default
PERCH_TRANSLATION_ASSIST:
PERCH_PASSWORD_MIN_LENGTH: 6
PERCH_MAX_FAILED_LOGINS: 10
PERCH_AUTH_LOCKOUT_DURATION: 1 HOUR
PERCH_VERIFY_UPLOADS:
PERCH_PRIV_ASSIST:
PERCH_AUTH_PLUGIN:
PERCH_DB_CHARSET: utf8
PERCH_DB_PORT:
PERCH_DB_SOCKET:
PERCH_SHOP_VERSION: 1.2.4
PERCH_APPS_EDITOR_PLUGIN: markitup
PERCH_APPS_EDITOR_MARKUP_LANGUAGE: markdown
Hosting settings

PHP: 7.1.6
Zend: 3.1.0
OS: Darwin
SAPI: cgi-fcgi
Safe mode: not detected
MySQL client: mysqlnd 5.0.12-dev - 20150407 - $Id: b396954eeb2d1d9ed7902b8bae237b287f21ad9e $
MySQL server: 5.6.35
Free disk space: 19.92 GB
Extensions: Core, date, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dom, hash, fileinfo, filter, ftp, gd, SPL, iconv, intl, json, ldap, mbstring, session, standard, mysqlnd, PDO, pdo_mysql, pdo_sqlite, Phar, posix, readline, Reflection, mysqli, SimpleXML, soap, sockets, exif, tokenizer, wddx, xml, xmlreader, xmlrpc, xmlwriter, xsl, zip, cgi-fcgi, imap, gettext, mcrypt, pgsql, pdo_pgsql, igbinary, memcached
GD: Yes
ImageMagick: No
PHP max upload size: 32M
PHP max form post size: 32M
PHP memory limit: 128M
Total max uploadable file size: 32M
Resource folder writeable: Yes
Session timeout: 24 minutes
Native JSON: Yes
Filter functions: Yes
Transliteration functions: Yes
PHP_FCGI_CHILDREN: 4
PWD: /Applications/MAMP/fcgi-bin
PHP_FCGI_MAX_REQUESTS: 200
__CF_USER_TEXT_ENCODING: 0x1F5:0x0:0x2
ORIG_SCRIPT_NAME: /fcgi-bin/php7.1.6.fcgi
ORIG_PATH_TRANSLATED: /Users/Toby/Sites/1-day-wow/admin/core/settings/diagnostics/index.php
ORIG_PATH_INFO: /admin/core/settings/diagnostics/index.php
ORIG_SCRIPT_FILENAME: /Applications/MAMP/fcgi-bin/php7.1.6.fcgi
SCRIPT_NAME: /admin/core/settings/diagnostics/index.php
REQUEST_URI: /admin/core/settings/diagnostics/?extended
QUERY_STRING: extended
REQUEST_METHOD: GET
SERVER_PROTOCOL: HTTP/1.1
GATEWAY_INTERFACE: CGI/1.1
REDIRECT_URL: /admin/core/settings/diagnostics/index.php
REDIRECT_QUERY_STRING: extended
REMOTE_PORT: 57620
SCRIPT_FILENAME: /Users/Toby/Sites/1-day-wow/admin/core/settings/diagnostics/index.php
SERVER_ADMIN: you@example.com
DOCUMENT_ROOT: /Users/Toby/Sites/1-day-wow
REMOTE_ADDR: ::1
SERVER_PORT: 8888
SERVER_ADDR: ::1
SERVER_NAME: 1dw.loc
SERVER_SOFTWARE: Apache
PATH: /usr/bin:/bin:/usr/sbin:/sbin
HTTP_COOKIE: PHPSESSID=d0c37df6973ff5751de7a78349a17187; cmsa=1
HTTP_ACCEPT_LANGUAGE: en-US,en;q=0.8
HTTP_ACCEPT_ENCODING: gzip, deflate
HTTP_REFERER: https://1dw.loc:8888/admin/core/settings/diagnostics/add-ons/
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
HTTP_USER_AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36
HTTP_UPGRADE_INSECURE_REQUESTS: 1
HTTP_CONNECTION: keep-alive
HTTP_HOST: 1dw.loc:8888
REDIRECT_STATUS: 200
REDIRECT_HANDLER: php-fastcgi
FCGI_ROLE: RESPONDER
PHP_SELF: /admin/core/settings/diagnostics/index.php
REQUEST_TIME_FLOAT: 1504036457.8
REQUEST_TIME: 1504036457
argc: 1
Toby Martin

Toby Martin 1 points

  • 4 years ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

it doesn't appear that split-items is supported by perch_shop_cart()

I guess the answer is to turn your $cartContents string into an array with explode()?

Yeah I've done that in the end but it does feel kind of hacky:

  $cartItemIds = perch_shop_cart([
    'template' => 'cart/_ids.html',
  ], true);

  $ids = explode("|", $cartItemIds);

  foreach ($ids as $id) {
    $combinedIds[] = explode(",", $id);
  }

  foreach($combinedIds as $combinedId) {
    $productInCartData = perch_shop_products([
      'filter'        => '_id',
      'match'         => 'eq',
      'value'         => $combinedId[0],
      'skip-template' => true
    ], true);

    $endDate = $productInCartData[0]['endDate'];
    $now = date('Y-m-d H:i:s');

    if ($endDate < $now) {
      perch_shop_remove_from_cart($combinedId[1]);
      $productsRemoved = true;
    }
  }

Template for _ids.html

<perch:cartitems>
  <perch:if exists="parentID">
    <perch:cartitem id="parentID" />,<perch:cartitem id="id" /><perch:if not-exists="perch_item_last">|</perch:if>
  <perch:else />
    <perch:cartitem id="id" />,<perch:cartitem id="id" /><perch:if not-exists="perch_item_last">|</perch:if>
  </perch:if>
</perch:cartitems>

What made it trickier, was that I needed the parent ID to check the endDate field on, but also the product ID of the variant to use with perch_shop_remove_from_cart().

It would be helpful if you could pass in the parent ID to perch_shop_remove_from_cart() to remove all variants of that ID, as well as have support for split-items to perch_shop_cart().